annotationProcessorPaths;
+ /**
+ *
+ * Whether to use the Maven dependency management section when resolving transitive dependencies of annotation
+ * processor paths.
+ *
+ *
+ * This flag does not enable / disable the ability to resolve the version of annotation processor paths
+ * from dependency management section. It only influences the resolution o transitive dependencies of those
+ * top-level paths.
+ *
+ *
+ * @since 3.12.0
+ */
+ @Parameter(defaultValue = "false")
+ private boolean annotationProcessorPathsUseDepMgmt;
+
/**
*
* Sets the arguments to be passed to the compiler (prepending a dash).
@@ -1584,42 +1605,91 @@ private List resolveProcessorPathEntries() throws MojoExecutionException
return null;
}
- Set elements = new LinkedHashSet<>();
try {
- List dependencies = convertToDependencies(annotationProcessorPaths);
+ List dependencies = convertToDependencies(annotationProcessorPaths);
+ List managedDependencies =
+ getManagedDependenciesForAnnotationProcessorPaths();
CollectRequest collectRequest =
- new CollectRequest(dependencies, Collections.emptyList(), project.getRemoteProjectRepositories());
+ new CollectRequest(dependencies, managedDependencies, project.getRemoteProjectRepositories());
DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setCollectRequest(collectRequest);
DependencyResult dependencyResult =
repositorySystem.resolveDependencies(session.getRepositorySession(), dependencyRequest);
- for (ArtifactResult resolved : dependencyResult.getArtifactResults()) {
- elements.add(resolved.getArtifact().getFile().getAbsolutePath());
- }
- return new ArrayList<>(elements);
+ return dependencyResult.getArtifactResults().stream()
+ .map(resolved -> resolved.getArtifact().getFile().getAbsolutePath())
+ .collect(Collectors.toList());
} catch (Exception e) {
throw new MojoExecutionException(
"Resolution of annotationProcessorPath dependencies failed: " + e.getLocalizedMessage(), e);
}
}
- private List convertToDependencies(List annotationProcessorPaths) {
- List dependencies = new ArrayList<>();
+ private List convertToDependencies(
+ List annotationProcessorPaths) throws MojoExecutionException {
+ List dependencies = new ArrayList<>();
for (DependencyCoordinate annotationProcessorPath : annotationProcessorPaths) {
ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(annotationProcessorPath.getType());
+ String version = getAnnotationProcessorPathVersion(annotationProcessorPath);
Artifact artifact = new DefaultArtifact(
annotationProcessorPath.getGroupId(),
annotationProcessorPath.getArtifactId(),
annotationProcessorPath.getClassifier(),
handler.getExtension(),
- annotationProcessorPath.getVersion());
+ version);
Set exclusions = convertToAetherExclusions(annotationProcessorPath.getExclusions());
- dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME, false, exclusions));
+ dependencies.add(new org.eclipse.aether.graph.Dependency(artifact, JavaScopes.RUNTIME, false, exclusions));
}
return dependencies;
}
+ private String getAnnotationProcessorPathVersion(DependencyCoordinate annotationProcessorPath)
+ throws MojoExecutionException {
+ String configuredVersion = annotationProcessorPath.getVersion();
+ if (configuredVersion != null) {
+ return configuredVersion;
+ } else {
+ List managedDependencies = getProjectManagedDependencies();
+ return findManagedVersion(annotationProcessorPath, managedDependencies)
+ .orElseThrow(() -> new MojoExecutionException(String.format(
+ "Cannot find version for annotation processor path '%s'. The version needs to be either"
+ + " provided directly in the plugin configuration or via dependency management.",
+ annotationProcessorPath)));
+ }
+ }
+
+ private Optional findManagedVersion(
+ DependencyCoordinate dependencyCoordinate, List managedDependencies) {
+ return managedDependencies.stream()
+ .filter(dep -> Objects.equals(dep.getGroupId(), dependencyCoordinate.getGroupId())
+ && Objects.equals(dep.getArtifactId(), dependencyCoordinate.getArtifactId())
+ && Objects.equals(dep.getClassifier(), dependencyCoordinate.getClassifier())
+ && Objects.equals(dep.getType(), dependencyCoordinate.getType()))
+ .findAny()
+ .map(org.apache.maven.model.Dependency::getVersion);
+ }
+
+ private List getManagedDependenciesForAnnotationProcessorPaths() {
+ if (!annotationProcessorPathsUseDepMgmt) {
+ return Collections.emptyList();
+ }
+ List projectManagedDependencies = getProjectManagedDependencies();
+ ArtifactTypeRegistry artifactTypeRegistry =
+ session.getRepositorySession().getArtifactTypeRegistry();
+
+ return projectManagedDependencies.stream()
+ .map(dep -> RepositoryUtils.toDependency(dep, artifactTypeRegistry))
+ .collect(Collectors.toList());
+ }
+
+ private List getProjectManagedDependencies() {
+ DependencyManagement dependencyManagement = project.getDependencyManagement();
+ if (dependencyManagement == null || dependencyManagement.getDependencies() == null) {
+ return Collections.emptyList();
+ }
+ return dependencyManagement.getDependencies();
+ }
+
private Set convertToAetherExclusions(Set exclusions) {
if (exclusions == null || exclusions.isEmpty()) {
return Collections.emptySet();