psiroky commented on code in PR #180: URL: https://github.com/apache/maven-compiler-plugin/pull/180#discussion_r1121566199
########## src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java: ########## @@ -1584,42 +1605,91 @@ private List<String> resolveProcessorPathEntries() throws MojoExecutionException return null; } - Set<String> elements = new LinkedHashSet<>(); try { - List<Dependency> dependencies = convertToDependencies(annotationProcessorPaths); + List<org.eclipse.aether.graph.Dependency> dependencies = convertToDependencies(annotationProcessorPaths); + List<org.eclipse.aether.graph.Dependency> 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<Dependency> convertToDependencies(List<DependencyCoordinate> annotationProcessorPaths) { - List<Dependency> dependencies = new ArrayList<>(); + private List<org.eclipse.aether.graph.Dependency> convertToDependencies( + List<DependencyCoordinate> annotationProcessorPaths) throws MojoExecutionException { + List<org.eclipse.aether.graph.Dependency> 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<Exclusion> 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<Dependency> 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<String> findManagedVersion( + DependencyCoordinate dependencyCoordinate, List<Dependency> 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<org.eclipse.aether.graph.Dependency> getManagedDependenciesForAnnotationProcessorPaths() { + if (!annotationProcessorPathsUseDepMgmt) { + return Collections.emptyList(); + } + List<Dependency> projectManagedDependencies = getProjectManagedDependencies(); + ArtifactTypeRegistry artifactTypeRegistry = + session.getRepositorySession().getArtifactTypeRegistry(); + + return projectManagedDependencies.stream() + .map(dep -> RepositoryUtils.toDependency(dep, artifactTypeRegistry)) Review Comment: This is also not great, since it re-creates the dependency list for every sub-module of the Maven build, and usually the dependency management section is 99% same for all modules. Taking example of Quarkus. It has ~500 modules using annotation processing and the dependency management section has ~2k dependencies. That 1 million of new objects created during the build. Possible not the biggest deal (since the build already creates tens of millions of other objects). Again, at this point I am not seeing a simple alternative. Suggestions more than welcome. Maybe some caching could be done here. But caching is usually double-edge sword, so I wanted to avoid that for now. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org