diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java b/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java index 976471b..64d3ec6 100644 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java +++ b/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java @@ -148,7 +148,7 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo { * @since 5.0 */ @Parameter - List annotationProcessorPaths; + List annotationProcessorPaths; /** * Annotation Processor FQN (Full Qualified Name) - when processors are not specified, the default discovery mechanism will be used @@ -241,7 +241,7 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo { * The entry point to Aether, i.e. the component doing all the work. */ @Component - protected RepositorySystem repoSystem; + private RepositorySystem repoSystem; /** * The current repository/network configuration of Maven. @@ -253,7 +253,7 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo { * The project's remote repositories to use for the resolution of plugins and their dependencies. */ @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true) - protected List remoteRepos; + private List remoteRepos; /** * List of artifacts on which perform sources scanning @@ -545,8 +545,7 @@ private List prepareOptions(JavaCompiler compiler) throws MojoExecutionE options.add("-proc:only"); - Optional processorPath = this.buildProcessorPath(); - processorPath.ifPresent(value -> { + this.buildProcessorPath().ifPresent(value -> { options.add("-processorpath"); options.add(value); }); @@ -997,52 +996,58 @@ private void processSourceArtifacts(Consumer closure) { } } - private Optional> resolveProcessorPathEntries() throws MojoExecutionException { - if (this.annotationProcessorPaths == null || this.annotationProcessorPaths.isEmpty()) { - return Optional.empty(); - } + private Optional> resolveProcessorPathEntries() { + if (this.annotationProcessorPaths != null && !this.annotationProcessorPaths.isEmpty() ) { - try { - Set requiredDependencies = new LinkedHashSet<>(); - - for (DependencyCoordinate coord : this.annotationProcessorPaths) { - //@formatter:off - DefaultArtifact artifact = new DefaultArtifact( - coord.getGroupId(), - coord.getArtifactId(), - coord.getClassifier(), - coord.getType(), - coord.getVersion()); - //@formatter:on - - requiredDependencies.add(new Dependency(artifact, Artifact.SCOPE_RUNTIME)); - } - CollectRequest collectRequest = new CollectRequest(requiredDependencies.iterator().next(), - new ArrayList<>(requiredDependencies), this.remoteRepos); - DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null); + try { - DependencyResult resolutionResult = this.repoSystem.resolveDependencies( this.repoSession, - dependencyRequest); + final List requiredDependencies = + this.annotationProcessorPaths.stream() + .map(dependency -> new DefaultArtifact( + dependency.getGroupId(), + dependency.getArtifactId(), + dependency.getClassifier(), + dependency.getType(), + dependency.getVersion())) + .distinct() + .map(artifact -> new Dependency(artifact, Artifact.SCOPE_RUNTIME)) + .collect(Collectors.toList()); - List artifactPaths = new ArrayList<>(resolutionResult.getArtifactResults().size()); - for (ArtifactResult artifactResult : resolutionResult.getArtifactResults()) { - artifactPaths.add(artifactResult.getArtifact().getFile().getAbsolutePath()); - } + final Dependency root = requiredDependencies.get(0); + + final CollectRequest collectRequest = + new CollectRequest(root, requiredDependencies, this.remoteRepos); + + final DependencyRequest dependencyRequest = + new DependencyRequest(collectRequest, null); - return Optional.of(artifactPaths); - } catch (Exception e) { - throw new MojoExecutionException( - "Resolution of annotationProcessorPath dependencies failed: " + e.getLocalizedMessage(), e); + final DependencyResult resolutionResult = + this.repoSystem.resolveDependencies(this.repoSession, dependencyRequest); + + final List artifactPaths = + resolutionResult.getArtifactResults().stream() + .map(artifactResult -> artifactResult.getArtifact().getFile().getAbsolutePath()) + .collect(Collectors.toList()); + + return Optional.of(artifactPaths); + + } catch (Exception e) { + + final String msg = format("Resolution of annotationProcessorPath dependencies failed: %s", e.getLocalizedMessage()); + getLog().error(msg, e); + } } + return empty(); + } /** * * @return - * @throws MojoExecutionException */ - Optional buildProcessorPath() throws MojoExecutionException { - Optional> processorPathEntries = this.resolveProcessorPathEntries(); - return processorPathEntries.map(value -> StringUtils.join(value.iterator(), File.pathSeparator)); + protected Optional buildProcessorPath() { + return this.resolveProcessorPathEntries() + .flatMap( artifactPaths -> + Optional.of(artifactPaths.stream().collect(Collectors.joining(File.separator))) ); } } diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/DependencyCoordinate.java b/processor/src/main/java/org/bsc/maven/plugin/processor/DependencyCoordinate.java deleted file mode 100644 index d7916e8..0000000 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/DependencyCoordinate.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.bsc.maven.plugin.processor; - -import java.util.Objects; - -/** - * Maven-coordinates of a dependency. - * - * @author Ulysses R. Ribeiro - * - */ -public class DependencyCoordinate { - - private String groupId; - - private String artifactId; - - private String version; - - private String classifier; - - private String type = "jar"; - - public String getGroupId() { - return this.groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - public String getArtifactId() { - return this.artifactId; - } - - public void setArtifactId(String artifactId) { - this.artifactId = artifactId; - } - - public String getVersion() { - return this.version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getClassifier() { - return this.classifier; - } - - public void setClassifier(String classifier) { - this.classifier = classifier; - } - - public String getType() { - return this.type; - } - - public void setType(String type) { - this.type = type; - } - - @Override - public int hashCode() { - return Objects.hash(artifactId, classifier, groupId, type, version); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - DependencyCoordinate other = (DependencyCoordinate) obj; - return Objects.equals(artifactId, other.artifactId) && Objects.equals(classifier, other.classifier) - && Objects.equals(groupId, other.groupId) && Objects.equals(type, other.type) - && Objects.equals(version, other.version); - } - - @Override - public String toString() { - return "DependencyCoordinate [groupId=" + groupId + ", artifactId=" + artifactId + ", version=" + version - + ", classifier=" + classifier + ", type=" + type + "]"; - } - -} diff --git a/processor/src/test/java/org/bsc/maven/plugin/processor/AnnotationProcessorMojoTest.java b/processor/src/test/java/org/bsc/maven/plugin/processor/AnnotationProcessorMojoTest.java index f2eed8f..102c31e 100644 --- a/processor/src/test/java/org/bsc/maven/plugin/processor/AnnotationProcessorMojoTest.java +++ b/processor/src/test/java/org/bsc/maven/plugin/processor/AnnotationProcessorMojoTest.java @@ -2,6 +2,7 @@ import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Dependency; import org.apache.maven.plugin.Mojo; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.testing.MojoRule; @@ -209,17 +210,13 @@ public void testPR45() throws Exception { assertNotNull( myMojo.annotationProcessorPaths ); assertEquals( 1, myMojo.annotationProcessorPaths.size() ); - final DependencyCoordinate coord = myMojo.annotationProcessorPaths.get(0); + final Dependency coord = myMojo.annotationProcessorPaths.get(0); assertNotNull( coord ); assertEquals( "org.mapstruct", coord.getGroupId() ); assertEquals( "mapstruct-processor", coord.getArtifactId() ); assertEquals( "1.4.2.Final", coord.getVersion() ); - assertNotNull( "repoSystem not initialized", myMojo.repoSystem ); - assertNotNull( "repoSession not initialized", myMojo.repoSession ); - assertNotNull( "remoteRepos not initialized", myMojo.remoteRepos ); - final Optional processorPath = myMojo.buildProcessorPath();