【问题标题】:Maven spotless can't skip filesMaven 一尘不染不能跳过文件
【发布时间】:2020-12-25 07:56:13
【问题描述】:

我正在使用 maven 一尘不染的插件来格式化 java 文件,但它不能跳过文件。我用了excludetoggle off,都不管用。

<plugin>
  <groupId>com.diffplug.spotless</groupId>
  <artifactId>spotless-maven-plugin</artifactId>
  <version>2.6.1</version>
  <configuration>
    <formats>
      <format>
        <!-- define the files to apply to -->
        <includes>
          <include>*.java</include>
        </includes>
        <excludes>
          <exclude>api/**/RayCall.java</exclude>
          <exclude>api/**/ActorCall.java</exclude>
          <exclude>runtime/*/generated/**/*.*</exclude>
        </excludes>
        <!-- define the steps to apply to those files -->
        <trimTrailingWhitespace/>
        <endWithNewline/>
        <indent>
          <tabs>true</tabs>
          <spacesPerTab>2</spacesPerTab>
        </indent>
      </format>
    </formats>
    <!-- define a language-specific format -->
    <java>
      <toggleOffOn>
        <off>// Generated by `RayCallGenerator.java`. DO NOT EDIT.</off>
      </toggleOffOn>
      <googleJavaFormat>
        <version>1.7</version>
        <style>GOOGLE</style>
      </googleJavaFormat>
    </java>
  </configuration>
</plugin>

【问题讨论】:

    标签: java maven google-java-format


    【解决方案1】:

    根据pom.xmlsn -p 配置了两个不同的检查。

    1. 通用格式
    <formats>
        <format>
            <!-- define the files to apply to -->
            <includes>
                <include>*.java</include>
            </includes>
            <excludes>
                  <exclude>api/**/RayCall.java</exclude>
                  <exclude>api/**/ActorCall.java</exclude>
                  <exclude>runtime/*/generated/**/*.*</exclude>
            </excludes>
            <!-- define the steps to apply to those files -->
            <trimTrailingWhitespace/>
            <endWithNewline/>
            <indent>
                  <tabs>true</tabs>
                  <spacesPerTab>2</spacesPerTab>
            </indent>
        </format>
    </formats>
    

    在本节中包含/排除模式配置良好,并且工作正常 2.Java格式

    <java>
        <toggleOffOn>
            <off>// Generated by `RayCallGenerator.java`. DO NOT EDIT.</off>
        </toggleOffOn>
        <googleJavaFormat>
            <version>1.7</version>
            <style>GOOGLE</style>
        </googleJavaFormat>
    </java>
    

    在这种情况下,没有配置包含/排除模式,因此将使用默认值

    // for default includes
    ImmutableSet.of("src/main/java/**/*.java", "src/test/java/**/*.java");
    
    // for default excludes
    Collections.emptySet();
    

    现在很容易弄清楚 Java 格式化程序将分析所有 Java 文件。 幸运的是 &lt;java&gt; 格式化程序也支持 &lt;includes&gt;&lt;excludes&gt; 所以这将工作:

    <java>
        <toggleOffOn>
            <off>// Generated by `RayCallGenerator.java`. DO NOT EDIT.</off>
        </toggleOffOn>
        <includes>
            <include>**/*.java</include>
        </includes>
        <excludes>
            <exclude>**/api/**/RayCall.java</exclude>
            <exclude>**/api/**/ActorCall.java</exclude>
            <exclude>**/runtime/*/generated/**/*.*</exclude>
        </excludes>
        <googleJavaFormat>
            <version>1.7</version>
            <style>GOOGLE</style>
        </googleJavaFormat>
    </java>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多