GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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. #649

New issue
Copy link
New issue
Copy link
Closed
#2517
Closed
Add remove wildcards imports when formating and ordering imports.#649
#2517
Copy link
Labels
enhancement

Description

@survivant
survivant
opened on Jul 22, 2020
Issue body actions

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
Reacted by jochenberger, Krzysztof Czelusniak, Matteo Hausner, usimd, Arlo O'Keeffe, Robert Craigie, uvsmtid, Alexey Pakseykin, Vincent Potuček and Attacktive

Activity

  1. nedtwigg
    added
    enhancement
    on Jul 22, 2020
  2. nedtwigg commented on Jul 22, 2020

    @nedtwigg
    nedtwigg
    on Jul 22, 2020
    Member
    More actions

    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.

  3. survivant commented on Aug 3, 2020

    @survivant
    survivant
    on Aug 3, 2020
    Author
    More actions

    I found this plugin. Could it be integrated to the project ? https://github.com/nxnet/organize-imports-maven-plugin

  4. nedtwigg commented on Aug 4, 2020

    @nedtwigg
    nedtwigg
    on Aug 4, 2020
    Member
    More actions

    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.

  5. brendandburns commented on Dec 3, 2020

    @brendandburns
    brendandburns
    on Dec 3, 2020
    More actions

    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
  6. EricGao888 commented on Aug 13, 2022

    @EricGao888
    EricGao888
    on Aug 13, 2022
    Contributor
    More actions

    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.

  7. nedtwigg commented on Aug 21, 2022

    @nedtwigg
    nedtwigg
    on Aug 21, 2022
    Member
    More actions

    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
  8. guilhemferr commented on Jul 20, 2023

    @guilhemferr
    guilhemferr
    on Jul 20, 2023
    More actions

    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.

  9. lipiridi commented on Aug 16, 2023

    @lipiridi
    lipiridi
    on Aug 16, 2023
    More actions

    I have added an option to the plugin below that executes IntelliJ IDEA's 'optimize imports' task while disallowing the use of asterisk imports. This means that asterisk imports will be replaced with fully qualified names before applying the Spotless task.
    https://github.com/lipiridi/spotless-applier
    https://plugins.jetbrains.com/plugin/22455-spotless-applier

    Reacted by Oliver Weiler
  10. joquijada commented on Jul 15, 2024

    @joquijada
    joquijada
    on Jul 15, 2024
    More actions

    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.

  11. lipiridi commented on Jul 16, 2024

    @lipiridi
    lipiridi
    on Jul 16, 2024
    More actions

    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.

  12. Xtansia
    added a commit that references this issue on Sep 3, 2024

    Forbid wildcard imports

    c12e164
  13. dblock
    added a commit that references this issue on Sep 4, 2024

    Forbid wildcard imports (#1180)

    e856ecb
  14. Xtansia
    added a commit that references this issue on Sep 4, 2024

    Forbid wildcard imports (opensearch-project#1180)

    48d52e9
  15. reta
    added a commit that references this issue on Sep 5, 2024

    Forbid wildcard imports (#1180) (#1183)

    2d6d5f8
  16. Pankraz76 commented on Jun 16, 2025

    @Pankraz76
    Pankraz76
    on Jun 16, 2025
    More actions

    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 ?

  17. iddeepak commented on Jun 16, 2025

    @iddeepak
    iddeepak
    on Jun 16, 2025
    Contributor
    More actions

    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
  18. Pankraz76 commented on Jun 17, 2025

    @Pankraz76
    Pankraz76
    on Jun 17, 2025
    More actions

    for me its just removing the wildcard, forcing manual fix:

    Image

    Its still a win, as it will prevent

    As its rare, its acceptable having to fix by IDE.

  19. iddeepak commented on Jun 19, 2025

    @iddeepak
    iddeepak
    on Jun 19, 2025
    Contributor
    More actions

    @Pankraz76
    Have a look on this PR. I would be happy to know your feedback on this.

  20. Goooler
    closed this as completedin #2517on Jun 24, 2025
  21. Pankraz76 commented on Jun 24, 2025

    @Pankraz76
    Pankraz76
    on Jun 24, 2025
    More actions

    Thanks.

  22. nedtwigg commented on Jul 7, 2025

    @nedtwigg
    nedtwigg
    on Jul 7, 2025
    Member
    More actions

    Released in plugin-gradle 7.1.0 and plugin-maven 2.45.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.