GeezerVison is showing github.com — Add remove wildcards imports when formating and ordering imports. · Issue #649 · diffplug/spotless
Add remove wildcards imports when formating and ordering imports. #649DescriptionI 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
Reacted by jochenberger, Krzysztof Czelusniak, Matteo Hausner, usimd, Arlo O'Keeffe, Robert Craigie, uvsmtid, Alexey Pakseykin, Vincent Potuček and Attacktive Activitynedtwigg commented on Jul 22, 2020 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. survivant commented on Aug 3, 2020 nedtwigg commented on Aug 4, 2020 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. brendandburns commented on Dec 3, 2020 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>Reacted by Ned Twigg, Dane Summers, Dylan, Jefersom Martins, Eric Gao, Maksim Kostromin, Szymon Siemieniuk and Vincent Potuček Reacted by Eric Gao, Maksim Kostromin and sblommers EricGao888 commented on Aug 13, 2022
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. nedtwigg commented on Aug 21, 2022
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. Reacted by Eric Gao and Roger Lin guilhemferr commented on Jul 20, 2023 In case anyone is interested in the removal approach. The regex below also works for static imports.
<searchRegex>import\s+(?:static\s+)?[^\*\s]+\*;(\r\n|\r|\n)</searchRegex>
Note that the non-capturing group ?: here is important for it to work. lipiridi commented on Aug 16, 2023 joquijada commented on Jul 15, 2024 lipiridi commented on Jul 16, 2024
https://github.com/lipiridi/spotless-applier
Do you have something that integrates with Gradle itself? In our team we want to automatically fix wildcard imports when we run Gradle spotlessApply task.
Unfortunately, I don't. Pankraz76 commented on Jun 16, 2025 thx.
why not only wrap the regex command into simple runner class and pass the args to the path how its done now.
If located where, it should be one class the mock the current working regex.
@iddeepak
whats your call on this ? iddeepak commented on Jun 16, 2025 How about building this right into the Spotless plugin (Gradle and Maven)? Also, I am not sure but we may need diff regexes in normal and static imports or with comments. we may need cases on this.
We may also need a flag like failOnWildcard. would be an option to error out. Reacted by Dimitrii Lipiridi Pankraz76 commented on Jun 17, 2025 for me its just removing the wildcard, forcing manual fix:
Its still a win, as it will prevent
As its rare, its acceptable having to fix by IDE. iddeepak commented on Jun 19, 2025 @Pankraz76
Have a look on this PR. I would be happy to know your feedback on this. Pankraz76 commented on Jun 24, 2025 nedtwigg commented on Jul 7, 2025 Released in plugin-gradle 7.1.0 and plugin-maven 2.45.0.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|