Closed5

spotlessが動かない

tomstoms

spotless-maven-plugin を使って mvn spotless:apply しても動かない.

spotless-maven-plugin: 2.43.0

pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.43.0</version>
                <configuration>
                    <formats>
                        <format>
                            <includes>
                                <include>src/main/java/**/*.java</include>
                                <include>src/test/java/**/*.java</include>
                            </includes>
                            <trimTrailingWhitespace/>
                            <endWithNewline/>
                            <indent>
                                <spaces>true</spaces>
                                <spacesPerTab>4</spacesPerTab>
                            </indent>
                        </format>
                    </formats>
                    <java>
                        <googleJavaFormat>
                            <style>AOSP</style>
                        </googleJavaFormat>
                    </java>
                </configuration>
            </plugin>
        </plugins>
    </build>

target/spotless-index を削除してもフォーマットされない.

[INFO] --- spotless:2.43.0:apply (default-cli) @ *** ---
[INFO] Index file does not exist. Fallback to an empty index
[INFO] Spotless.Java is keeping * files clean - 0 were changed to be clean, 0 were already clean, * were skipped because caching determined they were already clean
tomstoms

upToDateCheckingfalse にするとフォーマットされる.

pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.43.0</version>
                <configuration>
                    <upToDateChecking>
                        <enabled>false</enabled>
                    </upToDateChecking>
                    <formats>
                        <format>
                            <includes>
                                <include>src/main/java/**/*.java</include>
                                <include>src/test/java/**/*.java</include>
                            </includes>
                            <trimTrailingWhitespace/>
                            <endWithNewline/>
                            <indent>
                                <spaces>true</spaces>
                                <spacesPerTab>4</spacesPerTab>
                            </indent>
                        </format>
                    </formats>
                    <java>
                        <googleJavaFormat>
                            <style>AOSP</style>
                        </googleJavaFormat>
                    </java>
                </configuration>
            </plugin>
        </plugins>
    </build>
tomstoms

format タグと他のタグ, 例えば java などがあるとフォーマットが利かなくなるらしい.

This worked fine for pre-2.35 version, this also works fine if we disable up-to-date index:

  <upToDateChecking>
   <enabled>false</enabled>
 </upToDateChecking>

https://github.com/diffplug/spotless/issues/1656#issuecomment-1861705764

https://github.com/diffplug/spotless/issues/1767

試しに format タグを消すと確かに動作した.

pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.43.0</version>
                <configuration>
                    <java>
                        <googleJavaFormat>
                            <style>AOSP</style>
                        </googleJavaFormat>
                    </java>
                </configuration>
            </plugin>
        </plugins>
    </build>
tomstoms

結論

spotless-maven-plugin:2.35.0 以降では format タグは使わずに java タグだけに書くのが正しい.

pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.43.0</version>
                <configuration>
                    <java>
                        <includes>
                            <include>src/main/java/**/*.java</include>
                            <include>src/test/java/**/*.java</include>
                        </includes>
                        <trimTrailingWhitespace/>
                        <endWithNewline/>
                        <indent>
                            <spaces>true</spaces>
                            <spacesPerTab>4</spacesPerTab>
                        </indent>
                        <googleJavaFormat>
                            <style>AOSP</style>
                        </googleJavaFormat>
                    </java>
                </configuration>
            </plugin>
        </plugins>
    </build>
このスクラップは2024/03/14にクローズされました