【问题标题】:Maven plugin for auto formatting code to match google checkstyle?用于自动格式化代码以匹配 google checkstyle 的 Maven 插件?
【发布时间】:2018-04-07 21:27:35
【问题描述】:

我在 Intellij 中使用 google checkstyle:https://github.com/google/styleguide

当我在 IntelliJ 中进行任何编码期间进行格式化时,我运行了这个 eclipse checkstyle,它似乎工作正常。我想要一个 Maven 阶段,每当我运行 mvn clean install 时格式化代码。

我正在使用以下插件:

<plugin>
    <groupId>net.revelc.code.formatter</groupId>
    <artifactId>formatter-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>format</goal>
            </goals>
            <configuration>
                <configFile>src/main/resources/eclipse-java-google-style.xml</configFile>
                <encoding>UTF-8</encoding>
            </configuration>
        </execution>
    </executions>
</plugin>

不幸的是,它似乎没有正确注册该配置文件。我想知道获取正确的 checkstyle 配置的最佳经验法则是什么。我应该把它放在哪里以及如何在我的 POM 中表示它?

【问题讨论】:

    标签: java maven checkstyle


    【解决方案1】:

    根据formatter-maven-plugin 文档的示例“使用外部资源的基本配置”,您必须删除:中的“src/main/resources/”:

    <plugin>
    <groupId>net.revelc.code.formatter</groupId>
    <artifactId>formatter-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>format</goal>
            </goals>
            <configuration>
                <configFile>eclipse-java-google-style.xml</configFile>
                <encoding>UTF-8</encoding>
            </configuration>
        </execution>
    </executions>
    </plugin>
    

    【讨论】:

    • Ryan,如果它确实解决了你的问题,你能接受答案吗?
    • 嗨@Guillem,这对我有用。非常感谢。我可以在 git-commit 期间应用这种代码格式吗?很多时候,人们在推送代码之前不会执行 mvn 命令。如果我能以某种方式将其添加为 git commit 钩子,它将有很大帮助。
    • @mitmehta 一种选择是在每个拉取请求上运行一个操作,如果需要重新格式化代码则失败。这样所有的合并都是已经正确格式化的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 2011-04-28
    • 2016-08-31
    • 2012-06-24
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多