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 a74985dba122413a09e18640268ff93ea6106731 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 13:23:43 2026 +0100 Match copy-dependencies file names in build-classpath repository layout With useRepositoryLayout the file name is the base-version name that copy-dependencies writes; stripVersion, stripClassifier, useBaseVersion and prependGroupId are ignored there, with a warning. --- .../invoker.properties | 2 +- .../module-b/pom.xml | 6 ++- .../fromDependencies/BuildClasspathMojo.java | 45 ++++++++++++++-- src/site/markdown/usage.md.vm | 4 +- .../fromDependencies/TestBuildClasspathMojo.java | 63 ++++++++++++++++++++++ 5 files changed, 111 insertions(+), 9 deletions(-) diff --git a/src/it/projects/build-classpath-repository-layout/invoker.properties b/src/it/projects/build-classpath-repository-layout/invoker.properties index 2e199f7b..6beacbc1 100644 --- a/src/it/projects/build-classpath-repository-layout/invoker.properties +++ b/src/it/projects/build-classpath-repository-layout/invoker.properties @@ -15,4 +15,4 @@ # specific language governing permissions and limitations # under the License. -invoker.goals = clean package +invoker.goals = clean package -Dmdep.useRepositoryLayout=true diff --git a/src/it/projects/build-classpath-repository-layout/module-b/pom.xml b/src/it/projects/build-classpath-repository-layout/module-b/pom.xml index f2c8612b..6a49eaa7 100644 --- a/src/it/projects/build-classpath-repository-layout/module-b/pom.xml +++ b/src/it/projects/build-classpath-repository-layout/module-b/pom.xml @@ -71,6 +71,7 @@ <configuration> <outputFile>${project.build.directory}/prefix-only.txt</outputFile> <prefix>repository</prefix> + <useRepositoryLayout>false</useRepositoryLayout> </configuration> </execution> <execution> @@ -82,7 +83,10 @@ <configuration> <outputFile>${project.build.directory}/layout.txt</outputFile> <prefix>repository</prefix> - <useRepositoryLayout>true</useRepositoryLayout> + <!-- useRepositoryLayout comes from -Dmdep.useRepositoryLayout=true in invoker.properties --> + <!-- ignored with the layout --> + <stripVersion>true</stripVersion> + <prependGroupId>true</prependGroupId> <pathSeparator>:</pathSeparator> <fileSeparator>/</fileSeparator> </configuration> diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java index d948d402..5934b8df 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/BuildClasspathMojo.java @@ -85,8 +85,8 @@ public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements /** * The prefix to prepend on each dependent artifact. If undefined, the paths refer to the actual files of the * resolved artifacts: files in the local repository, or the build output (<code>target/classes</code> or the - * packaged file) for artifacts of modules in the same reactor. In that case the stripVersion, prependGroupId and - * useRepositoryLayout parameters do nothing. + * packaged file) for artifacts of modules in the same reactor. In that case the stripVersion, stripClassifier, + * useBaseVersion, prependGroupId and useRepositoryLayout parameters do nothing. * If defined, each path is the prefix followed by the artifact file name only (or by the * repository layout path if {@link #useRepositoryLayout} is {@code true}), independent of where the artifact * file actually is. @@ -98,6 +98,10 @@ public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements * Only used together with {@link #prefix}. If {@code true}, each path is built in the layout of a Maven * repository below the prefix, for example <code>prefix/junit/junit/3.8.1/junit-3.8.1.jar</code>, for every * artifact including those of modules in the same reactor, whose files are in the build output directory. + * The stripVersion, stripClassifier, useBaseVersion and prependGroupId parameters are ignored then, so that the + * classpath matches a directory filled by <code>copy-dependencies</code> with + * <code>-Dmdep.useRepositoryLayout=true</code>. The user property is shared with that goal. + * A reactor module must be packaged (run at <code>package</code> or later) for the path to name an existing jar. * * @since 3.12.0 */ @@ -211,6 +215,13 @@ public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements localRepoProperty = "${M2_REPO}"; } + if (prefix != null + && useRepositoryLayout + && (stripVersion || stripClassifier || prependGroupId || !useBaseVersion)) { + getLog().warn("useRepositoryLayout ignores stripVersion, stripClassifier, prependGroupId and " + + "useBaseVersion, to match the file names written by copy-dependencies."); + } + Set<Artifact> artifacts = getResolvedDependencies(true); if (artifacts.isEmpty()) { @@ -304,15 +315,18 @@ public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements sb.append(prefix); sb.append(File.separator); if (useRepositoryLayout) { + // same names as copy-dependencies with useRepositoryLayout: base version, no strip or prepend sb.append(artifact.getGroupId().replace('.', File.separatorChar)) .append(File.separatorChar) .append(artifact.getArtifactId()) .append(File.separatorChar) .append(artifact.getBaseVersion()) - .append(File.separatorChar); + .append(File.separatorChar) + .append(DependencyUtil.getFormattedFileName(artifact, false, false, true, false)); + } else { + sb.append(DependencyUtil.getFormattedFileName( + artifact, this.stripVersion, this.prependGroupId, this.useBaseVersion, this.stripClassifier)); } - sb.append(DependencyUtil.getFormattedFileName( - artifact, this.stripVersion, this.prependGroupId, this.useBaseVersion, this.stripClassifier)); } } @@ -462,6 +476,27 @@ public class BuildClasspathMojo extends AbstractDependencyFilterMojo implements this.stripVersion = theStripVersion; } + /** + * @param stripClassifier {@link #stripClassifier} + */ + public void setStripClassifier(boolean stripClassifier) { + this.stripClassifier = stripClassifier; + } + + /** + * @param useBaseVersion {@link #useBaseVersion} + */ + public void setUseBaseVersion(boolean useBaseVersion) { + this.useBaseVersion = useBaseVersion; + } + + /** + * @param useRepositoryLayout {@link #useRepositoryLayout} + */ + public void setUseRepositoryLayout(boolean useRepositoryLayout) { + this.useRepositoryLayout = useRepositoryLayout; + } + /** * @param localRepoProperty {@link #localRepoProperty} */ diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm index 19d7a644..bcb186ff 100644 --- a/src/site/markdown/usage.md.vm +++ b/src/site/markdown/usage.md.vm @@ -707,14 +707,14 @@ mvn dependency:build-classpath -Dmdep.outputFile=cp.txt Without `prefix`, each entry is the actual file of the artifact. In a multi-module build, the file of a module in the same reactor is in its build output (`target/classes`, or the packaged file after `package`), not in the local repository. To get a classpath that is the same for every artifact, set `prefix` together with `useRepositoryLayout`. Each entry then has the layout of a Maven repository below the prefix: ``` -mvn package dependency:build-classpath -Dmdep.prefix=repository -Dmdep.useRepositoryLayout=true -Dmdep.outputFile=cp.txt +mvn package dependency:build-classpath -Dmdep.prefix=repository -Dmdep.useRepositoryLayout=true -Dmdep.fileSeparator=/ -Dmdep.pathSeparator=: -Dmdep.outputFile=cp.txt ``` ``` repository/org/example/module-a/1.0-SNAPSHOT/module-a-1.0-SNAPSHOT.jar:repository/junit/junit/3.8.1/junit-3.8.1.jar ``` -`useRepositoryLayout` has no effect without `prefix`. With `prefix` alone, each entry is the prefix followed by the file name only. +`useRepositoryLayout` has no effect without `prefix`. With `prefix` alone, each entry is the prefix followed by the file name only. With the layout, `stripVersion`, `stripClassifier`, `useBaseVersion` and `prependGroupId` are ignored (the goal logs a warning), so the classpath matches a directory filled by `dependency:copy-dependencies -Dmdep.useRepositoryLayout=true`; the `mdep.useRepositoryLayout` user property is shared with that goal. A module of the same reactor must be packaged (ru [...] The goal can also be bound to a lifecycle phase with the following configuration: diff --git a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestBuildClasspathMojo.java b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestBuildClasspathMojo.java index b800906f..7a41a0a4 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestBuildClasspathMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestBuildClasspathMojo.java @@ -172,6 +172,69 @@ class TestBuildClasspathMojo { assertEquals("prefix" + File.separator + DependencyUtil.getFormattedFileName(artifact, true), sb.toString()); } + private static String sep(String path) { + return path.replace("/", File.separator); + } + + private String layoutPath(BuildClasspathMojo mojo, Artifact artifact) { + StringBuilder sb = new StringBuilder(); + mojo.appendArtifactPath(artifact, sb); + return sb.toString(); + } + + @Test + @InjectMojo(goal = "build-classpath") + void testRepositoryLayoutWithClassifier(BuildClasspathMojo mojo) { + Artifact artifact = new DefaultArtifact( + "org.test", "lib", "1.0", null, "jar", "sources", new DefaultArtifactHandler("jar")); + artifact.setFile(new File(testDir, "module/target/classes")); + + mojo.setPrefix("repository"); + mojo.setUseRepositoryLayout(true); + assertEquals(sep("repository/org/test/lib/1.0/lib-1.0-sources.jar"), layoutPath(mojo, artifact)); + + // negative control: without the layout only the file name is used + mojo.setUseRepositoryLayout(false); + assertEquals(sep("repository/lib-1.0-sources.jar"), layoutPath(mojo, artifact)); + } + + @Test + @InjectMojo(goal = "build-classpath") + void testRepositoryLayoutUsesBaseVersionOfTimestampedSnapshot(BuildClasspathMojo mojo) { + Artifact artifact = new DefaultArtifact( + "org.test", "lib", "1.0-20260101.120000-1", null, "jar", "", new DefaultArtifactHandler("jar")); + artifact.setBaseVersion("1.0-SNAPSHOT"); + artifact.setFile(new File(testDir, "lib.jar")); + + mojo.setPrefix("repository"); + mojo.setUseBaseVersion(false); + mojo.setUseRepositoryLayout(true); + assertEquals(sep("repository/org/test/lib/1.0-SNAPSHOT/lib-1.0-SNAPSHOT.jar"), layoutPath(mojo, artifact)); + + // negative control: without the layout useBaseVersion=false keeps the timestamp + mojo.setUseRepositoryLayout(false); + assertEquals(sep("repository/lib-1.0-20260101.120000-1.jar"), layoutPath(mojo, artifact)); + } + + @Test + @InjectMojo(goal = "build-classpath") + void testRepositoryLayoutIgnoresStripAndPrepend(BuildClasspathMojo mojo) { + Artifact artifact = + new DefaultArtifact("org.test", "lib", "1.0", null, "jar", "tests", new DefaultArtifactHandler("jar")); + artifact.setFile(new File(testDir, "lib.jar")); + + mojo.setPrefix("repository"); + mojo.setStripVersion(true); + mojo.setPrependGroupId(true); + mojo.setStripClassifier(true); + mojo.setUseRepositoryLayout(true); + assertEquals(sep("repository/org/test/lib/1.0/lib-1.0-tests.jar"), layoutPath(mojo, artifact)); + + // negative control: without the layout the same flags change the name + mojo.setUseRepositoryLayout(false); + assertEquals(sep("repository/org.test.lib.jar"), layoutPath(mojo, artifact)); + } + private Set<Artifact> getArtifacts() { Artifact artifact1 = new DefaultArtifact( "testGroupId", "release1", "1.0", null, "jar", "", new DefaultArtifactHandler("jar"));
