分享一个
maven
插件spotless
https://github.com/diffplug/spotless
它可以让你的代码保持整洁
例如添加插件:
代码语言:javascript复制<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.27.2</version>
<configuration>
<java>
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>
<googleJavaFormat>
<version>1.7</version>
<style>GOOGLE</style>
</googleJavaFormat>
<importOrder>
<order>javax,java,#</order>
</importOrder>
<replaceRegex>
<name>Remove wildcard imports</name>
<searchRegex>imports (static)*s*[^*s] *;(rn|r|n)</searchRegex>
<replacement>$1</replacement>
</replaceRegex>
<replaceRegex>
<name>Block powermock</name>
<searchRegex>imports org.powermock.[^*s]*(|*);(rn|r|n)</searchRegex>
<replacement>$1</replacement>
</replaceRegex>
<replaceRegex>
<name>Block jUnit4 imports</name>
<searchRegex>imports org.junit.[^jupiter][^*s]*(|*);(rn|r|n)</searchRegex>
<replacement>$1</replacement>
</replaceRegex>
<removeUnusedImports/>
</java>
</configuration>
<executions>
<execution>
<id>spotless-check</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
然后即可使用:
代码语言:javascript复制user@machine repo % mvn spotless:check
[ERROR] > The following files had format violations:
[ERROR] srcmainjavacomdiffpluggradlespotlessFormatExtension.java
[ERROR] -tt····if·(targets.length·==·0)·{
[ERROR] ttif·(targets.length·==·0)·{
[ERROR] Run 'mvn spotless:apply' to fix these violations.
user@machine repo % mvn spotless:apply
[INFO] BUILD SUCCESS
user@machine repo % mvn spotless:check
[INFO] BUILD SUCCESS