maven spotless不能跳过文件

0yycz8jy  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(741)

我使用maven spotless插件格式化java文件,但它不能跳过文件。我曾经 exclude 以及 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>
unguejic

unguejic1#

根据 pom.xml 代码段配置了两个不同的检查。
通用格式

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

在本节中,include/exclude模式配置良好,工作正常2。java格式

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

在这种情况下,没有配置include/exclude模式,因此将使用默认模式

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

相关问题