-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for Maven POM sorting/formatting #946
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
22ddb38
Added support for Maven POM sorting/formatting
tisoft 503f4c0
load SortPom from JarState
tisoft 8b7c3eb
use FormatterFunc and SortPomState from current ClassLoader
tisoft c0d1b68
Fixed SpotBugs errors
tisoft 680d95c
Rename SortPomTest to SortPomMavenTest.
nedtwigg 4a80576
Create a no-maven SortPomTest which can run quickly.
nedtwigg f2c0e4a
Rename SortPomState to SortPomCfg, and:
nedtwigg bcd3735
FeatureClassLoader now loads `com.diffplug.spotless.glue.*` via `de…
nedtwigg 4b780c0
SortPomStep can now live within the normal `src/main/java`, and only …
nedtwigg 6d6ab66
Fix typo.
nedtwigg 24bc908
Fix FeatureClassLoader for eclipse entries.
nedtwigg 2b85d84
plugin-maven doesn't need any special dependency on the lib jar.
nedtwigg b95e93b
Generalize the `sortPom` stuff to facilitate #524 (glue code).
nedtwigg 617abf5
Added documentation for sortPom
tisoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fvgh, just FYI, we finally have a way to ship glue code without requiring all deps of that glue code. It works like so:
com.diffplug.spotless.glue.
-> we define classes from the jars, which requires linking againstcom.diffplug.spotless
frombuildToolClassLoader
com.diffplug.spotless.extra
, which we always load from the jars as (we have in the past)org.slf4j.
frombuildToolClassLoader
(as we have in the past)This will allow us to replace a lot of manual reflection code, ala #524.