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

Resolve plugins by Resolver API #529

Merged
merged 1 commit into from
Apr 2, 2024
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
10 changes: 5 additions & 5 deletions src/it/sigOkKeysMapWithPlugins/postbuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ assert buildLog.contains('[INFO] commons-chain:commons-chain:pom:1.2 PGP Signatu
assert buildLog.contains('[INFO] org.hamcrest:hamcrest-core:jar:1.3 PGP Signature OK')
assert buildLog.contains('[INFO] commons-chain:commons-chain:jar:1.2 PGP Signature OK')
assert buildLog.contains('[INFO] junit:junit:jar:4.12 PGP Signature OK')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-install-plugin:maven-plugin:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-install-plugin:jar:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-install-plugin:pom:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-deploy-plugin:maven-plugin:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-deploy-plugin:jar:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-deploy-plugin:pom:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-site-plugin:maven-plugin:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-site-plugin:jar:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-site-plugin:pom:.* PGP Signature OK$.*')
assert buildLog.matches('(?ms).*^\\[INFO\\] org.apache.maven.plugins:maven-clean-plugin:pom:.* PGP Signature OK$.*')
assert buildLog.contains('[INFO] org.apache.maven.plugins:maven-pmd-plugin:maven-plugin:3.12.0 PGP Signature OK')
assert buildLog.contains('[INFO] org.apache.maven.plugins:maven-pmd-plugin:jar:3.12.0 PGP Signature OK')
assert buildLog.contains('[INFO] org.apache.maven.plugins:maven-pmd-plugin:pom:3.12.0 PGP Signature OK')
assert buildLog.contains('[INFO] net.sourceforge.pmd:pmd-java:jar:6.15.0 PGP Signature OK')
assert buildLog.contains('[INFO] net.sourceforge.pmd:pmd-java:pom:6.15.0 PGP Signature OK')
Expand All @@ -41,6 +41,6 @@ assert buildLog.contains('[INFO] com.google.errorprone:error_prone_core:jar:2.3.
assert buildLog.contains('[INFO] com.google.errorprone:error_prone_core:pom:2.3.3 PGP Signature OK')
assert buildLog.contains('[INFO] com.uber.nullaway:nullaway:jar:0.7.8 PGP Signature OK')
assert buildLog.contains('[INFO] com.uber.nullaway:nullaway:pom:0.7.8 PGP Signature OK')
assert buildLog.contains('[INFO] org.apache.maven.plugins:maven-project-info-reports-plugin:maven-plugin:3.0.0 PGP Signature OK')
assert buildLog.contains('[INFO] org.apache.maven.plugins:maven-project-info-reports-plugin:jar:3.0.0 PGP Signature OK')
assert buildLog.contains('[INFO] org.apache.maven.plugins:maven-project-info-reports-plugin:pom:3.0.0 PGP Signature OK')
assert buildLog.contains('[INFO] BUILD SUCCESS')
318 changes: 155 additions & 163 deletions src/main/java/org/simplify4u/plugins/ArtifactResolver.java

Large diffs are not rendered by default.

14 changes: 4 additions & 10 deletions src/main/java/org/simplify4u/plugins/ShowMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

import javax.inject.Inject;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import lombok.AccessLevel;
import lombok.Setter;
Expand All @@ -44,7 +43,7 @@
* @since 1.10.0
*/
@Slf4j
@Mojo(name = ShowMojo.MOJO_NAME, requiresDirectInvocation = true, requiresOnline = true, requiresProject = false)
@Mojo(name = ShowMojo.MOJO_NAME, requiresOnline = true, requiresProject = false)
public class ShowMojo extends AbstractPGPMojo {

public static final String MOJO_NAME = "show";
Expand Down Expand Up @@ -78,16 +77,11 @@ protected String getMojoName() {
@Override
protected void executeConfiguredMojo() {

Set<Artifact> artifactsToCheck = new HashSet<>();
Artifact artifactToCheck = prepareArtifactToCheck();

artifactsToCheck.add(artifactResolver.resolveArtifact(artifactToCheck));
List<Artifact> resolveArtifacts = artifactResolver.resolveArtifact(artifactToCheck, showPom);

if (showPom && artifactToCheck.isResolved()) {
artifactsToCheck.add(artifactResolver.resolvePom(artifactToCheck));
}

Map<Artifact, Artifact> artifactMap = artifactResolver.resolveSignatures(artifactsToCheck);
Map<Artifact, Artifact> artifactMap = artifactResolver.resolveSignatures(resolveArtifacts);

Boolean result = artifactMap.entrySet().stream()
.map(this::processArtifact)
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/org/simplify4u/plugins/utils/MavenCompilerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package org.simplify4u.plugins.utils;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Plugin;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.model.Plugin;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;

import static java.util.Arrays.stream;
import static java.util.Collections.emptySet;
import static java.util.Objects.requireNonNull;

/**
* Utilities specific for org.apache.maven.plugins:maven-compiler-plugin.
Expand All @@ -35,8 +34,6 @@ public final class MavenCompilerUtils {
private static final String GROUPID = "org.apache.maven.plugins";
private static final String ARTIFACTID = "maven-compiler-plugin";

private static final String PACKAGING = "jar";

private MavenCompilerUtils() {
// No need to instantiate utility class.
}
Expand All @@ -54,12 +51,10 @@ public static boolean checkCompilerPlugin(Plugin plugin) {
/**
* Extract annotation processors for maven-compiler-plugin configuration.
*
* @param system maven repository system
* @param plugin maven-compiler-plugin plugin
* @return Returns set of maven artifacts configured as annotation processors.
*/
public static Set<Artifact> extractAnnotationProcessors(RepositorySystem system, Plugin plugin) {
requireNonNull(system);
public static Set<Artifact> extractAnnotationProcessors(Plugin plugin) {
if (!checkCompilerPlugin(plugin)) {
throw new IllegalArgumentException("Plugin is not '" + GROUPID + ":" + ARTIFACTID + "'.");
}
Expand All @@ -70,11 +65,12 @@ public static Set<Artifact> extractAnnotationProcessors(RepositorySystem system,
if (config instanceof Xpp3Dom) {
return stream(((Xpp3Dom) config).getChildren("annotationProcessorPaths"))
.flatMap(aggregate -> stream(aggregate.getChildren("path")))
.map(processor -> system.createArtifact(
.map(processor -> new DefaultArtifact(
extractChildValue(processor, "groupId"),
extractChildValue(processor, "artifactId"),
extractChildValue(processor, "version"),
PACKAGING))
"",
"jar",
extractChildValue(processor, "version")))
// A path specification is automatically ignored in maven-compiler-plugin if version is absent,
// therefore there is little use in logging incomplete paths that are filtered out.
.filter(a -> !a.getGroupId().isEmpty())
Expand Down
25 changes: 8 additions & 17 deletions src/test/java/org/simplify4u/plugins/ArtifactResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.assertj.core.api.Condition;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
Expand All @@ -55,7 +54,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
Expand All @@ -67,14 +65,11 @@ class ArtifactResolverTest {
private static final Condition<Artifact> IS_JAR_TYPE = new Condition<>(a -> "jar".equals(a.getType()), "is jar type");
private static final Condition<Artifact> IS_POM_TYPE = new Condition<>(a -> "pom".equals(a.getType()), "is pom type");

@Mock
private RepositorySystem repositorySystem;

@Mock
private org.eclipse.aether.RepositorySystem aetherRepositorySystem;

@Mock
private ArtifactRepository localRepository;
private RepositorySystemSession repositorySession;

@Mock
private MavenSession session;
Expand All @@ -86,31 +81,27 @@ class ArtifactResolverTest {

@BeforeEach
void setup() {
when(session.getLocalRepository()).thenReturn(localRepository);
when(session.getCurrentProject()).thenReturn(project);
when(project.getRemoteArtifactRepositories()).thenReturn(emptyList());

resolver = new ArtifactResolver(repositorySystem, session, aetherRepositorySystem);
when(session.getRepositorySession()).thenReturn(repositorySession);
resolver = new ArtifactResolver(session, aetherRepositorySystem);
}

@Test
void testConstructArtifactResolverWithNull() {

reset(session, project);

assertThatCode(() -> new ArtifactResolver(null, null, null))
assertThatCode(() -> new ArtifactResolver(null, null))
.isExactlyInstanceOf(NullPointerException.class);

assertThatCode(() -> new ArtifactResolver(null, session, null))
assertThatCode(() -> new ArtifactResolver(session, null))
.isExactlyInstanceOf(NullPointerException.class);

doThrow(new NullPointerException()).when(session).getLocalRepository();
assertThatCode(() -> new ArtifactResolver(repositorySystem, session, null))
assertThatCode(() -> new ArtifactResolver(session, null))
.isExactlyInstanceOf(NullPointerException.class);

doReturn(localRepository).when(session).getLocalRepository();
doThrow(new NullPointerException()).when(session).getCurrentProject();
assertThatCode(() -> new ArtifactResolver(repositorySystem, session, null))
assertThatCode(() -> new ArtifactResolver(session, null))
.isExactlyInstanceOf(NullPointerException.class);
}

Expand Down
9 changes: 4 additions & 5 deletions src/test/java/org/simplify4u/plugins/ShowMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void shouldProcessArtifact() throws MojoFailureException, MojoExecutionException

// then
verify(repositorySystem).createArtifactWithClassifier("groupId", "artifactId", "1.0.0", "war", null);
verify(artifactResolver).resolveArtifact(artifact);
verify(artifactResolver).resolveArtifact(artifact, false);
verify(artifactResolver).resolveSignatures(anyCollection());

verify(signatureUtils).checkSignature(artifact, artifactAsc, pgpKeysCache);
Expand Down Expand Up @@ -149,8 +149,7 @@ void shouldProcessArtifactWithPom() throws MojoFailureException, MojoExecutionEx

// then
verify(repositorySystem).createArtifactWithClassifier("groupId", "artifactId", "1.0.0", "war", null);
verify(artifactResolver).resolveArtifact(artifact);
verify(artifactResolver).resolvePom(artifact);
verify(artifactResolver).resolveArtifact(artifact, true);
verify(artifactResolver).resolveSignatures(anyCollection());

verify(pgpKeysCache).init(any(), any());
Expand Down Expand Up @@ -187,7 +186,7 @@ void shouldFailForNotResolvedArtifact() throws IOException {

// then
verify(repositorySystem).createArtifactWithClassifier("groupId", "artifactId", "1.0.0", "war", null);
verify(artifactResolver).resolveArtifact(artifact);
verify(artifactResolver).resolveArtifact(artifact, false);
verify(artifactResolver).resolveSignatures(anyCollection());

verify(signatureUtils, times(2)).keyAlgorithmName(anyInt());
Expand Down Expand Up @@ -228,7 +227,7 @@ void shouldFailForNotResolvedSignature() throws MojoExecutionException, IOExcept

// then
verify(repositorySystem).createArtifactWithClassifier("groupId", "artifactId", "1.0.0", "war", null);
verify(artifactResolver).resolveArtifact(artifact);
verify(artifactResolver).resolveArtifact(artifact, false);
verify(artifactResolver).resolveSignatures(anyCollection());

verify(signatureUtils).keyAlgorithmName(anyInt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,32 @@ void testCheckCompilerPlugin() {

@Test
void testExtractAnnotationProcessorsIllegalInputs() {
assertThrows(NullPointerException.class, () -> extractAnnotationProcessors(null, null));
assertThrows(NullPointerException.class, () -> extractAnnotationProcessors(null));
final Plugin badPlugin = mock(Plugin.class);
when(badPlugin.getGroupId()).thenReturn("org.my-bad-plugin");
when(badPlugin.getArtifactId()).thenReturn("bad-plugin");
when(badPlugin.getVersion()).thenReturn("1.1.1");
assertThrows(NullPointerException.class, () -> extractAnnotationProcessors(null, badPlugin));
final RepositorySystem repository = mock(RepositorySystem.class);
assertThrows(NullPointerException.class, () -> extractAnnotationProcessors(repository, null));
assertThrows(IllegalArgumentException.class, () -> extractAnnotationProcessors(repository, badPlugin));
assertThrows(NullPointerException.class, () -> extractAnnotationProcessors(null));
assertThrows(IllegalArgumentException.class, () -> extractAnnotationProcessors( badPlugin));
}

@Test
void testExtractAnnotationProcessorsNoConfiguration() {
final RepositorySystem repository = mock(RepositorySystem.class);
final Plugin plugin = mock(Plugin.class);
when(plugin.getGroupId()).thenReturn("org.apache.maven.plugins");
when(plugin.getArtifactId()).thenReturn("maven-compiler-plugin");
when(plugin.getVersion()).thenReturn("3.8.1");
assertEquals(emptySet(), extractAnnotationProcessors(repository, plugin));
assertEquals(emptySet(), extractAnnotationProcessors(plugin));
}

@Test
void testExtractAnnotationProcessorsUnsupportedConfigurationType() {
final RepositorySystem repository = mock(RepositorySystem.class);
final Plugin plugin = mock(Plugin.class);
when(plugin.getGroupId()).thenReturn("org.apache.maven.plugins");
when(plugin.getArtifactId()).thenReturn("maven-compiler-plugin");
when(plugin.getVersion()).thenReturn("3.8.1");
when(plugin.getConfiguration()).thenReturn("Massive configuration encoded in magic \"Hello World!\" string.");
assertThrows(UnsupportedOperationException.class, () -> extractAnnotationProcessors(repository, plugin));
assertThrows(UnsupportedOperationException.class, () -> extractAnnotationProcessors(plugin));
}

@Test
Expand All @@ -98,9 +94,9 @@ void testExtractAnnotationProcessors() {
when(artifact.getVersion()).thenReturn(invocation.getArgument(2));
return artifact;
});
final Set<Artifact> result = extractAnnotationProcessors(repository, plugin);
final Set<org.eclipse.aether.artifact.Artifact> result = extractAnnotationProcessors(plugin);
assertEquals(1, result.size());
final Artifact resultElement = result.iterator().next();
final org.eclipse.aether.artifact.Artifact resultElement = result.iterator().next();
assertEquals("myGroupId", resultElement.getGroupId());
assertEquals("myArtifactId", resultElement.getArtifactId());
assertEquals("1.2.3", resultElement.getVersion());
Expand Down