This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
commit c540eec772db115b24a15efc7f6f87fed1b2ecd1 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 00:22:04 2026 +0100 [MDEP-907] Resolve maven-compiler-plugin annotationProcessorPaths in go-offline --- src/it/mrm/repository/mdep907-excluded-dep-1.0.jar | Bin 0 -> 339 bytes src/it/mrm/repository/mdep907-excluded-dep-1.0.pom | 25 +++ .../repository/mdep907-managed-processor-1.0.jar | Bin 0 -> 339 bytes .../repository/mdep907-managed-processor-1.0.pom | 25 +++ src/it/mrm/repository/mdep907-processor-1.0.jar | Bin 0 -> 339 bytes src/it/mrm/repository/mdep907-processor-1.0.pom | 36 +++++ .../mrm/repository/mdep907-processor-dep-1.0.jar | Bin 0 -> 339 bytes .../mrm/repository/mdep907-processor-dep-1.0.pom | 25 +++ .../invoker.properties | 18 +++ .../pom.xml | 78 +++++++++ .../verify.groovy | 38 +++++ .../dependency/resolvers/GoOfflineMojo.java | 53 +++++++ .../dependency/utils/AnnotationProcessorPaths.java | 161 +++++++++++++++++++ .../utils/AnnotationProcessorPathsTest.java | 174 +++++++++++++++++++++ 14 files changed, 633 insertions(+) diff --git a/src/it/mrm/repository/mdep907-excluded-dep-1.0.jar b/src/it/mrm/repository/mdep907-excluded-dep-1.0.jar new file mode 100644 index 00000000..8ff11b47 Binary files /dev/null and b/src/it/mrm/repository/mdep907-excluded-dep-1.0.jar differ diff --git a/src/it/mrm/repository/mdep907-excluded-dep-1.0.pom b/src/it/mrm/repository/mdep907-excluded-dep-1.0.pom new file mode 100644 index 00000000..a47b2d25 --- /dev/null +++ b/src/it/mrm/repository/mdep907-excluded-dep-1.0.pom @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-excluded-dep</artifactId> + <version>1.0</version> +</project> diff --git a/src/it/mrm/repository/mdep907-managed-processor-1.0.jar b/src/it/mrm/repository/mdep907-managed-processor-1.0.jar new file mode 100644 index 00000000..8ff11b47 Binary files /dev/null and b/src/it/mrm/repository/mdep907-managed-processor-1.0.jar differ diff --git a/src/it/mrm/repository/mdep907-managed-processor-1.0.pom b/src/it/mrm/repository/mdep907-managed-processor-1.0.pom new file mode 100644 index 00000000..06661910 --- /dev/null +++ b/src/it/mrm/repository/mdep907-managed-processor-1.0.pom @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-managed-processor</artifactId> + <version>1.0</version> +</project> diff --git a/src/it/mrm/repository/mdep907-processor-1.0.jar b/src/it/mrm/repository/mdep907-processor-1.0.jar new file mode 100644 index 00000000..8ff11b47 Binary files /dev/null and b/src/it/mrm/repository/mdep907-processor-1.0.jar differ diff --git a/src/it/mrm/repository/mdep907-processor-1.0.pom b/src/it/mrm/repository/mdep907-processor-1.0.pom new file mode 100644 index 00000000..363fc9b5 --- /dev/null +++ b/src/it/mrm/repository/mdep907-processor-1.0.pom @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-processor</artifactId> + <version>1.0</version> + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-processor-dep</artifactId> + <version>1.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-excluded-dep</artifactId> + <version>1.0</version> + </dependency> + </dependencies></project> diff --git a/src/it/mrm/repository/mdep907-processor-dep-1.0.jar b/src/it/mrm/repository/mdep907-processor-dep-1.0.jar new file mode 100644 index 00000000..8ff11b47 Binary files /dev/null and b/src/it/mrm/repository/mdep907-processor-dep-1.0.jar differ diff --git a/src/it/mrm/repository/mdep907-processor-dep-1.0.pom b/src/it/mrm/repository/mdep907-processor-dep-1.0.pom new file mode 100644 index 00000000..1a83a196 --- /dev/null +++ b/src/it/mrm/repository/mdep907-processor-dep-1.0.pom @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-processor-dep</artifactId> + <version>1.0</version> +</project> diff --git a/src/it/projects/mdep-907-go-offline-annotation-processor-paths/invoker.properties b/src/it/projects/mdep-907-go-offline-annotation-processor-paths/invoker.properties new file mode 100644 index 00000000..e5961219 --- /dev/null +++ b/src/it/projects/mdep-907-go-offline-annotation-processor-paths/invoker.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:go-offline diff --git a/src/it/projects/mdep-907-go-offline-annotation-processor-paths/pom.xml b/src/it/projects/mdep-907-go-offline-annotation-processor-paths/pom.xml new file mode 100644 index 00000000..07e8b3e7 --- /dev/null +++ b/src/it/projects/mdep-907-go-offline-annotation-processor-paths/pom.xml @@ -0,0 +1,78 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.plugins.dependency</groupId> + <artifactId>mdep-907-go-offline-annotation-processor-paths</artifactId> + <version>1.0.0-SNAPSHOT</version> + <description>Test that dependency:go-offline resolves the annotationProcessorPaths of maven-compiler-plugin</description> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-managed-processor</artifactId> + <version>1.0</version> + </dependency> + </dependencies> + </dependencyManagement> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.14.1</version> + <configuration> + <annotationProcessorPaths> + <path> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-processor</artifactId> + <version>1.0</version> + <exclusions> + <exclusion> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-excluded-dep</artifactId> + </exclusion> + </exclusions> + </path> + </annotationProcessorPaths> + </configuration> + <executions> + <execution> + <id>other</id> + <phase>none</phase> + <configuration> + <annotationProcessorPathsUseDepMgmt>true</annotationProcessorPathsUseDepMgmt> + <annotationProcessorPaths> + <path> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep907-managed-processor</artifactId> + </path> + </annotationProcessorPaths> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/projects/mdep-907-go-offline-annotation-processor-paths/verify.groovy b/src/it/projects/mdep-907-go-offline-annotation-processor-paths/verify.groovy new file mode 100644 index 00000000..89fede5e --- /dev/null +++ b/src/it/projects/mdep-907-go-offline-annotation-processor-paths/verify.groovy @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +def assertResolved(String artifactId) { + ['jar', 'pom'].each { ext -> + File f = new File(localRepositoryPath, "org/apache/maven/its/dependency/${artifactId}/1.0/${artifactId}-1.0.${ext}") + assert f.isFile() : "${f} was not resolved" + } +} + +// configured at plugin level, with a transitive dependency +assertResolved('mdep907-processor') +assertResolved('mdep907-processor-dep') +// configured in an execution, version taken from dependencyManagement +assertResolved('mdep907-managed-processor') +// excluded by the path's exclusions +assert !new File(localRepositoryPath, 'org/apache/maven/its/dependency/mdep907-excluded-dep').exists() + +String buildLog = new File(basedir, 'build.log').getText('UTF-8') +assert buildLog.contains('Resolved annotation processor path: mdep907-processor-1.0.jar') + +return true diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java index 7fe66745..0c30b2d5 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java @@ -20,6 +20,7 @@ package org.apache.maven.plugins.dependency.resolvers; import javax.inject.Inject; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -42,6 +43,7 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.dependency.fromDependencies.AbstractDependencyFilterMojo; +import org.apache.maven.plugins.dependency.utils.AnnotationProcessorPaths; import org.apache.maven.plugins.dependency.utils.DependencyUtil; import org.apache.maven.plugins.dependency.utils.ResolverUtil; import org.apache.maven.plugins.dependency.utils.filters.GroupIdFilter; @@ -57,6 +59,7 @@ import org.apache.maven.shared.artifact.filter.collection.TypeFilter; import org.eclipse.aether.artifact.ArtifactTypeRegistry; import org.eclipse.aether.resolution.ArtifactDescriptorException; import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; import org.eclipse.aether.resolution.DependencyResolutionException; import org.sonatype.plexus.build.incremental.BuildContext; @@ -128,6 +131,11 @@ public class GoOfflineMojo extends AbstractDependencyFilterMojo { + DependencyUtil.getFormattedFileName(RepositoryUtils.toArtifact(artifact), false)); } + for (org.eclipse.aether.artifact.Artifact artifact : resolveAnnotationProcessorPaths()) { + logMessage("Resolved annotation processor path: " + + DependencyUtil.getFormattedFileName(RepositoryUtils.toArtifact(artifact), false)); + } + } catch (ArtifactFilterException | ArtifactResolutionException | DependencyResolutionException @@ -194,6 +202,51 @@ public class GoOfflineMojo extends AbstractDependencyFilterMojo { getProject().getRemoteProjectRepositories()); } + /** + * Resolves the {@code annotationProcessorPaths} of the {@code maven-compiler-plugin}, which Maven 3 resolves + * only when it compiles. The artifact filters of this goal apply to the paths; the transitive dependencies of a + * path are resolved unless {@code excludeTransitive} is set. + */ + private List<org.eclipse.aether.artifact.Artifact> resolveAnnotationProcessorPaths() + throws ArtifactFilterException, ArtifactResolutionException, DependencyResolutionException { + List<Dependency> paths = AnnotationProcessorPaths.collect( + getProject().getBuild().getPlugins(), getProject().getDependencyManagement(), getLog()::warn); + + List<Dependency> selected = new ArrayList<>(); + for (Dependency path : paths) { + if (!filterDependencies(Collections.singletonList(path)).isEmpty()) { + selected.add(path); + } + } + if (selected.isEmpty()) { + return Collections.emptyList(); + } + + ArtifactTypeRegistry artifactTypeRegistry = + session.getRepositorySession().getArtifactTypeRegistry(); + List<org.eclipse.aether.graph.Dependency> coordinates = selected.stream() + .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) + .collect(Collectors.toList()); + + if (excludeTransitive) { + return getResolverUtil() + .resolveArtifacts( + coordinates.stream() + .map(org.eclipse.aether.graph.Dependency::getArtifact) + .collect(Collectors.toList()), + getProject().getRemoteProjectRepositories()) + .stream() + .map(ArtifactResult::getArtifact) + .collect(Collectors.toList()); + } + return getResolverUtil() + .resolveDependenciesForArtifact( + RepositoryUtils.toArtifact(getProject().getArtifact()), + coordinates, + null, + getProject().getRemoteProjectRepositories()); + } + /** * This method retrieve plugins list from the project. * diff --git a/src/main/java/org/apache/maven/plugins/dependency/utils/AnnotationProcessorPaths.java b/src/main/java/org/apache/maven/plugins/dependency/utils/AnnotationProcessorPaths.java new file mode 100644 index 00000000..7e0851cc --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/utils/AnnotationProcessorPaths.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.dependency.utils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Consumer; + +import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; +import org.apache.maven.model.Exclusion; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginExecution; +import org.codehaus.plexus.util.xml.Xpp3Dom; + +/** + * Reads the {@code annotationProcessorPaths} of the {@code maven-compiler-plugin} from the configuration of build + * plugins. Maven does not resolve these artifacts as plugin dependencies, so they have to be found in the + * configuration. + */ +public final class AnnotationProcessorPaths { + + static final String COMPILER_PLUGIN_GROUP_ID = "org.apache.maven.plugins"; + + static final String COMPILER_PLUGIN_ARTIFACT_ID = "maven-compiler-plugin"; + + private static final String PARAMETER = "annotationProcessorPaths"; + + private AnnotationProcessorPaths() {} + + /** + * Collects the annotation processor paths configured for the {@code maven-compiler-plugin}, both in the plugin + * configuration and in the configuration of each of its executions. + * + * @param plugins the build plugins of the project, from the effective model + * @param dependencyManagement the dependency management of the project, used for paths without a version; may be + * {@code null} + * @param warn receives a message for each path that is skipped + * @return the paths as compile scope dependencies, without duplicates, in configuration order + */ + public static List<Dependency> collect( + Collection<Plugin> plugins, DependencyManagement dependencyManagement, Consumer<String> warn) { + Map<String, Dependency> result = new LinkedHashMap<>(); + for (Plugin plugin : plugins) { + if (!COMPILER_PLUGIN_GROUP_ID.equals(plugin.getGroupId()) + || !COMPILER_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) { + continue; + } + List<Object> configurations = new ArrayList<>(); + configurations.add(plugin.getConfiguration()); + for (PluginExecution execution : plugin.getExecutions()) { + configurations.add(execution.getConfiguration()); + } + for (Object configuration : configurations) { + for (Dependency dependency : parse(configuration, dependencyManagement, warn)) { + result.putIfAbsent(key(dependency), dependency); + } + } + } + return new ArrayList<>(result.values()); + } + + private static List<Dependency> parse( + Object configuration, DependencyManagement dependencyManagement, Consumer<String> warn) { + List<Dependency> dependencies = new ArrayList<>(); + if (!(configuration instanceof Xpp3Dom)) { + return dependencies; + } + Xpp3Dom paths = ((Xpp3Dom) configuration).getChild(PARAMETER); + if (paths == null) { + return dependencies; + } + for (Xpp3Dom path : paths.getChildren()) { + Dependency dependency = new Dependency(); + dependency.setGroupId(value(path, "groupId")); + dependency.setArtifactId(value(path, "artifactId")); + dependency.setVersion(value(path, "version")); + String type = value(path, "type"); + dependency.setType(type == null ? "jar" : type); + dependency.setClassifier(value(path, "classifier")); + dependency.setScope("compile"); + + if (dependency.getGroupId() == null || dependency.getArtifactId() == null) { + warn.accept("Skipping annotation processor path without groupId or artifactId: " + path); + continue; + } + if (dependency.getVersion() == null) { + dependency.setVersion(managedVersion(dependency, dependencyManagement)); + if (dependency.getVersion() == null) { + warn.accept("Skipping annotation processor path " + dependency.getGroupId() + ":" + + dependency.getArtifactId() + + ": it has no version and none is managed in dependencyManagement"); + continue; + } + } + Xpp3Dom exclusions = path.getChild("exclusions"); + if (exclusions != null) { + for (Xpp3Dom exclusionNode : exclusions.getChildren("exclusion")) { + Exclusion exclusion = new Exclusion(); + exclusion.setGroupId(value(exclusionNode, "groupId")); + exclusion.setArtifactId(value(exclusionNode, "artifactId")); + if (exclusion.getGroupId() != null && exclusion.getArtifactId() != null) { + dependency.addExclusion(exclusion); + } + } + } + dependencies.add(dependency); + } + return dependencies; + } + + private static String managedVersion(Dependency dependency, DependencyManagement dependencyManagement) { + if (dependencyManagement == null) { + return null; + } + for (Dependency managed : dependencyManagement.getDependencies()) { + if (Objects.equals(managed.getGroupId(), dependency.getGroupId()) + && Objects.equals(managed.getArtifactId(), dependency.getArtifactId()) + && Objects.equals(managed.getType(), dependency.getType()) + && Objects.equals(emptyToNull(managed.getClassifier()), emptyToNull(dependency.getClassifier()))) { + return managed.getVersion(); + } + } + return null; + } + + private static String value(Xpp3Dom node, String name) { + Xpp3Dom child = node.getChild(name); + return child == null + ? null + : emptyToNull(child.getValue() == null ? null : child.getValue().trim()); + } + + private static String emptyToNull(String value) { + return value == null || value.isEmpty() ? null : value; + } + + private static String key(Dependency dependency) { + return dependency.getManagementKey() + ":" + dependency.getVersion(); + } +} diff --git a/src/test/java/org/apache/maven/plugins/dependency/utils/AnnotationProcessorPathsTest.java b/src/test/java/org/apache/maven/plugins/dependency/utils/AnnotationProcessorPathsTest.java new file mode 100644 index 00000000..0b186cf0 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/dependency/utils/AnnotationProcessorPathsTest.java @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.dependency.utils; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginExecution; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class AnnotationProcessorPathsTest { + + private final List<String> warnings = new ArrayList<>(); + + private static Plugin compiler(String pluginConfig, String... executionConfigs) throws Exception { + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-compiler-plugin"); + if (pluginConfig != null) { + plugin.setConfiguration(Xpp3DomBuilder.build(new StringReader(pluginConfig))); + } + int i = 0; + for (String config : executionConfigs) { + PluginExecution execution = new PluginExecution(); + execution.setId("e" + i++); + execution.setConfiguration(Xpp3DomBuilder.build(new StringReader(config))); + plugin.addExecution(execution); + } + return plugin; + } + + private static String paths(String... paths) { + return "<configuration><annotationProcessorPaths>" + String.join("", paths) + + "</annotationProcessorPaths></configuration>"; + } + + private static String path(String groupId, String artifactId, String version) { + return path(groupId, artifactId, version, ""); + } + + private static String path(String groupId, String artifactId, String version, String extra) { + return "<path><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId>" + + (version == null ? "" : "<version>" + version + "</version>") + extra + "</path>"; + } + + private List<Dependency> collect(DependencyManagement management, Plugin... plugins) { + List<Plugin> list = new ArrayList<>(); + Collections.addAll(list, plugins); + return AnnotationProcessorPaths.collect(list, management, warnings::add); + } + + @Test + void readsPluginLevelConfiguration() throws Exception { + List<Dependency> result = collect(null, compiler(paths(path("g", "a", "1.0"), path("g", "b", "2.0")))); + + assertEquals(2, result.size()); + assertEquals( + "g:a:jar:1.0", + result.get(0).getManagementKey() + ":" + result.get(0).getVersion()); + assertEquals("compile", result.get(0).getScope()); + assertEquals("b", result.get(1).getArtifactId()); + assertTrue(warnings.isEmpty()); + } + + @Test + void readsExecutionConfigurationAndDeduplicates() throws Exception { + List<Dependency> result = collect( + null, + compiler( + paths(path("g", "a", "1.0")), + paths(path("g", "a", "1.0"), path("g", "c", "3.0")), + paths(path("g", "a", "1.1")))); + + assertEquals(3, result.size()); + assertEquals("1.0", result.get(0).getVersion()); + assertEquals("c", result.get(1).getArtifactId()); + assertEquals("1.1", result.get(2).getVersion()); + } + + @Test + void readsTypeAndClassifier() throws Exception { + List<Dependency> result = collect( + null, compiler(paths(path("g", "a", "1.0", "<type>test-jar</type><classifier>proc</classifier>")))); + + assertEquals(1, result.size()); + assertEquals("test-jar", result.get(0).getType()); + assertEquals("proc", result.get(0).getClassifier()); + } + + @Test + void takesMissingVersionFromDependencyManagement() throws Exception { + DependencyManagement management = new DependencyManagement(); + Dependency managed = new Dependency(); + managed.setGroupId("g"); + managed.setArtifactId("a"); + managed.setVersion("4.2"); + management.addDependency(managed); + + List<Dependency> result = collect(management, compiler(null, paths(path("g", "a", null)))); + + assertEquals(1, result.size()); + assertEquals("4.2", result.get(0).getVersion()); + assertTrue(warnings.isEmpty()); + } + + @Test + void skipsMissingVersionWithoutManagedVersion() throws Exception { + DependencyManagement management = new DependencyManagement(); + Dependency managed = new Dependency(); + managed.setGroupId("g"); + managed.setArtifactId("a"); + managed.setClassifier("other"); + managed.setVersion("4.2"); + management.addDependency(managed); + + List<Dependency> result = collect(management, compiler(paths(path("g", "a", null), path("g", "b", "1")))); + + assertEquals(1, result.size()); + assertEquals("b", result.get(0).getArtifactId()); + assertEquals(1, warnings.size()); + assertTrue(warnings.get(0).contains("g:a")); + } + + @Test + void readsExclusions() throws Exception { + String exclusions = "<exclusions><exclusion><groupId>x</groupId><artifactId>y</artifactId></exclusion>" + + "<exclusion><groupId>*</groupId><artifactId>z</artifactId></exclusion></exclusions>"; + + List<Dependency> result = collect(null, compiler(paths(path("g", "a", "1.0", exclusions)))); + + assertEquals(2, result.get(0).getExclusions().size()); + assertEquals("x", result.get(0).getExclusions().get(0).getGroupId()); + assertEquals("y", result.get(0).getExclusions().get(0).getArtifactId()); + assertEquals("*", result.get(0).getExclusions().get(1).getGroupId()); + } + + @Test + void ignoresOtherPluginsAndMissingConfiguration() throws Exception { + Plugin other = compiler(paths(path("g", "a", "1.0"))); + other.setArtifactId("maven-surefire-plugin"); + + assertTrue(collect(null).isEmpty()); + assertTrue(collect(null, other).isEmpty()); + assertTrue(collect(null, compiler(null)).isEmpty()); + assertTrue(collect(null, compiler("<configuration><release>17</release></configuration>")) + .isEmpty()); + assertTrue(warnings.isEmpty()); + } +}
