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 3677e5f06226a85a1a0e6ca0b34327c86c2998cc Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 13:37:15 2026 +0100 Filter get-classpath to the runtime classpath and document version rules --- src/it/mrm/repository/get-classpath-lib-1.0.pom | 16 +++++++ .../mrm/repository/get-classpath-lib-dep-1.0.pom | 7 ++++ ...-dep-1.0.pom => get-classpath-lib-leaf-2.0.pom} | 12 +----- ...p-1.0.pom => get-classpath-lib-runtime-1.0.pom} | 10 +---- src/it/projects/get-classpath/invoker.properties | 4 +- src/it/projects/get-classpath/setup.groovy | 32 ++++++++++++++ src/it/projects/get-classpath/verify.groovy | 16 +++++-- .../maven/plugins/dependency/GetClasspathMojo.java | 22 ++++++---- .../plugins/dependency/utils/ResolverUtil.java | 49 ++++++++++++++++++---- src/site/markdown/usage.md.vm | 4 +- 10 files changed, 131 insertions(+), 41 deletions(-) diff --git a/src/it/mrm/repository/get-classpath-lib-1.0.pom b/src/it/mrm/repository/get-classpath-lib-1.0.pom index a4ac4e2e..b28479ce 100644 --- a/src/it/mrm/repository/get-classpath-lib-1.0.pom +++ b/src/it/mrm/repository/get-classpath-lib-1.0.pom @@ -27,12 +27,28 @@ <artifactId>get-classpath-lib</artifactId> <version>1.0</version> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>get-classpath-lib-leaf</artifactId> + <version>2.0</version> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> <dependency> <groupId>org.apache.maven.its.dependency</groupId> <artifactId>get-classpath-lib-dep</artifactId> <version>1.0</version> </dependency> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>get-classpath-lib-runtime</artifactId> + <version>1.0</version> + <scope>runtime</scope> + </dependency> <dependency> <groupId>org.apache.maven.its.dependency</groupId> <artifactId>get-classpath-lib-test</artifactId> diff --git a/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom b/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom index 9f7984b7..923c07ca 100644 --- a/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom +++ b/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom @@ -33,6 +33,13 @@ <artifactId>get-classpath-lib-leaf</artifactId> <version>1.0</version> </dependency> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>get-classpath-lib-system</artifactId> + <version>1.0</version> + <scope>system</scope> + <systemPath>/nonexistent/get-classpath-lib-system.jar</systemPath> + </dependency> </dependencies> </project> diff --git a/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom b/src/it/mrm/repository/get-classpath-lib-leaf-2.0.pom similarity index 80% copy from src/it/mrm/repository/get-classpath-lib-dep-1.0.pom copy to src/it/mrm/repository/get-classpath-lib-leaf-2.0.pom index 9f7984b7..ba2004f6 100644 --- a/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom +++ b/src/it/mrm/repository/get-classpath-lib-leaf-2.0.pom @@ -24,15 +24,7 @@ <modelVersion>4.0.0</modelVersion> <groupId>org.apache.maven.its.dependency</groupId> - <artifactId>get-classpath-lib-dep</artifactId> - <version>1.0</version> - - <dependencies> - <dependency> - <groupId>org.apache.maven.its.dependency</groupId> - <artifactId>get-classpath-lib-leaf</artifactId> - <version>1.0</version> - </dependency> - </dependencies> + <artifactId>get-classpath-lib-leaf</artifactId> + <version>2.0</version> </project> diff --git a/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom b/src/it/mrm/repository/get-classpath-lib-runtime-1.0.pom similarity index 82% copy from src/it/mrm/repository/get-classpath-lib-dep-1.0.pom copy to src/it/mrm/repository/get-classpath-lib-runtime-1.0.pom index 9f7984b7..57f84fa1 100644 --- a/src/it/mrm/repository/get-classpath-lib-dep-1.0.pom +++ b/src/it/mrm/repository/get-classpath-lib-runtime-1.0.pom @@ -24,15 +24,7 @@ <modelVersion>4.0.0</modelVersion> <groupId>org.apache.maven.its.dependency</groupId> - <artifactId>get-classpath-lib-dep</artifactId> + <artifactId>get-classpath-lib-runtime</artifactId> <version>1.0</version> - <dependencies> - <dependency> - <groupId>org.apache.maven.its.dependency</groupId> - <artifactId>get-classpath-lib-leaf</artifactId> - <version>1.0</version> - </dependency> - </dependencies> - </project> diff --git a/src/it/projects/get-classpath/invoker.properties b/src/it/projects/get-classpath/invoker.properties index 26bc8c96..ab57c21d 100644 --- a/src/it/projects/get-classpath/invoker.properties +++ b/src/it/projects/get-classpath/invoker.properties @@ -16,5 +16,5 @@ # under the License. # no project is needed, the artifact comes from -Dartifact -invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:get-classpath -Dartifact=org.apache.maven.its.dependency:get-classpath-lib:1.0 -Dmdep.outputFile=target/classpath.txt -invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:get-classpath -Dartifact=org.apache.maven.its.dependency:get-classpath-lib:1.0 -Dtransitive=false -Dmdep.outputFile=target/classpath-no-transitive.txt +invoker.goals.1 = -V ${project.groupId}:${project.artifactId}:${project.version}:get-classpath -Dartifact=org.apache.maven.its.dependency:get-classpath-lib:1.0 -Dmdep.outputFile=target/classpath.txt +invoker.goals.2 = -V ${project.groupId}:${project.artifactId}:${project.version}:get-classpath -Dartifact=org.apache.maven.its.dependency:get-classpath-lib:1.0 -Dtransitive=false -Dmdep.outputFile=target/classpath-no-transitive.txt diff --git a/src/it/projects/get-classpath/setup.groovy b/src/it/projects/get-classpath/setup.groovy new file mode 100644 index 00000000..06687709 --- /dev/null +++ b/src/it/projects/get-classpath/setup.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. + */ +import org.codehaus.plexus.util.FileUtils; + +// forces the download: every artifact of the IT is removed from the local repository first +File dir = new File( localRepositoryPath, "org/apache/maven/its/dependency" ); +File[] artifacts = dir.listFiles( { File f -> f.getName().startsWith( "get-classpath-lib" ) } as FileFilter ); +if ( artifacts != null ) +{ + for ( File artifact : artifacts ) + { + FileUtils.deleteDirectory( artifact ); + } +} + +return true; diff --git a/src/it/projects/get-classpath/verify.groovy b/src/it/projects/get-classpath/verify.groovy index 52be5a48..822846a0 100644 --- a/src/it/projects/get-classpath/verify.groovy +++ b/src/it/projects/get-classpath/verify.groovy @@ -33,12 +33,20 @@ String read( String name ) return new String( Files.readAllBytes( file.toPath() ), StandardCharsets.UTF_8 ); } -// the artifact first, then its runtime dependencies in the order of the dependency graph; the test, provided -// and optional dependencies of the artifact are not part of the classpath -String expected = [ jar( "get-classpath-lib" ), jar( "get-classpath-lib-dep" ), jar( "get-classpath-lib-leaf" ) ] +// The artifact is resolved as for a project that depends on it, in the order of the running Maven's resolver: the +// artifact first, then its runtime dependencies. The test, provided and optional dependencies of the artifact and the +// system dependency of its dependency are not part of the classpath. The artifact's own dependencyManagement, +// which sets the version of the leaf to 2.0, is applied by Maven 4 only. +String mavenVersion = new File( basedir, "build.log" ).readLines().find { it.startsWith( "Apache Maven " ) }; +assert mavenVersion != null : "No Maven version in build.log"; +String leafVersion = mavenVersion.startsWith( "Apache Maven 4" ) ? "2.0" : "1.0"; + +String leaf = new File( localRepositoryPath, + "org/apache/maven/its/dependency/get-classpath-lib-leaf/" + leafVersion + "/get-classpath-lib-leaf-" + leafVersion + ".jar" ).getPath(); +String expected = [ jar( "get-classpath-lib" ), jar( "get-classpath-lib-dep" ), jar( "get-classpath-lib-runtime" ), leaf ] .join( File.pathSeparator ); String actual = read( "classpath.txt" ); -assert actual.equals( expected ) : "Unexpected classpath: " + actual + ", expected: " + expected; +assert actual.equals( expected ) : "Unexpected classpath: " + actual + ", expected: " + expected + " for " + mavenVersion; String noTransitive = read( "classpath-no-transitive.txt" ); assert noTransitive.equals( jar( "get-classpath-lib" ) ) : "Unexpected classpath: " + noTransitive; diff --git a/src/main/java/org/apache/maven/plugins/dependency/GetClasspathMojo.java b/src/main/java/org/apache/maven/plugins/dependency/GetClasspathMojo.java index 0196aa1a..4b7a7364 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/GetClasspathMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/GetClasspathMojo.java @@ -53,10 +53,14 @@ import org.eclipse.aether.resolution.DependencyResolutionException; * <pre> * mvn dependency:get-classpath -Dartifact=org.apache.maven:maven-model:3.9.9 -Dmdep.outputFile=cp.txt * </pre> - * The artifact itself comes first in the classpath, followed by its dependencies in the order in which the resolver - * of the running Maven returns them, which may differ between Maven versions. Test and provided dependencies, and - * optional ones, are not part of the classpath. As <code>dependency:get</code> does, this goal always checks the - * central repository defined in the super POM. You could use a mirror entry in your <code>settings.xml</code>. + * The artifact is resolved as for a project that depends on it. The artifact comes first in the classpath, followed by + * its runtime dependencies. Test, provided, system and optional dependencies are not part of the classpath. Both the + * versions and the order of the dependencies follow the dependency management rules of the running Maven, which + * differ between Maven versions: Maven 3.9 and Maven 4 do not apply the artifact's own + * <code>dependencyManagement</code> alike. The result can therefore differ from + * <code>dependency:get -Dtransitive</code>, which resolves the artifact as the root of the graph. As + * <code>dependency:get</code> does, this goal always checks the central repository defined in the super POM. You + * could use a mirror entry in your <code>settings.xml</code>. * * @since 3.12.0 */ @@ -75,6 +79,10 @@ public class GetClasspathMojo extends AbstractMojo { * <p> * The comma-separated form works both on the command line and as the text of a single * <code><remoteRepositories></code> element. In a POM the repositories may also be listed one per element. + * <p> + * Without this parameter the repositories of the current project are used, which, when run without a project, are + * the central repository of the super POM plus those of the active profiles of <code>settings.xml</code>. Only + * the repositories given by this parameter are checked for updates on every run. */ @Parameter(property = "remoteRepositories") private List<String> remoteRepositories; @@ -109,7 +117,7 @@ public class GetClasspathMojo extends AbstractMojo { /** * Override the char used between path folders. The default is the system-dependent file-separator character: '/' - * on UNIX systems, '\' on Microsoft Windows systems. + * on UNIX systems, '\' on Microsoft Windows systems. It is applied to each path entry on its own. */ @Parameter(property = "mdep.fileSeparator", defaultValue = "") private String fileSeparator; @@ -139,8 +147,8 @@ public class GetClasspathMojo extends AbstractMojo { } if (paramArtifact.getArtifact() == null) { - throw new MojoFailureException("You must specify an artifact, " - + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0"); + throw new MojoFailureException( + "You must specify an artifact, " + "e.g. -Dartifact=org.apache.commons:commons-lang3:3.17.0"); } List<Artifact> artifacts; diff --git a/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java b/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java index d8458cba..adf5c5a7 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java +++ b/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java @@ -76,7 +76,9 @@ import org.eclipse.aether.resolution.VersionRangeRequest; import org.eclipse.aether.resolution.VersionRangeResolutionException; import org.eclipse.aether.resolution.VersionRangeResult; import org.eclipse.aether.util.artifact.ArtifactIdUtils; +import org.eclipse.aether.util.artifact.JavaScopes; import org.eclipse.aether.util.artifact.SubArtifact; +import org.eclipse.aether.util.filter.DependencyFilterUtils; import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator; import org.eclipse.aether.version.Version; @@ -310,8 +312,11 @@ public class ResolverUtil { /** * Resolve an artifact that is named by its coordinates alone, that is, without a project, and optionally its - * transitive dependencies, the way {@code dependency:get} does. The transitive dependencies are those of the - * runtime classpath: test and provided dependencies, and optional ones, are left out. + * transitive dependencies of the runtime classpath. The artifact is resolved as for a project that depends on + * it: its test and provided dependencies, its optional ones and its {@code system} ones are left out, and both + * the versions and the order of the dependencies follow the dependency management rules of the running Maven. + * The result can therefore differ from {@code dependency:get -Dtransitive}, which resolves the artifact as the + * root of the graph. * * @param artifact an artifact to resolve * @param repositories remote repositories list @@ -328,15 +333,45 @@ public class ResolverUtil { Artifact artifact, List<RemoteRepository> repositories, boolean transitive) throws ArtifactResolutionException, ArtifactDescriptorException, DependencyResolutionException { if (transitive) { - // The artifact is a direct dependency of a rootless request, so the session's selectors treat its own - // dependencies as transitive ones: test, provided and optional dependencies stay out. - CollectRequest collectRequest = - new CollectRequest(Collections.singletonList(new Dependency(artifact, null)), null, repositories); - return resolveDependencies(collectRequest, null); + return resolveArtifactAndDependencies( + artifact, + repositories, + DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME), + mavenSessionProvider.get().getRepositorySession()); } return Collections.singletonList(resolveArtifact(artifact, repositories)); } + /** + * Resolve an artifact and its transitive dependencies as for a project that depends on it, keeping the + * dependencies that the filter accepts. + * + * @param artifact an artifact to resolve + * @param repositories remote repositories list + * @param dependencyFilter dependency filter, or {@code null}; it is applied to the artifact itself as well + * @param session a repository system session + * @return the resolved artifacts, the artifact itself first, as for + * {@link #resolveArtifactAndDependencies(Artifact, List, boolean)} + * @throws DependencyResolutionException if the dependency tree could not be built or any dependency artifact could + * not be resolved + */ + public List<Artifact> resolveArtifactAndDependencies( + Artifact artifact, + List<RemoteRepository> repositories, + DependencyFilter dependencyFilter, + RepositorySystemSession session) + throws DependencyResolutionException { + // The artifact is a direct dependency of a rootless request, so the session's selectors treat its own + // dependencies as transitive ones: test, provided and optional dependencies stay out. + CollectRequest collectRequest = + new CollectRequest(Collections.singletonList(new Dependency(artifact, null)), null, repositories); + DependencyResult result = + repositorySystem.resolveDependencies(session, new DependencyRequest(collectRequest, dependencyFilter)); + return result.getArtifactResults().stream() + .map(ArtifactResult::getArtifact) + .collect(Collectors.toList()); + } + /** * Resolve transitive dependencies for artifact. * diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm index 4185fcd0..5df9f910 100644 --- a/src/site/markdown/usage.md.vm +++ b/src/site/markdown/usage.md.vm @@ -785,7 +785,7 @@ mvn dependency:get -DgroupId=org.apache.maven -DartifactId=maven-core -Dversion= `dependency:get-classpath` ------------------------- -This goal resolves an artifact, given by its Maven coordinates, and its transitive runtime dependencies, and outputs the classpath string of the files in the local repository. It needs no project, so it works in any directory. Use it to get the classpath of an ad-hoc artifact for `java -cp`. The artifact comes first, followed by its dependencies in the order in which the resolver of the running Maven returns them, which may differ between Maven versions. Test, provided and optional depen [...] +This goal resolves an artifact, given by its Maven coordinates, and its transitive runtime dependencies, and outputs the classpath string of the files in the local repository. It needs no project, so it works in any directory. Use it to get the classpath of an ad-hoc artifact for `java -cp`. The artifact is resolved as for a project that depends on it, so the result can differ from `dependency:get -Dtransitive`. The artifact comes first, followed by its runtime dependencies. Test, provid [...] ``` mvn dependency:get-classpath -Dartifact=org.apache.maven:maven-model:3.9.9 @@ -793,7 +793,7 @@ mvn dependency:get-classpath -Dartifact=org.apache.maven:maven-model:3.9.9 -Dmde mvn dependency:get-classpath -Dartifact=org.apache.maven:maven-model:3.9.9 -Dtransitive=false -DremoteRepositories=https://myrepo.com/maven2 ``` -Without `mdep.outputFile` the classpath is logged at the INFO level. Use `-q` together with `mdep.outputFile` to keep the build output quiet. The separators and a path prefix can be changed with `mdep.pathSeparator`, `mdep.fileSeparator` and `mdep.prefix`, as for `dependency:build-classpath`. +Without `mdep.outputFile` the classpath is logged at the INFO level. Use `-q` together with `mdep.outputFile` to keep the build output quiet. The separators and a path prefix can be changed with `mdep.pathSeparator`, `mdep.fileSeparator` and `mdep.prefix`, as for `dependency:build-classpath`. The file separator is applied to each path entry on its own. `dependency:analyze-exclusions` -------------------------------
