This is an automated email from the ASF dual-hosted git repository. davidb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-feature-converter-maven-plugin.git
commit 8e70884a60216b284cb87351fbd48e82c1a5acf6 Author: David Bosschaert <[email protected]> AuthorDate: Tue Oct 6 15:28:20 2020 +0100 SLING-9794 Content package artifacts found by maven plugin don't always have their 'file' attribute set This fixes the NPE that can happen in this case --- .../cpconverter/maven/mojos/ContentPackage.java | 67 +++++++++++++++++++--- .../cpconverter/maven/mojos/ConvertCPMojo.java | 14 ++++- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java index 295f270..0956a5f 100644 --- a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java +++ b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java @@ -17,17 +17,25 @@ package org.apache.sling.cpconverter.maven.mojos; import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.project.MavenProject; +import org.eclipse.aether.RepositoryException; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResult; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Set; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public class ContentPackage { /** @@ -48,7 +56,7 @@ public class ContentPackage { private boolean excludeTransitive; private boolean moduleIsContentPackage; - + public void setGroupId(String groupId) { this.groupId = groupId == null ? "" : groupId; } @@ -78,7 +86,8 @@ public class ContentPackage { return moduleIsContentPackage; } - public Collection<Artifact> getMatchingArtifacts(final MavenProject project) { + Collection<Artifact> getMatchingArtifacts(final MavenProject project, + RepositorySystem repoSystem, RepositorySystemSession repoSession) { // get artifacts depending on whether we exclude transitives or not final Set<Artifact> artifacts; // TODO: when I ran the tests the artifacts where only available in the Dependency Artifacts and @@ -90,13 +99,57 @@ public class ContentPackage { // all dependencies, transitives included artifacts = project.getArtifacts(); } + + // Sometimes artifacts don't have their 'file' attribute set, which we need. For those that don't, resolve them + final Set<Artifact> fileArtifacts = new HashSet<>(); + for (Artifact a : artifacts) { + if (a.getFile() != null) { + fileArtifacts.add(a); + } else { + if (repoSystem != null && repoSession != null) { + // Resolving the artifact via Aether will fill in the file attribute + Artifact fileArt = resolveArtifact(repoSystem, repoSession, a); + if (fileArt != null) { + fileArtifacts.add(fileArt); + } + } + } + } + // Add the project artifact itself to convert after building a content package if(moduleIsContentPackage) { Artifact projectArtifact = project.getArtifact(); System.out.println("Project Artifact: " + projectArtifact); - artifacts.add(projectArtifact); + fileArtifacts.add(projectArtifact); + } + return getMatchingArtifacts(fileArtifacts); + } + + private Artifact resolveArtifact(final RepositorySystem repoSystem, final RepositorySystemSession repoSession, + final Artifact artifact) { + try { + // Get an Aether Artifact + org.eclipse.aether.artifact.Artifact a = new org.eclipse.aether.artifact.DefaultArtifact( + artifact.getGroupId(), artifact.getArtifactId(), + artifact.getClassifier(), artifact.getType(), + artifact.getVersion()); + + ArtifactRequest req = new ArtifactRequest(a, null, null); + ArtifactResult res = repoSystem.resolveArtifact(repoSession, req); + + if (res.isResolved()) { + org.eclipse.aether.artifact.Artifact aetherArt = res.getArtifact(); + Artifact mavenArt = new DefaultArtifact( + aetherArt.getGroupId(), aetherArt.getArtifactId(), aetherArt.getVersion(), null, aetherArt.getExtension(), + aetherArt.getClassifier(), new DefaultArtifactHandler()); + mavenArt.setFile(aetherArt.getFile()); + return mavenArt; + } else { + return null; + } + } catch (RepositoryException e) { + throw new RuntimeException(e); } - return getMatchingArtifacts(artifacts); } public Collection<Artifact> getMatchingArtifacts(final Collection<Artifact> artifacts) { diff --git a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java index a7921c5..2c0fddd 100644 --- a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java +++ b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java @@ -17,11 +17,11 @@ package org.apache.sling.cpconverter.maven.mojos; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.ProjectBuildingRequest; @@ -32,6 +32,8 @@ import org.apache.sling.feature.cpconverter.artifacts.DefaultArtifactsDeployer; import org.apache.sling.feature.cpconverter.features.DefaultFeaturesManager; import org.apache.sling.feature.cpconverter.handlers.DefaultEntryHandlersManager; import org.apache.sling.feature.cpconverter.vltpkg.DefaultPackagesEventsEmitter; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; import java.io.File; import java.io.IOException; @@ -57,7 +59,7 @@ import static org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelCo requiresProject = true, threadSafe = true ) -public final class ConvertCPMojo +public class ConvertCPMojo extends AbstractBaseMojo { public static final String CFG_STRICT_VALIDATION = "strictValidation"; @@ -163,6 +165,12 @@ public final class ConvertCPMojo @Parameter(property = CFG_IS_CONTENT_PACKAGE, defaultValue = DEFAULT_IS_CONTENT_PACKAGE + "") private boolean isContentPackage; + @Parameter(defaultValue="${repositorySystemSession}") + private RepositorySystemSession repoSession; + + @Component + private RepositorySystem repoSystem; + @Override public void execute() throws MojoExecutionException, MojoFailureException { // Un-encode a given Artifact Override Id @@ -227,7 +235,7 @@ public final class ConvertCPMojo getLog().info("Content Package Artifact File: " + contentPackage.toString() + ", is module CP: " + isContentPackage); contentPackage.setExcludeTransitive(true); contentPackage.setModuleIsContentPackage(isContentPackage); - final Collection<Artifact> artifacts = contentPackage.getMatchingArtifacts(project); + final Collection<Artifact> artifacts = contentPackage.getMatchingArtifacts(project, repoSystem, repoSession); if (artifacts.isEmpty()) { getLog().warn("No matching artifacts for " + contentPackage); continue;
