Skip to content

Commit

Permalink
PR #95 - replace DependencyCoordinate with org.apache.maven.model.Dep…
Browse files Browse the repository at this point in the history
…endency
  • Loading branch information
bsorrentino committed Jun 28, 2021
1 parent 4b1261c commit e7818da
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo {
* @since 5.0
*/
@Parameter
List<DependencyCoordinate> annotationProcessorPaths;
List<org.apache.maven.model.Dependency> annotationProcessorPaths;

/**
* Annotation Processor FQN (Full Qualified Name) - when processors are not specified, the default discovery mechanism will be used
Expand Down Expand Up @@ -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.
Expand All @@ -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<RemoteRepository> remoteRepos;
private List<RemoteRepository> remoteRepos;

/**
* List of artifacts on which perform sources scanning
Expand Down Expand Up @@ -545,8 +545,7 @@ private List<String> prepareOptions(JavaCompiler compiler) throws MojoExecutionE

options.add("-proc:only");

Optional<String> processorPath = this.buildProcessorPath();
processorPath.ifPresent(value -> {
this.buildProcessorPath().ifPresent(value -> {
options.add("-processorpath");
options.add(value);
});
Expand Down Expand Up @@ -997,52 +996,58 @@ private void processSourceArtifacts(Consumer<Artifact> closure) {
}
}

private Optional<List<String>> resolveProcessorPathEntries() throws MojoExecutionException {
if (this.annotationProcessorPaths == null || this.annotationProcessorPaths.isEmpty()) {
return Optional.empty();
}
private Optional<List<String>> resolveProcessorPathEntries() {
if (this.annotationProcessorPaths != null && !this.annotationProcessorPaths.isEmpty() ) {

try {
Set<Dependency> 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<Dependency> 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<String> 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<String> 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<String> buildProcessorPath() throws MojoExecutionException {
Optional<List<String>> processorPathEntries = this.resolveProcessorPathEntries();
return processorPathEntries.map(value -> StringUtils.join(value.iterator(), File.pathSeparator));
protected Optional<String> buildProcessorPath() {
return this.resolveProcessorPathEntries()
.flatMap( artifactPaths ->
Optional.of(artifactPaths.stream().collect(Collectors.joining(File.separator))) );
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> processorPath = myMojo.buildProcessorPath();

Expand Down

0 comments on commit e7818da

Please sign in to comment.