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 e4d422212f2ed5b48f1318f2b31e71c236a4af88 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 12:52:53 2026 +0100 Add includeVersions to dependency:properties --- .../dependency-properties/invoker.properties | 2 +- .../projects/dependency-properties/verify.groovy | 4 ++ .../maven/plugins/dependency/PropertiesMojo.java | 24 +++++++- src/site/markdown/usage.md.vm | 33 +++++++++++ .../plugins/dependency/TestPropertiesMojo.java | 69 ++++++++++++++++++++++ 5 files changed, 130 insertions(+), 2 deletions(-) diff --git a/src/it/projects/dependency-properties/invoker.properties b/src/it/projects/dependency-properties/invoker.properties index 1e1876a7..5e3c94ac 100644 --- a/src/it/projects/dependency-properties/invoker.properties +++ b/src/it/projects/dependency-properties/invoker.properties @@ -15,4 +15,4 @@ # specific language governing permissions and limitations # under the License. -invoker.goals = initialize +invoker.goals = initialize -Dmdep.properties.includeVersions=true diff --git a/src/it/projects/dependency-properties/verify.groovy b/src/it/projects/dependency-properties/verify.groovy index 88081094..7a715cc7 100644 --- a/src/it/projects/dependency-properties/verify.groovy +++ b/src/it/projects/dependency-properties/verify.groovy @@ -32,4 +32,8 @@ String mavenArtifactPath = props.getProperty( "org.apache.maven:maven-artifact:j assert mavenArtifactPath != null : "org.apache.maven:maven-artifact:jar is null"; assert (new File( mavenArtifactPath )).isFile() : "maven-artifact jar is not a file: " + mavenArtifactPath; +// bound through the mdep.properties.includeVersions user property +assert "1.0".equals( props.getProperty( "org.apache.maven.its.dependency:unpack-fixture:jar.version" ) ) : "unpack-fixture version: " + props.getProperty( "org.apache.maven.its.dependency:unpack-fixture:jar.version" ) +assert "2.0.9".equals( props.getProperty( "org.apache.maven:maven-artifact:jar.version" ) ) : "maven-artifact version: " + props.getProperty( "org.apache.maven:maven-artifact:jar.version" ) + return true; diff --git a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java index 25ccea6a..917301a4 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java @@ -40,7 +40,8 @@ import org.eclipse.aether.resolution.ArtifactResolutionException; /** * Goal that sets a property pointing to the artifact file for each project dependency. For each dependency (direct and * transitive) a project property will be set which follows the <code>groupId:artifactId:type:[classifier]</code> form - * and contains the path to the resolved artifact. + * and contains the path to the resolved artifact. When {@link #includeVersions} is enabled, a second property named + * like the first one with a <code>.version</code> suffix holds the resolved version. * * @author Paul Gier * @since 2.2 @@ -52,6 +53,8 @@ import org.eclipse.aether.resolution.ArtifactResolutionException; threadSafe = true) public class PropertiesMojo extends AbstractMojo { + private static final String VERSION_SUFFIX = ".version"; + /** * The current Maven project. */ @@ -73,6 +76,17 @@ public class PropertiesMojo extends AbstractMojo { @Parameter(property = "mdep.skip", defaultValue = "false") private boolean skip; + /** + * Also set a property holding the resolved version of each artifact. The property is named like the path property + * with a <code>.version</code> suffix, for example <code>org.slf4j:slf4j-api:jar.version</code>. For a snapshot + * the base version (<code>1.0-SNAPSHOT</code>) is used, not the timestamped version of the resolved file. It is + * set for the same artifacts as the path property, including the {@link #extraArtifacts}. + * + * @since 3.12.0 + */ + @Parameter(property = "mdep.properties.includeVersions", defaultValue = "false") + private boolean includeVersions; + /** * Extra artifacts that can be provided to the plugin. For each artifact in this list a property will be set * similar to the project artifacts discovered. This allows callers to supply additional @@ -115,6 +129,9 @@ public class PropertiesMojo extends AbstractMojo { File file = artifact.getFile(); if (file != null) { project.getProperties().setProperty(conflictId, file.getAbsolutePath()); + if (includeVersions) { + project.getProperties().setProperty(conflictId + VERSION_SUFFIX, artifact.getBaseVersion()); + } } else { getLog().warn("Artifact " + conflictId + " has no associated file; no property will be set for it."); } @@ -135,6 +152,11 @@ public class PropertiesMojo extends AbstractMojo { File file = artifact.getFile(); if (file != null) { this.project.getProperties().setProperty(conflictId, file.getAbsolutePath()); + if (includeVersions) { + this.project + .getProperties() + .setProperty(conflictId + VERSION_SUFFIX, artifact.getBaseVersion()); + } } else { getLog().warn("Extra artifact " + conflictId + " has no associated file; no property will be set for it."); diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm index a761a54f..c9206fc8 100644 --- a/src/site/markdown/usage.md.vm +++ b/src/site/markdown/usage.md.vm @@ -403,6 +403,39 @@ This is the same as the resolve goal except it resolves plugins and optionally t This goal resolves the same artifacts as calling `mvn dependency:resolve dependency:resolve-plugins`. In addition, it resolves the artifacts listed in the `annotationProcessorPaths` of the `maven-compiler-plugin`, which Maven 3 resolves only when it compiles. The paths are read from the plugin configuration and from each execution, and their transitive dependencies are resolved too (with the project's `dependencyManagement` when `annotationProcessorPathsUseDepMgmt` is `true`). The scope [...] +`dependency:properties` +---------------------- + +This goal sets a project property for each project dependency (direct and transitive), named `<groupId>:<artifactId>:<type>[:<classifier>]`, that holds the absolute path of the resolved artifact file. It is bound to the `initialize` phase by default. + +Set `includeVersions` (user property `mdep.properties.includeVersions`) to also get a property with the resolved version, named like the path property plus a `.version` suffix. For a snapshot dependency the base version, for example `1.0-SNAPSHOT`, is used. To enable it from the command line: + +``` +mvn dependency:properties -Dmdep.properties.includeVersions=true +``` + +or in the `pom.xml`: + +```xml +<plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <version>${project.version}</version> + <executions> + <execution> + <goals> + <goal>properties</goal> + </goals> + <configuration> + <includeVersions>true</includeVersions> + </configuration> + </execution> + </executions> +</plugin> +``` + +With this, `${org.slf4j:slf4j-api:jar.version}` resolves to the version Maven selected, for example `2.0.16`, in the configuration of plugins that run after this goal. + `dependency:purge-local-repository` ----------------------------------- diff --git a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java index 12e40c7b..8651212a 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java @@ -36,6 +36,7 @@ import org.codehaus.plexus.util.ReflectionUtils; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.contains; @@ -122,4 +123,72 @@ class TestPropertiesMojo { assertFalse(project.getProperties().containsKey("group:artifact")); verify(log).warn(contains("group:artifact")); } + + @Test + @InjectMojo(goal = "properties") + void testVersionsNotSetByDefault(PropertiesMojo mojo) throws Exception { + Artifact artifact = mockArtifact("group:artifact:jar", "1.0", "1.0"); + when(this.project.getArtifacts()).thenReturn(new HashSet<>(Arrays.asList(artifact))); + + mojo.execute(); + + assertTrue(project.getProperties().containsKey("group:artifact:jar")); + assertFalse(project.getProperties().containsKey("group:artifact:jar.version")); + } + + @Test + @InjectMojo(goal = "properties") + void testIncludeVersionsUsesBaseVersion(PropertiesMojo mojo) throws Exception { + Artifact release = mockArtifact("group:release:jar", "2.0.16", "2.0.16"); + Artifact snapshot = mockArtifact("group:snapshot:jar:tests", "1.0-20260101.101010-1", "1.0-SNAPSHOT"); + when(this.project.getArtifacts()).thenReturn(new HashSet<>(Arrays.asList(release, snapshot))); + ReflectionUtils.setVariableValueInObject(mojo, "includeVersions", true); + + mojo.execute(); + + assertEquals("2.0.16", project.getProperties().getProperty("group:release:jar.version")); + assertEquals("1.0-SNAPSHOT", project.getProperties().getProperty("group:snapshot:jar:tests.version")); + assertTrue(project.getProperties().getProperty("group:release:jar").endsWith("group-release.jar")); + } + + @Test + @InjectMojo(goal = "properties") + void testIncludeVersionsSkipsArtifactWithoutFile(PropertiesMojo mojo) throws Exception { + Artifact artifact = mockArtifact("group:artifact:jar", "1.0", "1.0"); + when(artifact.getFile()).thenReturn(null); + when(this.project.getArtifacts()).thenReturn(new HashSet<>(Arrays.asList(artifact))); + mojo.setLog(Mockito.mock(Log.class)); + ReflectionUtils.setVariableValueInObject(mojo, "includeVersions", true); + + mojo.execute(); + + assertFalse(project.getProperties().containsKey("group:artifact:jar.version")); + } + + @Test + @InjectMojo(goal = "properties") + void testIncludeVersionsForExtraArtifacts(PropertiesMojo mojo) throws Exception { + ParamArtifact a1 = new ParamArtifact(); + a1.setGroupId("org.apache.commons"); + a1.setArtifactId("commons-lang3"); + a1.setVersion("3.6"); + ReflectionUtils.setVariableValueInObject(mojo, "extraArtifacts", Arrays.asList(a1)); + ReflectionUtils.setVariableValueInObject(mojo, "includeVersions", true); + + mojo.execute(); + + assertEquals("3.6", project.getProperties().getProperty("org.apache.commons:commons-lang3:jar.version")); + } + + private static Artifact mockArtifact(String conflictId, String version, String baseVersion) { + Artifact artifact = Mockito.mock(Artifact.class); + when(artifact.getDependencyConflictId()).thenReturn(conflictId); + when(artifact.getFile()) + .thenReturn(new File( + MojoExtension.getBasedir(), + conflictId.split(":")[0] + "-" + conflictId.split(":")[1] + ".jar")); + when(artifact.getVersion()).thenReturn(version); + when(artifact.getBaseVersion()).thenReturn(baseVersion); + return artifact; + } }
