From f7d2581009fb3f68ddc58c4c873012ff048b5b27 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sat, 20 May 2023 20:12:58 +0200 Subject: [PATCH] Use Maven Resolver API (#230) Increase Maven requirement to 3.5.0 (Resolver 1.0.3); enforce minimum Maven version via prerequisite This closes #223 Co-authored-by: Uwe Schindler --- build.xml | 3 +- ivy.xml | 6 +- .../maven/AbstractCheckMojo.java | 78 +++++++++---------- .../maven/SignaturesArtifact.java | 8 +- src/main/maven/pom-build.xml.template | 6 +- src/main/maven/pom-deploy.xml.template | 4 + 6 files changed, 55 insertions(+), 50 deletions(-) diff --git a/build.xml b/build.xml index d1b8b958..a411c0d4 100644 --- a/build.xml +++ b/build.xml @@ -672,8 +672,7 @@ - - + diff --git a/ivy.xml b/ivy.xml index 99cde781..3e793510 100644 --- a/ivy.xml +++ b/ivy.xml @@ -15,7 +15,8 @@ * limitations under the License. --> + + @@ -34,7 +35,8 @@ - + + diff --git a/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java b/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java index 02b74ee9..59facbc9 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java +++ b/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java @@ -16,34 +16,20 @@ package de.thetaphi.forbiddenapis.maven; -import static de.thetaphi.forbiddenapis.Checker.Option.*; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; -import org.apache.maven.artifact.resolver.ArtifactResolver; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.plexus.util.DirectoryScanner; - -import de.thetaphi.forbiddenapis.Checker; -import de.thetaphi.forbiddenapis.Constants; -import de.thetaphi.forbiddenapis.ForbiddenApiException; -import de.thetaphi.forbiddenapis.Logger; -import de.thetaphi.forbiddenapis.ParseException; +import static de.thetaphi.forbiddenapis.Checker.Option.DISABLE_CLASSLOADING_CACHE; +import static de.thetaphi.forbiddenapis.Checker.Option.FAIL_ON_MISSING_CLASSES; +import static de.thetaphi.forbiddenapis.Checker.Option.FAIL_ON_UNRESOLVABLE_SIGNATURES; +import static de.thetaphi.forbiddenapis.Checker.Option.FAIL_ON_VIOLATION; +import static de.thetaphi.forbiddenapis.Checker.Option.IGNORE_SIGNATURES_OF_MISSING_CLASSES; import java.io.File; import java.io.IOException; import java.lang.annotation.RetentionPolicy; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; -import java.net.URLClassLoader; -import java.net.MalformedURLException; import java.net.URL; +import java.net.URLClassLoader; import java.util.Arrays; import java.util.EnumSet; import java.util.LinkedHashSet; @@ -51,6 +37,25 @@ import java.util.Locale; import java.util.Set; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.Parameter; +import org.codehaus.plexus.util.DirectoryScanner; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; + +import de.thetaphi.forbiddenapis.Checker; +import de.thetaphi.forbiddenapis.Constants; +import de.thetaphi.forbiddenapis.ForbiddenApiException; +import de.thetaphi.forbiddenapis.Logger; +import de.thetaphi.forbiddenapis.ParseException; + /** * Base class for forbiddenapis Mojos. * @since 1.0 @@ -242,18 +247,15 @@ public abstract class AbstractCheckMojo extends AbstractMojo implements Constant /** The project packaging (pom, jar, etc.). */ @Parameter(defaultValue = "${project.packaging}", readonly = true, required = true) private String packaging; - - @Component - private ArtifactFactory artifactFactory; @Component - private ArtifactResolver artifactResolver; - - @Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true) - private List remoteRepositories; + private RepositorySystem repoSystem; - @Parameter(defaultValue = "${localRepository}", readonly = true, required = true) - private ArtifactRepository localRepository; + @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true) + private RepositorySystemSession repoSession; + + @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true) + private List projectRepos; /** provided by the concrete Mojos for compile and test classes processing */ protected abstract List getClassPathElements(); @@ -266,15 +268,11 @@ protected String getTargetVersion() { return (releaseVersion != null) ? releaseVersion : targetVersion; } - private File resolveSignaturesArtifact(SignaturesArtifact signaturesArtifact) throws ArtifactResolutionException, ArtifactNotFoundException { - final Artifact artifact = signaturesArtifact.createArtifact(artifactFactory); - artifactResolver.resolve(artifact, this.remoteRepositories, this.localRepository); - final File f = artifact.getFile(); - // Can this ever be false? Be sure. Found the null check also in other Maven code, so be safe! - if (f == null) { - throw new ArtifactNotFoundException("Artifact does not resolve to a file.", artifact); - } - return f; + private File resolveSignaturesArtifact(SignaturesArtifact signaturesArtifact) throws ArtifactResolutionException { + final Artifact artifact = signaturesArtifact.createArtifact(); + ArtifactRequest req = new ArtifactRequest(artifact, projectRepos, null); + ArtifactResult resolutionResult = repoSystem.resolveArtifact(repoSession, req); + return resolutionResult.getArtifact().getFile(); } private String encodeUrlPath(String path) { @@ -459,8 +457,6 @@ public void debug(String msg) { throw new MojoExecutionException("Parsing signatures failed: " + pe.getMessage(), pe); } catch (ArtifactResolutionException e) { throw new MojoExecutionException("Problem while resolving Maven artifact.", e); - } catch (ArtifactNotFoundException e) { - throw new MojoExecutionException("Maven artifact does not exist.", e); } if (checker.hasNoSignatures()) { diff --git a/src/main/java/de/thetaphi/forbiddenapis/maven/SignaturesArtifact.java b/src/main/java/de/thetaphi/forbiddenapis/maven/SignaturesArtifact.java index b02bc1ec..504dc92d 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/maven/SignaturesArtifact.java +++ b/src/main/java/de/thetaphi/forbiddenapis/maven/SignaturesArtifact.java @@ -16,8 +16,8 @@ package de.thetaphi.forbiddenapis.maven; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.DefaultArtifact; /** * Defines coordinates of a Maven artifact that provides signatures files. @@ -51,10 +51,10 @@ public final class SignaturesArtifact { public String path; /** Used by the mojo to fetch the artifact */ - Artifact createArtifact(ArtifactFactory artifactFactory) { + Artifact createArtifact() { if (groupId == null || artifactId == null || version == null || type == null) { throw new NullPointerException("signaturesArtifact is missing some properties. Required are: groupId, artifactId, version, type"); } - return artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier); + return new DefaultArtifact(groupId, artifactId, classifier, type, version); } } diff --git a/src/main/maven/pom-build.xml.template b/src/main/maven/pom-build.xml.template index fec0a25f..37520a60 100644 --- a/src/main/maven/pom-build.xml.template +++ b/src/main/maven/pom-build.xml.template @@ -24,11 +24,15 @@ @DESCRIPTION@ maven-plugin + + ${injected.maven.version} + + UTF-8 UTF-8 - + ${injected.build.dir} ${injected.src.dir} diff --git a/src/main/maven/pom-deploy.xml.template b/src/main/maven/pom-deploy.xml.template index c9d009e2..504ff889 100644 --- a/src/main/maven/pom-deploy.xml.template +++ b/src/main/maven/pom-deploy.xml.template @@ -76,4 +76,8 @@ -5 + + + ${injected.maven.version} +