-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Maven POM sorting/formatting (#946)
- Loading branch information
Showing
18 changed files
with
631 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.pom; | ||
|
||
import java.io.Serializable; | ||
|
||
// Class and members must be public, otherwise we get failed to access class com.diffplug.spotless.pom.SortPomInternalState from class com.diffplug.spotless.pom.SortPomFormatterFunc (com.diffplug.spotless.pom.SortPomInternalState is in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @682bd3c4; com.diffplug.spotless.pom.SortPomFormatterFunc is in unnamed module of loader com.diffplug.spotless.pom.DelegatingClassLoader @573284a5) | ||
public class SortPomCfg implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public String encoding = "UTF-8"; | ||
|
||
public String lineSeparator = System.getProperty("line.separator"); | ||
|
||
public boolean expandEmptyElements = true; | ||
|
||
public boolean spaceBeforeCloseEmptyElement = false; | ||
|
||
public boolean keepBlankLines = true; | ||
|
||
public int nrOfIndentSpace = 2; | ||
|
||
public boolean indentBlankLines = false; | ||
|
||
public boolean indentSchemaLocation = false; | ||
|
||
public String predefinedSortOrder = "recommended_2008_06"; | ||
|
||
public String sortOrderFile = null; | ||
|
||
public String sortDependencies = null; | ||
|
||
public String sortDependencyExclusions = null; | ||
|
||
public String sortPlugins = null; | ||
|
||
public boolean sortProperties = false; | ||
|
||
public boolean sortModules = false; | ||
|
||
public boolean sortExecutions = false; | ||
} |
54 changes: 54 additions & 0 deletions
54
lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.pom; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
public class SortPomStep { | ||
public static final String NAME = "sortPom"; | ||
|
||
private SortPomStep() {} | ||
|
||
private SortPomCfg cfg; | ||
|
||
public static FormatterStep create(SortPomCfg cfg, Provisioner provisioner) { | ||
return FormatterStep.createLazy(NAME, () -> new State(cfg, provisioner), State::createFormat); | ||
} | ||
|
||
static class State implements Serializable { | ||
SortPomCfg cfg; | ||
JarState jarState; | ||
|
||
public State(SortPomCfg cfg, Provisioner provisioner) throws IOException { | ||
this.cfg = cfg; | ||
this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.0.0", provisioner); | ||
} | ||
|
||
FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | ||
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.pom.SortPomFormatterFunc"); | ||
Constructor<?> constructor = formatterFunc.getConstructor(SortPomCfg.class); | ||
return (FormatterFunc) constructor.newInstance(cfg); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
lib/src/sortPom/java/com/diffplug/spotless/glue/pom/SortPomFormatterFunc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.glue.pom; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.pom.SortPomCfg; | ||
|
||
import sortpom.SortPomImpl; | ||
import sortpom.logger.SortPomLogger; | ||
import sortpom.parameter.PluginParameters; | ||
|
||
public class SortPomFormatterFunc implements FormatterFunc { | ||
private static final Logger logger = Logger.getLogger(SortPomFormatterFunc.class.getName()); | ||
private final SortPomCfg cfg; | ||
|
||
public SortPomFormatterFunc(SortPomCfg cfg) { | ||
this.cfg = cfg; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
// SortPom expects a file to sort, so we write the inpout into a temporary file | ||
File pom = File.createTempFile("pom", ".xml"); | ||
pom.deleteOnExit(); | ||
IOUtils.write(input, new FileOutputStream(pom), cfg.encoding); | ||
SortPomImpl sortPom = new SortPomImpl(); | ||
sortPom.setup(new MySortPomLogger(), PluginParameters.builder() | ||
.setPomFile(pom) | ||
.setFileOutput(false, null, null, false) | ||
.setEncoding(cfg.encoding) | ||
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement, cfg.keepBlankLines) | ||
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation) | ||
.setSortOrder(cfg.sortOrderFile, cfg.predefinedSortOrder) | ||
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions) | ||
.setTriggers(false) | ||
.build()); | ||
sortPom.sortPom(); | ||
return IOUtils.toString(new FileInputStream(pom), cfg.encoding); | ||
} | ||
|
||
private static class MySortPomLogger implements SortPomLogger { | ||
@Override | ||
public void warn(String content) { | ||
logger.warning(content); | ||
} | ||
|
||
@Override | ||
public void info(String content) { | ||
logger.info(content); | ||
} | ||
|
||
@Override | ||
public void error(String content) { | ||
logger.severe(content); | ||
} | ||
} | ||
} |
Oops, something went wrong.