spotless
spotless copied to clipboard
Add remove wildcards imports when formating and ordering imports.
I have some java classes that have imports with wildcards like : import java.util.*;
I want to replace that with the appropriate import list like
import java.util.List;
I'm using this configuration to format my java code
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<!-- Runs in compile phase to fail fast in case of formatting issues.-->
<id>format java</id>
<phase>compile</phase>
<goals>
<goal>apply</goal>
</goals>
<configuration>
<formats>
<!-- you can define as many formats as you want, each is independent -->
<format>
<!-- define the files to apply to -->
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>
<indent>
<spaces>true</spaces>
<spacesPerTab>4</spacesPerTab>
</indent>
</format>
</formats>
<java>
<eclipse>
<file>${basedir}/formatter/eclipse-format.xml</file>
</eclipse>
<removeUnusedImports/>
<importOrder>
<file>${basedir}/formatter/eclipse.importorder</file>
</importOrder>
</java>
</configuration>
</execution>
</executions>
</plugin>
I have this import order
#Organize Import Order
#Wed Jul 22 13:29:56 EDT 2020
0=java
1=javax
2=org
3=com
Currently, all of Spotless' java formatters look at each file in isolation. To implement this feature, Spotless would need to hook into the full classpath and other compiled files. Which is possible, but a lot of work for a small benefit. See #240 for details. Happy to merge a PR, but the level of difficulty is quite high.
I found this plugin. Could it be integrated to the project ? https://github.com/nxnet/organize-imports-maven-plugin
Happy to take a PR for it. One important caveat is that there is a ton of code out there which has already been formatted by our old broken importSorter. Some people will want the new behavior, but some projects might prefer to just not have their import ordering change.
So the new step should have a new name, perhaps organizeImportsNxNet()
or organizeImportsModern()
, so that people who want the old behavior can keep it.
fwiw, I added the following replaceRegexp
to remove wildcards while formatting:
<replaceRegex>
<name>Remove wildcard imports</name>
<searchRegex>import\s+[^\*\s]+\*;(\r\n|\r|\n)</searchRegex>
<replacement>$1</replacement>
</replaceRegex>
fwiw, I added the following
replaceRegexp
to remove wildcards while formatting:<replaceRegex> <name>Remove wildcard imports</name> <searchRegex>import\s+[^\*\s]+\*;(\r\n|\r|\n)</searchRegex> <replacement>$1</replacement> </replaceRegex>
@brendandburns @nedtwigg This is a pretty cool way to remove wildcards automatically. May I ask whether there is a way to only block wildcards or throw some error messages when doing mvn spotless:apply
instead of directly removing them? One possible scenario is that developers thought their wildcard imports got fixed and pushed their code to the remote but actually they lost some imports.
whether there is a way to only block wildcards or throw some error message
With the Gradle plugin, you can add a custom step that throws an Exception
. We don't have a way to do that using the maven syntax.