diff --git a/changelog.html b/changelog.html index 913cc44915..c1f61a48e9 100644 --- a/changelog.html +++ b/changelog.html @@ -58,6 +58,9 @@ Upcoming changes
  • Display Name is not shown. (issue 17715) +
  • + Properly find parent POMs when fingerprinting a Maven project. + (issue 17775) @@ -73,7 +76,7 @@ Upcoming changes Third-party license display for core was broken since 1.506. (issue 17724)
  • - Amelioration of exception from fingerprinting in a Maven project when a parent POM could not be located. + Mitigation of exception from fingerprinting in a Maven project when a parent POM could not be located. (issue 17775)
  • NPE from MatrixConfiguration.newBuild. diff --git a/maven-plugin/src/main/java/hudson/maven/MavenBuildInformation.java b/maven-plugin/src/main/java/hudson/maven/MavenBuildInformation.java index cd631dd90f..66dbb24f8c 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenBuildInformation.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenBuildInformation.java @@ -21,9 +21,13 @@ package hudson.maven; */ import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; import org.apache.commons.lang.StringUtils; import org.apache.maven.artifact.versioning.ComparableVersion; +import org.apache.maven.model.Model; +import org.apache.maven.project.MavenProject; /** * @author Olivier Lamy @@ -33,6 +37,15 @@ public class MavenBuildInformation implements Serializable { private static final long serialVersionUID = -3719709179508200057L; private String mavenVersion; + /** + * Map of model IDs to IDs of the model parents (if defined). + * @see MavenProject#getId + * @see Model#getId + * @see MavenProject#getParent + * @see Model#getParent + * @since 1.515 + */ + public final Map modelParents = new HashMap(); public MavenBuildInformation(String mavenVersion) { this.mavenVersion = mavenVersion; diff --git a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java index c5874d3d61..2ca820ea64 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -65,6 +65,7 @@ import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; import java.io.PrintStream; +import java.io.Serializable; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collection; @@ -582,7 +583,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild)MavenModuleSetBuild.this)); @@ -619,7 +620,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild(); List changedModules = new ArrayList(); @@ -856,12 +857,14 @@ public class MavenModuleSetBuild extends AbstractMavenBuild poms; try { - poms = getModuleRoot().act(new PomParser(listener, mvn, mavenVersion, envVars, MavenModuleSetBuild.this)); + PomParser.Result result = getModuleRoot().act(new PomParser(listener, mvn, mavenVersion, envVars, MavenModuleSetBuild.this)); + poms = result.infos; + mavenBuildInformation.modelParents.putAll(result.modelParents); } catch (IOException e) { if (project.isIncrementalBuild()) { // If POM parsing failed we should do a full build next time. @@ -997,7 +1000,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild> { + private static final class PomParser implements FileCallable { private final BuildListener listener; private final String rootPOM; /** @@ -1028,7 +1031,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild invoke(File ws, VirtualChannel channel) throws IOException { + public static final class Result implements Serializable { + public final List infos; + public final Map modelParents; + public Result(List infos, Map modelParents) { + this.infos = infos; + this.modelParents = modelParents; + } + } + + public Result invoke(File ws, VirtualChannel channel) throws IOException { File pom; PrintStream logger = listener.getLogger(); @@ -1194,10 +1206,23 @@ public class MavenModuleSetBuild extends AbstractMavenBuild canonicalPaths = new HashMap( mps.size() ); + Map modelParents = new HashMap(); for(MavenProject mp : mps) { // Projects are indexed by POM path and not module path because // Maven allows to have several POMs with different names in the same directory canonicalPaths.put( mp.getFile().getCanonicalPath(), mp ); + while (true) { + String k = mp.getId(); + if (modelParents.containsKey(k)) { + break; + } + MavenProject mpp = mp.getParent(); + if (mpp == null) { + break; + } + modelParents.put(k, mpp.getId()); + mp = mpp; + } } //MavenUtil.resolveModules(embedder,mp,getRootPath(rootPOMRelPrefix),relPath,listener,nonRecursive); @@ -1225,7 +1250,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild(infos); + return new Result(new ArrayList(infos), modelParents); } catch (MavenEmbedderException e) { throw new MavenExecutionException(e); } catch (ProjectBuildingException e) { diff --git a/maven-plugin/src/main/java/hudson/maven/reporters/MavenFingerprinter.java b/maven-plugin/src/main/java/hudson/maven/reporters/MavenFingerprinter.java index 1bc8fae8f1..d884a77ea8 100644 --- a/maven-plugin/src/main/java/hudson/maven/reporters/MavenFingerprinter.java +++ b/maven-plugin/src/main/java/hudson/maven/reporters/MavenFingerprinter.java @@ -138,60 +138,28 @@ public class MavenFingerprinter extends MavenReporter { } private void recordParents(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws IOException, InterruptedException { - MavenProject parent = getParent(pom, listener); + Map modelParents = build.getMavenBuildInformation().modelParents; + ArtifactRepository localRepository = getLocalRepository(build.getMavenBuildInformation(), pom); + if (localRepository == null) { + listener.error("Could not find local repository for " + build.getMavenBuildInformation().getMavenVersion()); + return; + } + String parent = modelParents.get(pom.getId()); while (parent != null) { - File parentFile = parent.getFile(); - - if (parentFile == null) { - // Parent artifact contains no actual file, so we resolve against - // the local repository - ArtifactRepository localRepository = getLocalRepository(build.getMavenBuildInformation(), parent, pom); - if (localRepository != null) { - Artifact parentArtifact = getArtifact(parent); - // Don't use ArtifactRepository.find(), for compatibility with Maven 2.x - if (parentArtifact != null) { - parentFile = new File(localRepository.getBasedir(), - localRepository.pathOf(parentArtifact)); - } - } - } - - if (parentFile != null) { - // we need to include the artifact Id for poms as well, otherwise a - // project with the same groupId would override its parent's - // fingerprint - record(parent.getGroupId() + ":" + parent.getArtifactId(), - parentFile, used); - } - parent = getParent(parent, listener); + String[] parts = parent.split(":"); + assert parts.length == 4 : parent; + // Maven 2.x lacks DefaultArtifact constructor with String version and ArtifactRepository.find: + Artifact parentArtifact = new DefaultArtifact(parts[0], parts[1], VersionRange.createFromVersion(parts[3]), null, parts[2], null, new DefaultArtifactHandler(parts[2])); + File parentFile = new File(localRepository.getBasedir(), localRepository.pathOf(parentArtifact)); + // we need to include the artifact Id for poms as well, otherwise a project with the same groupId would override its parent's fingerprint + record(parts[0] + ":" + parts[1], parentFile, used); + parent = modelParents.get(parent); } } - // XXX consider calling also from PomInfo which makes a naked call to getParent - private static MavenProject getParent(MavenProject pom, BuildListener listener) { - try { - return pom.getParent(); - } catch (IllegalStateException x) { // MNG-5075 - x.printStackTrace(listener.error("Warning: failed to resolve parent of " + pom.getId())); - return null; - } - } - - private Artifact getArtifact(MavenProject parent) { - Artifact art = parent.getArtifact(); - if (art == null) { - // happens for Maven 2.x - DefaultArtifactHandler artifactHandler = new DefaultArtifactHandler("pom"); - art = new DefaultArtifact(parent.getGroupId(), parent.getArtifactId(), VersionRange.createFromVersion(parent.getVersion()), - null, "pom", "", artifactHandler); - } - return art; - } - - private ArtifactRepository getLocalRepository(MavenBuildInformation mavenBuildInformation, MavenProject parent, MavenProject pom) { - + private ArtifactRepository getLocalRepository(MavenBuildInformation mavenBuildInformation, MavenProject pom) { if (mavenBuildInformation.isMaven3OrLater()) { - return parent.getProjectBuildingRequest().getLocalRepository(); + return pom.getProjectBuildingRequest().getLocalRepository(); } else if (mavenBuildInformation.isAtLeastMavenVersion("2.2")) { // principally this should also work with Maven 2.1, but it's not tested, so err on the safe side return getArtifactRepositoryMaven21(pom);