使用 M2E 从 Checkstyle 配置为 Eclipse 格式化程序自动配置制表符

Configure automatically tab character for Eclipse Formatter from Checkstyle config with M2E

我想 完全自动 我的代码格式化程序关于我的 配置的配置,已经外部化了。它几乎可以工作......但不完全:至少制表符没有反映在 Eclipse 格式化程序中。

配置

工具

我已经在使用以下工具和插件

结构

这是一个深度多模块分层项目。主要思想是 checkstyle 配置被外部化(在 fmk-qa 中)并在 fmk-core 中被称为依赖项。

fmk-parent
|-- fmk-core
|   |-- fmk-front
|   |   |-- front-sub1
|   |   |-- front-sub2
|   |   |-- ...
|   |   `-- pom.xml
|   |-- fmk-back
|   |   |-- back-sub1
|   |   |-- back-sub2
|   |   |-- ...
|   |   `-- pom.xml
|   |-- fmk-commons
|   |   `-- pom.xml
|   `-- pom.xml
`-- fmk-qa
    |-- pom.xml
    `-- checkstyle-config.xml

配置

Maven 父配置

[...]

<build>
    <pluginManagement>
        <plugins>
        <!-- Checkstyle Dependency where configuration is externalized  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
                <dependencies>
                    <dependency>
                        <groupId>corp.framework</groupId>
                        <artifactId>fmk-qa</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            [...]
        <plugins>
    <pluginManagement>

核心项目配置

<build>

<plugins>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>

        <configuration>
            <configLocation>checkstyle-config.xml</configLocation>
        </configuration>

        <!-- Binding to indicate to M2E that it should trigger analysis in Eclipse -->
        <executions>
            <execution>
                <id>validate</id>
                <phase>validate</phase>
                <configuration>
                    <configLocation>checkstyle-config.xml</configLocation>
                    <failsOnError>false</failsOnError>
                    <linkXRef>true</linkXRef>
                </configuration>
                <goals>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>

        <inherited>true</inherited>
    </plugin>

Checkstyle 配置

<module name = "Checker">
[...]
<!-- Checks for whitespace                               -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
    <module name="FileTabCharacter">
        <property name="eachLine" value="true"/>
    </module>
    [...]
</module>

目前有效的方法

Eclipse 会自动检测一堆东西并配置它们,也就是说

(这也适用于 PMD 和 FindBugs)。

什么不起作用

当我尝试自动格式化代码 (CTRL+SHIFT+F) 时,它格式化了代码,但是用制表符替换了空格,这显然不是我想要的。

我怎样才能告诉自动 eclipse 如何正确

我显然不想让开发人员单独配置它。 此外,我知道我可以根据 Checkstyle 配置 (How to generate an Eclipse formatter configuration from a checkstyle configuration?) 手动生成代码格式化程序。所以希望能通过插件自动实现。

欢迎所有建议,甚至 "pre setted up workspace distribution",即使我正在寻找更复杂的解决方案。我也准备好看到(这可能是我自己的答案):这还不可能,自己做并为社区做贡献;)

不是开箱即用的。因此,您最好的选择是在 Eclipse 中导入或更新项目时向 m2e-code-quality Plugin to make it generate Eclipse formatter configuration in their M2Eclipse's project configurator 提交增强请求。