Skip to content

Commit 22ddb38

Browse files
committed
Added support for Maven POM sorting/formatting
1 parent a9e8357 commit 22ddb38

File tree

9 files changed

+382
-1
lines changed

9 files changed

+382
-1
lines changed

lib-extra/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ dependencies {
1616
implementation "com.googlecode.concurrent-trees:concurrent-trees:2.6.1"
1717
// used for xml parsing in EclipseFormatter
1818
implementation "org.codehaus.groovy:groovy-xml:3.0.9"
19+
// used for pom sorting
20+
implementation 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0'
1921

2022
// testing
2123
testImplementation project(':testlib')
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra.pom;
17+
18+
import java.io.File;
19+
import java.io.FileReader;
20+
import java.io.FileWriter;
21+
import java.io.Serializable;
22+
import java.util.logging.Logger;
23+
24+
import org.apache.commons.io.IOUtils;
25+
26+
import com.diffplug.spotless.FormatterFunc;
27+
import com.diffplug.spotless.FormatterStep;
28+
29+
import sortpom.SortPomImpl;
30+
import sortpom.logger.SortPomLogger;
31+
import sortpom.parameter.PluginParameters;
32+
33+
public class SortPomStep {
34+
35+
public static final String NAME = "sortPom";
36+
private static final Logger logger = Logger.getLogger(SortPomStep.class.getName());
37+
38+
private SortPomStep() {}
39+
40+
public static FormatterStep create(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions) {
41+
return FormatterStep.createLazy(NAME, () -> new State(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions), State::createFormat);
42+
}
43+
44+
static final class State implements Serializable {
45+
private static final long serialVersionUID = 1L;
46+
final String encoding;
47+
48+
final String lineSeparator;
49+
50+
final boolean expandEmptyElements;
51+
52+
final boolean spaceBeforeCloseEmptyElement;
53+
54+
final boolean keepBlankLines;
55+
56+
final int nrOfIndentSpace;
57+
58+
final boolean indentBlankLines;
59+
60+
final boolean indentSchemaLocation;
61+
62+
final String predefinedSortOrder;
63+
64+
final String sortOrderFile;
65+
66+
final String sortDependencies;
67+
68+
final String sortDependencyExclusions;
69+
70+
final String sortPlugins;
71+
72+
final boolean sortProperties;
73+
74+
final boolean sortModules;
75+
76+
final boolean sortExecutions;
77+
78+
State(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions) {
79+
this.encoding = encoding;
80+
this.lineSeparator = lineSeparator;
81+
this.expandEmptyElements = expandEmptyElements;
82+
this.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement;
83+
this.keepBlankLines = keepBlankLines;
84+
this.nrOfIndentSpace = nrOfIndentSpace;
85+
this.indentBlankLines = indentBlankLines;
86+
this.indentSchemaLocation = indentSchemaLocation;
87+
this.predefinedSortOrder = predefinedSortOrder;
88+
this.sortOrderFile = sortOrderFile;
89+
this.sortDependencies = sortDependencies;
90+
this.sortDependencyExclusions = sortDependencyExclusions;
91+
this.sortPlugins = sortPlugins;
92+
this.sortProperties = sortProperties;
93+
this.sortModules = sortModules;
94+
this.sortExecutions = sortExecutions;
95+
}
96+
97+
FormatterFunc createFormat() {
98+
return input -> {
99+
// SortPom expects a file to sort, so we write the inpout into a temporary file
100+
File pom = File.createTempFile("pom", ".xml");
101+
pom.deleteOnExit();
102+
try (FileWriter fw = new FileWriter(pom)) {
103+
fw.write(input);
104+
}
105+
SortPomImpl sortPom = new SortPomImpl();
106+
sortPom.setup(new SortPomLogger() {
107+
@Override
108+
public void warn(String content) {
109+
logger.warning(content);
110+
}
111+
112+
@Override
113+
public void info(String content) {
114+
logger.info(content);
115+
}
116+
117+
@Override
118+
public void error(String content) {
119+
logger.severe(content);
120+
}
121+
}, PluginParameters.builder()
122+
.setPomFile(pom)
123+
.setFileOutput(false, null, null, false)
124+
.setEncoding(encoding)
125+
.setFormatting(lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines)
126+
.setIndent(nrOfIndentSpace, indentBlankLines, indentSchemaLocation)
127+
.setSortOrder(sortOrderFile, predefinedSortOrder)
128+
.setSortEntities(sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions)
129+
.setTriggers(false)
130+
.build());
131+
sortPom.sortPom();
132+
return IOUtils.toString(new FileReader(pom));
133+
};
134+
}
135+
}
136+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.diffplug.spotless.maven.groovy.Groovy;
5757
import com.diffplug.spotless.maven.java.Java;
5858
import com.diffplug.spotless.maven.kotlin.Kotlin;
59+
import com.diffplug.spotless.maven.pom.Pom;
5960
import com.diffplug.spotless.maven.python.Python;
6061
import com.diffplug.spotless.maven.scala.Scala;
6162
import com.diffplug.spotless.maven.sql.Sql;
@@ -120,6 +121,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
120121
@Parameter
121122
private Antlr4 antlr4;
122123

124+
@Parameter
125+
private Pom pom;
126+
123127
@Parameter
124128
private Sql sql;
125129

@@ -258,7 +262,7 @@ private FileLocator getFileLocator() {
258262
}
259263

260264
private List<FormatterFactory> getFormatterFactories() {
261-
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4, sql, python))
265+
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4, pom, sql, python))
262266
.filter(Objects::nonNull)
263267
.collect(toList());
264268
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.pom;
17+
18+
import java.util.Set;
19+
20+
import com.diffplug.common.collect.ImmutableSet;
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
import com.diffplug.spotless.maven.generic.LicenseHeader;
23+
24+
/**
25+
* A {@link FormatterFactory} implementation that corresponds to {@code <pom>...</pom>} configuration element.
26+
* <p>
27+
* It defines a formatter for Maven pom files that can execute both language agnostic (e.g. {@link LicenseHeader})
28+
* and pom-specific (e.g. {@link SortPom}) steps.
29+
*/
30+
public class Pom extends FormatterFactory {
31+
@Override
32+
public Set<String> defaultIncludes() {
33+
return ImmutableSet.of("pom.xml");
34+
}
35+
36+
@Override
37+
public String licenseHeaderDelimiter() {
38+
return null;
39+
}
40+
41+
public void addSortPom(SortPom sortPom) {
42+
addStepFactory(sortPom);
43+
}
44+
45+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.pom;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.extra.pom.SortPomStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class SortPom implements FormatterStepFactory {
26+
@Parameter
27+
String encoding = "UTF-8";
28+
29+
@Parameter
30+
String lineSeparator = System.getProperty("line.separator");
31+
32+
@Parameter
33+
boolean expandEmptyElements = true;
34+
35+
@Parameter
36+
boolean spaceBeforeCloseEmptyElement = false;
37+
38+
@Parameter
39+
boolean keepBlankLines = true;
40+
41+
@Parameter
42+
int nrOfIndentSpace = 2;
43+
44+
@Parameter
45+
boolean indentBlankLines = false;
46+
47+
@Parameter
48+
boolean indentSchemaLocation = false;
49+
50+
@Parameter
51+
String predefinedSortOrder = "recommended_2008_06";
52+
53+
@Parameter
54+
String sortOrderFile;
55+
56+
@Parameter
57+
String sortDependencies;
58+
59+
@Parameter
60+
String sortDependencyExclusions;
61+
62+
@Parameter
63+
String sortPlugins;
64+
65+
@Parameter
66+
boolean sortProperties = false;
67+
68+
@Parameter
69+
boolean sortModules = false;
70+
71+
@Parameter
72+
boolean sortExecutions = false;
73+
74+
@Override
75+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
76+
return SortPomStep.create(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions);
77+
}
78+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ protected void writePomWithPrettierSteps(String includes, String... steps) throw
134134
writePom(formats(groupWithSteps("format", including(includes), steps)));
135135
}
136136

137+
protected void writePomWithPomSteps(String... steps) throws IOException {
138+
writePom(groupWithSteps("pom", including("pom_test.xml"), steps));
139+
}
140+
137141
protected void writePom(String... configuration) throws IOException {
138142
writePom(null, configuration);
139143
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.pom;
17+
18+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
public class SortPomTest extends MavenIntegrationHarness {
23+
@Test
24+
public void testSortPomWithDefaultConfig() throws Exception {
25+
writePomWithPomSteps("<sortPom/>");
26+
27+
setFile("pom_test.xml").toResource("pom/pom_dirty.xml");
28+
System.out.println(mavenRunner().withArguments("spotless:apply").runNoError().error());
29+
assertFile("pom_test.xml").sameAsResource("pom/pom_clean_default.xml");
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>spotless.test</groupId>
6+
<artifactId>minimal-pom</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>minimal-pom</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<java.version>1.8</java.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.11</version>
23+
<scope>test</scope>
24+
</dependency>
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>3.1</version>
33+
<configuration>
34+
<source>${java.version}</source>
35+
<target>${java.version}</target>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>

0 commit comments

Comments
 (0)