From 427b6ab57f1ee0525647e034d1bb6e3d012fce9a Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 28 Nov 2022 10:58:18 -0800 Subject: [PATCH] Support multi-module builds (regression in 3.26) (#397) --- .../AbstractDependencyGraphTraversingMojo.java | 1 + .../maven/plugins/hpi/AbstractJenkinsMojo.java | 10 ++++++++-- .../maven/plugins/hpi/MavenArtifact.java | 14 +++++++++++--- .../org/jenkinsci/maven/plugins/hpi/RunMojo.java | 15 ++++++++++++--- .../maven/plugins/hpi/TestDependencyMojo.java | 1 + .../maven/plugins/hpi/TestRuntimeMojo.java | 7 ++++++- .../org/jenkinsci/maven/plugins/hpi/WarMojo.java | 7 ++++++- 7 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractDependencyGraphTraversingMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractDependencyGraphTraversingMojo.java index 9556e649df..efd1b78abe 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractDependencyGraphTraversingMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractDependencyGraphTraversingMojo.java @@ -21,6 +21,7 @@ protected void traverseProject() throws DependencyGraphBuilderException { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); buildingRequest.setProject(project); + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); visit(graphBuilder.buildDependencyGraph(buildingRequest, null)); } diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsMojo.java index bfd62f83dd..a5d1fc4796 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsMojo.java @@ -16,9 +16,11 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; import org.apache.maven.project.ProjectBuilder; +import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate; import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver; import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; @@ -132,8 +134,11 @@ private Artifact resolveJenkinsCore() throws MojoExecutionException { artifactCoordinate.setVersion(findJenkinsVersion()); try { + ProjectBuildingRequest buildingRequest = + new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); return artifactResolver - .resolveArtifact(session.getProjectBuildingRequest(), artifactCoordinate) + .resolveArtifact(buildingRequest, artifactCoordinate) .getArtifact(); } catch (ArtifactResolverException e) { throw new MojoExecutionException("Couldn't download artifact: ", e); @@ -146,6 +151,7 @@ protected MavenArtifact wrap(Artifact a) { artifactResolver, artifactFactory, projectBuilder, - session); + session, + project); } } diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/MavenArtifact.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/MavenArtifact.java index 215ee4dab4..db64e7c294 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/MavenArtifact.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/MavenArtifact.java @@ -37,24 +37,28 @@ public class MavenArtifact implements Comparable { public final Artifact artifact; public final ArtifactResolver resolver; public final MavenSession session; + public final MavenProject project; public MavenArtifact( Artifact artifact, ArtifactResolver resolver, ArtifactFactory artifactFactory, ProjectBuilder builder, - MavenSession session) { + MavenSession session, + MavenProject project) { this.artifact = artifact; this.resolver = resolver; this.artifactFactory = artifactFactory; this.builder = builder; this.session = Objects.requireNonNull(session); + this.project = Objects.requireNonNull(project); } public MavenProject resolvePom() throws ProjectBuildingException { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); buildingRequest.setProcessPlugins(false); // improve performance + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); buildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); return builder.build(artifact, buildingRequest).getProject(); } @@ -123,7 +127,10 @@ public String getType() { public File getFile() { if (artifact.getFile()==null) try { - return resolver.resolveArtifact(session.getProjectBuildingRequest(), artifact).getArtifact().getFile(); + ProjectBuildingRequest buildingRequest = + new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); + return resolver.resolveArtifact(buildingRequest, artifact).getArtifact().getFile(); } catch (ArtifactResolverException e) { throw new RuntimeException("Failed to resolve "+getId(),e); } @@ -141,7 +148,8 @@ public MavenArtifact getHpi() throws IOException { resolver, artifactFactory, builder, - session); + session, + project); } public List getDependencyTrail() { diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/RunMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/RunMojo.java index d82291e39f..53ad4c5488 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/RunMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/RunMojo.java @@ -33,11 +33,13 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.DefaultDependencyResolutionRequest; +import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.DependencyResolutionException; import org.apache.maven.project.DependencyResolutionRequest; import org.apache.maven.project.DependencyResolutionResult; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuilder; +import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectDependenciesResolver; import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver; import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; @@ -327,7 +329,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (webAppFile == null) { Artifact jenkinsWarArtifact = getJenkinsWarArtifact(); try { - jenkinsWarArtifact = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), jenkinsWarArtifact).getArtifact(); + ProjectBuildingRequest buildingRequest = + new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); + jenkinsWarArtifact = artifactResolver.resolveArtifact(buildingRequest, jenkinsWarArtifact).getArtifact(); } catch (ArtifactResolverException x) { throw new MojoExecutionException("Could not resolve " + jenkinsWarArtifact + ": " + x, x); } @@ -375,7 +380,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { // find corresponding .hpi file Artifact hpi = artifactFactory.createArtifact(a.getGroupId(),a.getArtifactId(),a.getVersion(),null,"hpi"); - hpi = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), hpi).getArtifact(); + ProjectBuildingRequest buildingRequest = + new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); + hpi = artifactResolver.resolveArtifact(buildingRequest, hpi).getArtifact(); // check recursive dependency. this is a rare case that happens when we split out some things from the core // into a plugin @@ -829,7 +837,8 @@ protected MavenArtifact wrap(Artifact a) { artifactResolver, artifactFactory, projectBuilder, - session); + session, + project); } protected Artifact getJenkinsWarArtifact() throws MojoExecutionException { diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/TestDependencyMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/TestDependencyMojo.java index 43f21f1403..c9c413aee4 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/TestDependencyMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/TestDependencyMojo.java @@ -212,6 +212,7 @@ public void execute() throws MojoExecutionException { try { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); buildingRequest.setProject(shadow); + buildingRequest.setRemoteRepositories(shadow.getRemoteArtifactRepositories()); ArtifactFilter filter = null; // Evaluate all scopes node = dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter); } catch (DependencyCollectorBuilderException e) { diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/TestRuntimeMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/TestRuntimeMojo.java index e7d8ec798e..698ffd67f0 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/TestRuntimeMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/TestRuntimeMojo.java @@ -20,6 +20,8 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate; import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; @@ -79,8 +81,11 @@ private Artifact resolveJenkinsWar() throws MojoExecutionException { artifactCoordinate.setExtension("war"); try { + ProjectBuildingRequest buildingRequest = + new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); return artifactResolver - .resolveArtifact(session.getProjectBuildingRequest(), artifactCoordinate) + .resolveArtifact(buildingRequest, artifactCoordinate) .getArtifact(); } catch (ArtifactResolverException e) { throw new MojoExecutionException("Couldn't download artifact: ", e); diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/WarMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/WarMojo.java index 2f2f5c1c34..a25bd19550 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/WarMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/WarMojo.java @@ -7,8 +7,10 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; +import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Zip; @@ -84,7 +86,10 @@ public void execute() throws MojoExecutionException { // find corresponding .hpi file Artifact hpi = artifactFactory.createArtifact(a.getGroupId(),a.getArtifactId(),a.getVersion(),null,"hpi"); - hpi = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), hpi).getArtifact(); + ProjectBuildingRequest buildingRequest = + new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); + buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); + hpi = artifactResolver.resolveArtifact(buildingRequest, hpi).getArtifact(); if (hpi.getFile().isDirectory()) throw new UnsupportedOperationException(hpi.getFile()+" is a directory and not packaged yet. this isn't supported");