Skip to content

Commit fc55a5e

Browse files
committed
Added wildcardsLast option for Java importOrder in Maven
1 parent 9f158c5 commit fc55a5e

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
* Added `wildcardsLast` option for Java `importOrder` ([#956](https://github.com/diffplug/spotless/pull/956))
67

78
## [2.16.0] - 2021-10-02
89

plugin-maven/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
179179

180180
<importOrder /> <!-- standard import order -->
181181
<importOrder> <!-- or a custom ordering -->
182+
<wildcardsLast>false</wildcardsLast> <!-- Optional, default false. Sort wildcard import after specific imports -->
182183
<order>java,javax,org,com,com.diffplug,</order> <!-- or use <file>${project.basedir}/eclipse.importorder</file> -->
183184
<!-- You probably want an empty string at the end - all of the
184185
imports you didn't specify explicitly will go there. -->

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,17 +31,20 @@ public class ImportOrder implements FormatterStepFactory {
3131
@Parameter
3232
private String order;
3333

34+
@Parameter
35+
private boolean wildcardsLast = false;
36+
3437
@Override
3538
public FormatterStep newFormatterStep(FormatterStepConfig config) {
3639
if (file != null ^ order != null) {
3740
if (file != null) {
3841
File importsFile = config.getFileLocator().locateFile(file);
39-
return ImportOrderStep.forJava().createFrom(importsFile);
42+
return ImportOrderStep.forJava().createFrom(wildcardsLast, importsFile);
4043
} else {
41-
return ImportOrderStep.forJava().createFrom(order.split(",", -1));
44+
return ImportOrderStep.forJava().createFrom(wildcardsLast, order.split(",", -1));
4245
}
4346
} else if (file == null && order == null) {
44-
return ImportOrderStep.forJava().createFrom();
47+
return ImportOrderStep.forJava().createFrom(wildcardsLast);
4548
} else {
4649
throw new IllegalArgumentException("Must specify exactly one of 'file' or 'order'.");
4750
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ void standard() throws Exception {
4545
runTest("java/importsorter/JavaCodeSortedImportsDefault.test");
4646
}
4747

48+
@Test
49+
void wildcardsLast() throws Exception {
50+
writePomWithJavaSteps(
51+
"<importOrder>",
52+
" <wildcardsLast>true</wildcardsLast>",
53+
"</importOrder>");
54+
runTest("java/importsorter/JavaCodeSortedImportsWildcardsLast.test");
55+
}
56+
4857
private void runTest() throws Exception {
4958
runTest("java/importsorter/JavaCodeSortedImports.test");
5059
}

0 commit comments

Comments
 (0)