Skip to content

Commit

Permalink
Merge pull request #100 from oleg-nenashev/bug/JENKINS-55199
Browse files Browse the repository at this point in the history
[JENKINS-55199] - Add explicit resolution of self-dependencies to PCT
  • Loading branch information
oleg-nenashev committed Dec 14, 2018
2 parents ac968d1 + d44cd57 commit b716831
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Expand Up @@ -815,6 +815,9 @@ private void addSplitPluginDependencies(String thisPlugin, MavenRunner.Config mc
System.out.println("Adding/replacing plugin dependencies for compatibility: " + toAdd + " " + toReplace + "\nFor test: " + toAddTest + " " + toReplaceTest);
pom.addDependencies(toAdd, toReplace, toAddTest, toReplaceTest, coreDep, pluginGroupIds, convertFromTestDep);
}

// Remove the self-dependency if any
pom.removeDependency(pluginGroupIds.get(thisPlugin), thisPlugin);
}
}

Expand Down
Expand Up @@ -35,18 +35,25 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

import javax.annotation.Nonnull;

/**
* Class encapsulating business around maven poms
* @author Frederic Camblor
*/
public class MavenPom {

private static final Logger LOGGER = Logger.getLogger(MavenPom.class.getName());

private final static String GROUP_ID_ELEMENT = "groupId";
private final static String ARTIFACT_ID_ELEMENT = "artifactId";
private final static String VERSION_ELEMENT = "version";
Expand Down Expand Up @@ -100,6 +107,39 @@ public void transformPom(MavenCoordinates coreCoordinates) throws PomTransformat
}
}

/**
* Removes the dependency if it exists.
*/
public void removeDependency(@Nonnull String groupdId, @Nonnull String artifactId) 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 dependencies = doc.getRootElement().element("dependencies");
if (dependencies == null) {
dependencies = doc.getRootElement().addElement("dependencies");
}

for (Element mavenDependency : (List<Element>) dependencies.elements("dependency")) {
Element artifactIdElem = mavenDependency.element(ARTIFACT_ID_ELEMENT);
if (artifactIdElem == null || !artifactId.equalsIgnoreCase(artifactIdElem.getText())) {
continue;
}

Element groupIdElem = mavenDependency.element(GROUP_ID_ELEMENT);
if (groupIdElem != null && groupdId.equalsIgnoreCase(groupIdElem.getText()) ) {
LOGGER.log(Level.WARNING, "Removing dependency on {0}:{1}",
new Object[] {groupdId, artifactId});
dependencies.remove(mavenDependency);
}
}

writeDocument(pom, doc);
}

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

0 comments on commit b716831

Please sign in to comment.