Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Maven Resolver API #230

Merged
merged 2 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,7 @@
<exclude name="**/jdk-deprecated-*.txt"/>
<exclude name="**/jdk-internal-*.txt"/>
<exclude name="**/*.iml"/>
<exclude name="**/.project"/>
<exclude name="**/.settings"/>
<exclude name="**/.*"/>
</fileset>
</rat:report>
<!-- now print the output, for review -->
Expand Down
6 changes: 4 additions & 2 deletions 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">
uschindler marked this conversation as resolved.
Show resolved Hide resolved
<!ENTITY resolver.version "1.0.3">
<!ENTITY gradle.version "3.4">
<!ENTITY asm.version "9.5">
<!ENTITY jarjar.asm.version "5.2">
Expand All @@ -34,7 +35,8 @@
<!-- we compile against the minimum ANT / Maven / Gradle versions: -->
<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 @@ -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<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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that ${project.remotePluginRepositories} would be semantically correct, but you always used the project repository to resolve signature artifacts.

private List<RemoteRepository> projectRepos;

/** provided by the concrete Mojos for compile and test classes processing */
protected abstract List<String> getClassPathElements();
Expand All @@ -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) {
Expand Down Expand Up @@ -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()) {
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as long as type = extension no additional mapper needed, otherwise you need to inject https://maven.apache.org/ref/3.5.0/maven-core/apidocs/org/apache/maven/artifact/handler/manager/ArtifactHandlerManager.html and retrieve the ArtifactHandler for the given type to resolve its extension.

}
}
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>