Maven spotless can't skip files

9.3k Views Asked by At

I'm using maven spotless plugin to format java files, but it can't skip files. I used exclude and toggle off, neither works.

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

There are 2 best solutions below

0
On

Below is the working model. We have two packages for e.g. src/main/java/com/package1 and src/main/java/com/package2 And in below example we have excluded src/main/java/com/package1

<plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>${spotless.version}</version>
                <configuration>
                    <ratchetFrom>origin/main</ratchetFrom>
                    <formats>
                        <format>
                            <includes>
                                <include>src/**/*.java</include>
                            </includes>
                            <excludes>
                                <exclude>src/main/java/com/package1/**/*.java</exclude>
                            </excludes>
                            <trimTrailingWhitespace/>
                            <endWithNewline/>
                            <indent>
                                <spaces>true</spaces>
                                <spacesPerTab>2</spacesPerTab>
                            </indent>
                        </format>
                    </formats>
                    <java>
                        <excludes>
                            <exclude>src/main/java/com/package1/**/*.java</exclude>
                        </excludes>
                        <googleJavaFormat>
                            <version>1.7</version>
                            <style>AOSP</style>
                     <reflowLongStrings>true</reflowLongStrings>
                        </googleJavaFormat>
                        <eclipse>
                            <file>config/code-format/eclipse-java-google-style.xml</file>
                        </eclipse>
                    </java>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <phase>compile</phase>
                    </execution>
                </executions>
            </plugin>
0
On

According to pom.xml snippet two different check was configured.

  1. Generic format
<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>

In this section include / exclude pattern configured well and it works fine 2. Java format

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

In this case there are no include / exclude pattern configured so default will be used which is

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

// for default excludes
Collections.emptySet();

Now it's easy to figure out Java formatter will analyze all java files. Fortunately <java> formatter also supports <includes> and <excludes> so this will work:

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