0

我正在使用 maven 一尘不染的插件来格式化 java 文件,但它不能跳过文件。我用过excludeand toggle 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>
4

1 回答 1

2

根据pom.xml片段配置了两个不同的检查。

  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 文件。幸运的是<java>格式化程序也支持<includes><excludes>所以这将工作:

<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>

于 2020-12-26T11:53:58.377 回答