Skip to content
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

Support on transforming the pom to add/update pluginManagement and properties sections #339

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -146,6 +148,80 @@ public void removeDependency(@Nonnull String groupId, @Nonnull String artifactId

writeDocument(pom, doc);
}

/**
* Create/Update a plugin management section with a set of plugins
* @param pluginsToAdd
* @param includeGroupId - specify if we want to add the groupId or not
* @throws IOException
*/
public void addPluginManagement(List<MavenCoordinates> pluginsToAdd, boolean includeGroupId) throws IOException {
File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
Document doc;
try {
doc = new SAXReader().read(pom);
} catch (DocumentException x) {
throw new IOException(x);
}

Element build = doc.getRootElement().element("build");
if (build == null) {
build = doc.getRootElement().addElement("build");
}

Element pluginManagement = build.element("pluginManagement");
if (pluginManagement == null) {
pluginManagement = build.addElement("pluginManagement");
}

Element plugins = pluginManagement.element("plugins");
if (plugins == null) {
plugins = pluginManagement.addElement("plugins");
}
for (MavenCoordinates plugin : pluginsToAdd) {
Element entry = plugins.addElement("plugin");
if (includeGroupId) {
Element groupIdElem = entry.addElement(GROUP_ID_ELEMENT);
groupIdElem.setText(plugin.groupId);
}
Element artifactIdElem = entry.addElement(ARTIFACT_ID_ELEMENT);
artifactIdElem.setText(plugin.artifactId);
Element versionIdElem = entry.addElement(VERSION_ELEMENT);
versionIdElem.setText(plugin.version);
}

writeDocument(pom, doc);
}

/**
* Create/Update the properties section adding/updating some of them
* @param propertiesToAdd
* @throws IOException
*/
public void addProperties(Properties propertiesToAdd) throws IOException {
File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
Document doc;
try {
doc = new SAXReader().read(pom);
} catch (DocumentException x) {
throw new IOException(x);
}

Element properties = doc.getRootElement().element("properties");
if (properties == null) {
properties = doc.getRootElement().addElement("properties");
}

for (Entry<Object, Object> property : propertiesToAdd.entrySet()) {
String key = (String)property.getKey();
Element entry = properties.element(key);
if (entry == null) {
entry = properties.addElement(key);
}
entry.setText((String)property.getValue());
}
writeDocument(pom, doc);
}

public void addDependencies(Map<String, VersionNumber> toAdd, Map<String, VersionNumber> toReplace, Map<String, VersionNumber> toAddTest, Map<String, VersionNumber> toReplaceTest, Map<String, String> pluginGroupIds, List<String> toConvert)
throws IOException {
Expand Down