Skip to content

Commit 6e688fb

Browse files
committed
Use Maven Resolver API
Increase Maven requirement to 3.5.0 (Resolver 1.0.3) Enforce minimum Maven version via prerequisite This closes #223
1 parent beaaa9c commit 6e688fb

File tree

6 files changed

+55
-48
lines changed

6 files changed

+55
-48
lines changed

build.xml

+1
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@
672672
<exclude name="**/jdk-deprecated-*.txt"/>
673673
<exclude name="**/jdk-internal-*.txt"/>
674674
<exclude name="**/*.iml"/>
675+
<exclude name="**/.*"/>
675676
</fileset>
676677
</rat:report>
677678
<!-- now print the output, for review -->

ivy.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* limitations under the License.
1616
-->
1717
<!DOCTYPE ivy-module [
18-
<!ENTITY maven.version "2.0">
18+
<!ENTITY maven.version "3.5.0">
19+
<!ENTITY resolver.version "1.0.3">
1920
<!ENTITY gradle.version "3.4">
2021
<!ENTITY asm.version "9.5">
2122
<!ENTITY jarjar.asm.version "5.2">
@@ -34,7 +35,8 @@
3435
<!-- we compile against the minimum ANT / Maven / Gradle versions: -->
3536
<dependency org="org.apache.ant" name="ant" rev="1.7.0" conf="build"/>
3637
<dependency org="org.apache.maven" name="maven-plugin-api" rev="&maven.version;" conf="build"/>
37-
<dependency org="org.apache.maven" name="maven-artifact" rev="&maven.version;" conf="build"/>
38+
<dependency org="org.apache.maven.resolver" name="maven-resolver-api" rev="&resolver.version;" conf="build"/>
39+
<dependency org="org.apache.maven.resolver" name="maven-resolver-util" rev="&resolver.version;" conf="build"/>
3840
<dependency org="org.apache.maven.plugin-tools" name="maven-plugin-annotations" rev="3.4" conf="build"/>
3941
<dependency org="org.slf4j" name="slf4j-api" rev="1.7.7" conf="build"/>
4042
<dependency org="dev.gradleplugins" name="gradle-api" rev="&gradle.version;" conf="buildgradle"/>

src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java

+37-41
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,46 @@
1616

1717
package de.thetaphi.forbiddenapis.maven;
1818

19-
import static de.thetaphi.forbiddenapis.Checker.Option.*;
20-
21-
import org.apache.maven.artifact.Artifact;
22-
import org.apache.maven.artifact.factory.ArtifactFactory;
23-
import org.apache.maven.artifact.repository.ArtifactRepository;
24-
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
25-
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
26-
import org.apache.maven.artifact.resolver.ArtifactResolver;
27-
import org.apache.maven.plugin.AbstractMojo;
28-
import org.apache.maven.plugin.MojoExecutionException;
29-
import org.apache.maven.plugins.annotations.Component;
30-
import org.apache.maven.plugins.annotations.Parameter;
31-
import org.codehaus.plexus.util.DirectoryScanner;
32-
33-
import de.thetaphi.forbiddenapis.Checker;
34-
import de.thetaphi.forbiddenapis.Constants;
35-
import de.thetaphi.forbiddenapis.ForbiddenApiException;
36-
import de.thetaphi.forbiddenapis.Logger;
37-
import de.thetaphi.forbiddenapis.ParseException;
19+
import static de.thetaphi.forbiddenapis.Checker.Option.DISABLE_CLASSLOADING_CACHE;
20+
import static de.thetaphi.forbiddenapis.Checker.Option.FAIL_ON_MISSING_CLASSES;
21+
import static de.thetaphi.forbiddenapis.Checker.Option.FAIL_ON_UNRESOLVABLE_SIGNATURES;
22+
import static de.thetaphi.forbiddenapis.Checker.Option.FAIL_ON_VIOLATION;
23+
import static de.thetaphi.forbiddenapis.Checker.Option.IGNORE_SIGNATURES_OF_MISSING_CLASSES;
3824

3925
import java.io.File;
4026
import java.io.IOException;
4127
import java.lang.annotation.RetentionPolicy;
28+
import java.net.MalformedURLException;
4229
import java.net.URI;
4330
import java.net.URISyntaxException;
44-
import java.net.URLClassLoader;
45-
import java.net.MalformedURLException;
4631
import java.net.URL;
32+
import java.net.URLClassLoader;
4733
import java.util.Arrays;
4834
import java.util.EnumSet;
4935
import java.util.LinkedHashSet;
5036
import java.util.List;
5137
import java.util.Locale;
5238
import java.util.Set;
5339

40+
import org.apache.maven.plugin.AbstractMojo;
41+
import org.apache.maven.plugin.MojoExecutionException;
42+
import org.apache.maven.plugins.annotations.Component;
43+
import org.apache.maven.plugins.annotations.Parameter;
44+
import org.codehaus.plexus.util.DirectoryScanner;
45+
import org.eclipse.aether.RepositorySystem;
46+
import org.eclipse.aether.RepositorySystemSession;
47+
import org.eclipse.aether.artifact.Artifact;
48+
import org.eclipse.aether.repository.RemoteRepository;
49+
import org.eclipse.aether.resolution.ArtifactRequest;
50+
import org.eclipse.aether.resolution.ArtifactResolutionException;
51+
import org.eclipse.aether.resolution.ArtifactResult;
52+
53+
import de.thetaphi.forbiddenapis.Checker;
54+
import de.thetaphi.forbiddenapis.Constants;
55+
import de.thetaphi.forbiddenapis.ForbiddenApiException;
56+
import de.thetaphi.forbiddenapis.Logger;
57+
import de.thetaphi.forbiddenapis.ParseException;
58+
5459
/**
5560
* Base class for forbiddenapis Mojos.
5661
* @since 1.0
@@ -237,18 +242,15 @@ public abstract class AbstractCheckMojo extends AbstractMojo implements Constant
237242
/** The project packaging (pom, jar, etc.). */
238243
@Parameter(defaultValue = "${project.packaging}", readonly = true, required = true)
239244
private String packaging;
240-
241-
@Component
242-
private ArtifactFactory artifactFactory;
243245

244246
@Component
245-
private ArtifactResolver artifactResolver;
246-
247-
@Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true)
248-
private List<ArtifactRepository> remoteRepositories;
247+
private RepositorySystem repoSystem;
249248

250-
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
251-
private ArtifactRepository localRepository;
249+
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
250+
private RepositorySystemSession repoSession;
251+
252+
@Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
253+
private List<RemoteRepository> projectRepos;
252254

253255
/** provided by the concrete Mojos for compile and test classes processing */
254256
protected abstract List<String> getClassPathElements();
@@ -261,15 +263,11 @@ protected String getTargetVersion() {
261263
return (releaseVersion != null) ? releaseVersion : targetVersion;
262264
}
263265

264-
private File resolveSignaturesArtifact(SignaturesArtifact signaturesArtifact) throws ArtifactResolutionException, ArtifactNotFoundException {
265-
final Artifact artifact = signaturesArtifact.createArtifact(artifactFactory);
266-
artifactResolver.resolve(artifact, this.remoteRepositories, this.localRepository);
267-
final File f = artifact.getFile();
268-
// Can this ever be false? Be sure. Found the null check also in other Maven code, so be safe!
269-
if (f == null) {
270-
throw new ArtifactNotFoundException("Artifact does not resolve to a file.", artifact);
271-
}
272-
return f;
266+
private File resolveSignaturesArtifact(SignaturesArtifact signaturesArtifact) throws ArtifactResolutionException {
267+
final Artifact artifact = signaturesArtifact.createArtifact();
268+
ArtifactRequest req = new ArtifactRequest(artifact, projectRepos, null);
269+
ArtifactResult resolutionResult = repoSystem.resolveArtifact(repoSession, req);
270+
return resolutionResult.getArtifact().getFile();
273271
}
274272

275273
private String encodeUrlPath(String path) {
@@ -454,8 +452,6 @@ public void debug(String msg) {
454452
throw new MojoExecutionException("Parsing signatures failed: " + pe.getMessage(), pe);
455453
} catch (ArtifactResolutionException e) {
456454
throw new MojoExecutionException("Problem while resolving Maven artifact.", e);
457-
} catch (ArtifactNotFoundException e) {
458-
throw new MojoExecutionException("Maven artifact does not exist.", e);
459455
}
460456

461457
if (checker.hasNoSignatures()) {

src/main/java/de/thetaphi/forbiddenapis/maven/SignaturesArtifact.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package de.thetaphi.forbiddenapis.maven;
1818

19-
import org.apache.maven.artifact.Artifact;
20-
import org.apache.maven.artifact.factory.ArtifactFactory;
19+
import org.eclipse.aether.artifact.Artifact;
20+
import org.eclipse.aether.artifact.DefaultArtifact;
2121

2222
/**
2323
* Defines coordinates of a Maven artifact that provides signatures files.
@@ -51,10 +51,10 @@ public final class SignaturesArtifact {
5151
public String path;
5252

5353
/** Used by the mojo to fetch the artifact */
54-
Artifact createArtifact(ArtifactFactory artifactFactory) {
54+
Artifact createArtifact() {
5555
if (groupId == null || artifactId == null || version == null || type == null) {
5656
throw new NullPointerException("signaturesArtifact is missing some properties. Required are: groupId, artifactId, version, type");
5757
}
58-
return artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
58+
return new DefaultArtifact(groupId, artifactId, classifier, type, version);
5959
}
6060
}

src/main/maven/pom-build.xml.template

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
<description>@DESCRIPTION@</description>
2525
<packaging>maven-plugin</packaging>
2626

27+
<prerequisites>
28+
<maven>${injected.maven.version}</maven>
29+
</prerequisites>
30+
2731
<properties>
2832
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2933
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
3034
</properties>
31-
35+
3236
<build>
3337
<directory>${injected.build.dir}</directory>
3438
<sourceDirectory>${injected.src.dir}</sourceDirectory>

src/main/maven/pom-deploy.xml.template

+4
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@
7676
<timezone>-5</timezone>
7777
</developer>
7878
</developers>
79+
80+
<prerequisites>
81+
<maven>${injected.maven.version}</maven>
82+
</prerequisites>
7983
</project>

0 commit comments

Comments
 (0)