From 1bd7bb2d009b40d747fad3808458d984c4abde21 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sat, 6 Jul 2024 09:11:50 +0200 Subject: [PATCH] [MINVOKER-368] Filter artifacts by scope before resolving in install --- .../MINVOKER-368-install-filter-scope/pom.xml | 76 +++++++++++++++++++ .../verify.groovy | 32 ++++++++ .../maven/plugins/invoker/InstallMojo.java | 13 +--- 3 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 src/it/MINVOKER-368-install-filter-scope/pom.xml create mode 100644 src/it/MINVOKER-368-install-filter-scope/verify.groovy diff --git a/src/it/MINVOKER-368-install-filter-scope/pom.xml b/src/it/MINVOKER-368-install-filter-scope/pom.xml new file mode 100644 index 00000000..ed5374e6 --- /dev/null +++ b/src/it/MINVOKER-368-install-filter-scope/pom.xml @@ -0,0 +1,76 @@ + + + + + + 4.0.0 + + org.apache.maven.plugins.invoker + MINVOKER-368 + 1.0-SNAPSHOT + pom + + + Test to check for scope filtering during installation. + + + + UTF-8 + + + + + + + org.apache.maven.shared + maven-shared-utils + 3.4.2 + + + commons-io + commons-io + 2.16.1 + test + + + + + + + org.apache.maven.plugins + maven-invoker-plugin + @project.version@ + + ${project.build.directory}/it-repo + + + + integration-test + initialize + + install + + + + + + + + diff --git a/src/it/MINVOKER-368-install-filter-scope/verify.groovy b/src/it/MINVOKER-368-install-filter-scope/verify.groovy new file mode 100644 index 00000000..1a8e1c28 --- /dev/null +++ b/src/it/MINVOKER-368-install-filter-scope/verify.groovy @@ -0,0 +1,32 @@ +/* + * 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 expected = [ + 'org/apache/maven/shared/maven-shared-utils/3.4.2/maven-shared-utils-3.4.2.pom', + 'org/apache/maven/shared/maven-shared-utils/3.4.2/maven-shared-utils-3.4.2.jar', + 'commons-io/commons-io/2.11.0/commons-io-2.11.0.pom', + 'commons-io/commons-io/2.11.0/commons-io-2.11.0.jar' +] + +def localRepo = new File(basedir, 'target/it-repo') + +def missingFiles = expected.findAll { !new File(localRepo, it).isFile() } + +assert missingFiles == [] diff --git a/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java b/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java index 0a7ec02e..8894ef9d 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java @@ -52,6 +52,7 @@ import org.eclipse.aether.artifact.ArtifactTypeRegistry; import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.graph.DefaultDependencyNode; import org.eclipse.aether.graph.Dependency; import org.eclipse.aether.graph.DependencyFilter; import org.eclipse.aether.installation.InstallRequest; @@ -59,7 +60,6 @@ import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.LocalRepositoryManager; import org.eclipse.aether.repository.RemoteRepository; -import org.eclipse.aether.resolution.ArtifactDescriptorException; import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResolutionException; import org.eclipse.aether.resolution.ArtifactResult; @@ -172,10 +172,7 @@ public void execute() throws MojoExecutionException { resolveExtraArtifacts(resolvedArtifacts); installArtifacts(resolvedArtifacts); - } catch (DependencyResolutionException - | InstallationException - | ArtifactDescriptorException - | ArtifactResolutionException e) { + } catch (DependencyResolutionException | InstallationException | ArtifactResolutionException e) { throw new MojoExecutionException(e.getMessage(), e); } } @@ -227,6 +224,7 @@ private void resolveProjectDependencies(Map resolvedArtifacts) List dependencies = project.getDependencies().stream() .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) + .filter(d -> classpathFilter.accept(new DefaultDependencyNode(d), null)) .collect(Collectors.toList()); CollectRequest collectRequest = new CollectRequest(); @@ -251,12 +249,9 @@ private void resolveProjectDependencies(Map resolvedArtifacts) /** * Resolve extra artifacts. - * - * @return */ private void resolveExtraArtifacts(Map resolvedArtifacts) - throws MojoExecutionException, DependencyResolutionException, ArtifactDescriptorException, - ArtifactResolutionException { + throws MojoExecutionException, DependencyResolutionException, ArtifactResolutionException { if (extraArtifacts == null) { return;