Skip to content

Commit

Permalink
Use Maven Resolver API
Browse files Browse the repository at this point in the history
Increase Maven requirement to 3.5.0 (Resolver 1.0.3)
Enforce minimum Maven version via prerequisite

This closes #223
  • Loading branch information
kwin committed Apr 8, 2023
1 parent beaaa9c commit 09cb592
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 47 deletions.
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@
<exclude name="**/jdk-deprecated-*.txt"/>
<exclude name="**/jdk-internal-*.txt"/>
<exclude name="**/*.iml"/>
<exclude name="**/.*"/>
</fileset>
</rat:report>
<!-- now print the output, for review -->
Expand Down
5 changes: 4 additions & 1 deletion ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* limitations under the License.
-->
<!DOCTYPE ivy-module [
<!ENTITY maven.version "2.0">
<!ENTITY maven.version "3.5.0">
<!ENTITY resolver.version "1.0.3">
<!ENTITY gradle.version "3.4">
<!ENTITY asm.version "9.5">
<!ENTITY jarjar.asm.version "5.2">
Expand All @@ -35,6 +36,8 @@
<dependency org="org.apache.ant" name="ant" rev="1.7.0" conf="build"/>
<dependency org="org.apache.maven" name="maven-plugin-api" rev="&maven.version;" conf="build"/>
<dependency org="org.apache.maven" name="maven-artifact" rev="&maven.version;" conf="build"/>
<dependency org="org.apache.maven.resolver" name="maven-resolver-api" rev="&resolver.version;" conf="build"/>
<dependency org="org.apache.maven.resolver" name="maven-resolver-util" rev="&resolver.version;" conf="build"/>
<dependency org="org.apache.maven.plugin-tools" name="maven-plugin-annotations" rev="3.4" conf="build"/>
<dependency org="org.slf4j" name="slf4j-api" rev="1.7.7" conf="build"/>
<dependency org="dev.gradleplugins" name="gradle-api" rev="&gradle.version;" conf="buildgradle"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,46 @@

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;
import java.util.List;
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
Expand Down Expand Up @@ -237,18 +242,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<ArtifactRepository> 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<RemoteRepository> projectRepos;

/** provided by the concrete Mojos for compile and test classes processing */
protected abstract List<String> getClassPathElements();
Expand All @@ -261,15 +263,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) {
Expand Down Expand Up @@ -454,8 +452,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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
6 changes: 5 additions & 1 deletion src/main/maven/pom-build.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
<description>@DESCRIPTION@</description>
<packaging>maven-plugin</packaging>

<prerequisites>
<maven>${injected.maven.version}</maven>
</prerequisites>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
</properties>

<build>
<directory>${injected.build.dir}</directory>
<sourceDirectory>${injected.src.dir}</sourceDirectory>
Expand Down
4 changes: 4 additions & 0 deletions src/main/maven/pom-deploy.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@
<timezone>-5</timezone>
</developer>
</developers>

<prerequisites>
<maven>${injected.maven.version}</maven>
</prerequisites>
</project>

0 comments on commit 09cb592

Please sign in to comment.