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 dbc568dafc35e2a52973e6a146dd85a61ea6e2a0 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 13:11:15 2026 +0100 Set dependency versions without a file and test them in later plugin configuration --- src/it/projects/dependency-properties/pom.xml | 11 +++++++++++ src/it/projects/dependency-properties/verify.groovy | 8 ++++++++ .../maven/plugins/dependency/PropertiesMojo.java | 21 +++++++++++---------- src/site/markdown/usage.md.vm | 15 ++++----------- .../plugins/dependency/TestPropertiesMojo.java | 5 +++-- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/it/projects/dependency-properties/pom.xml b/src/it/projects/dependency-properties/pom.xml index 246859ef..a857ded0 100644 --- a/src/it/projects/dependency-properties/pom.xml +++ b/src/it/projects/dependency-properties/pom.xml @@ -63,6 +63,17 @@ <outputFile>${project.build.directory}/project.properties</outputFile> </configuration> </execution> + <execution> + <id>test-2</id> + <goals> + <goal>write-project-properties</goal> + </goals> + <phase>initialize</phase> + <configuration> + <!-- the version property set by dependency:properties above must be visible here --> + <outputFile>${project.build.directory}/interpolated-${org.apache.maven:maven-artifact:jar.version}.properties</outputFile> + </configuration> + </execution> </executions> </plugin> </plugins> diff --git a/src/it/projects/dependency-properties/verify.groovy b/src/it/projects/dependency-properties/verify.groovy index 7a715cc7..a1bd88e0 100644 --- a/src/it/projects/dependency-properties/verify.groovy +++ b/src/it/projects/dependency-properties/verify.groovy @@ -36,4 +36,12 @@ assert (new File( mavenArtifactPath )).isFile() : "maven-artifact jar is not a f 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" ) +// transitive dependency of maven-artifact 2.0.9 +String plexusUtilsVersion = props.getProperty( "org.codehaus.plexus:plexus-utils:jar.version" ) +assert "1.5.1".equals( plexusUtilsVersion ) : "plexus-utils version: " + plexusUtilsVersion + +// the version property is visible to the configuration of a plugin running after the goal +File interpolated = new File( basedir, "target/interpolated-2.0.9.properties" ) +assert interpolated.isFile() : "version property not interpolated into a later plugin configuration" + 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 917301a4..fc4c8b77 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java @@ -77,10 +77,11 @@ public class PropertiesMojo extends AbstractMojo { private boolean skip; /** - * Also set a property holding the resolved version of each artifact. The property is named like the path property + * Also set a property holding the version of the resolved 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}. + * set for the same artifacts as the path property, including the {@link #extraArtifacts}, and also for an + * artifact that has no file. An extra artifact with the same key overrides the value of the project dependency. * * @since 3.12.0 */ @@ -129,12 +130,12 @@ 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."); } + if (includeVersions) { + project.getProperties().setProperty(conflictId + VERSION_SUFFIX, artifact.getBaseVersion()); + } } if (extraArtifacts != null) { @@ -152,15 +153,15 @@ 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."); } + if (includeVersions) { + this.project + .getProperties() + .setProperty(conflictId + VERSION_SUFFIX, artifact.getBaseVersion()); + } } } catch (ArtifactResolutionException | ArtifactDescriptorException e) { throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e); diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm index c9206fc8..b11afec0 100644 --- a/src/site/markdown/usage.md.vm +++ b/src/site/markdown/usage.md.vm @@ -404,17 +404,13 @@ 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: +Set `includeVersions` (user property `mdep.properties.includeVersions`) to also get a property with the version of the resolved artifact, named like the path property plus a `.version` suffix. This includes transitive dependencies, and the version is set even for an artifact that has no file. For a snapshot dependency the base version, for example `1.0-SNAPSHOT`, is used. An artifact from `extraArtifacts` with the same key overrides the value of the project dependency. The suffix can onl [...] -``` -mvn dependency:properties -Dmdep.properties.includeVersions=true -``` - -or in the `pom.xml`: +Bind the goal in the `pom.xml` and let the user property switch the versions on: ```xml <plugin> @@ -426,15 +422,12 @@ or in the `pom.xml`: <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. +Then run `mvn verify -Dmdep.properties.includeVersions=true`. A plugin that runs after the goal can read the property in its configuration, for example `#[[${org.slf4j:slf4j-api:jar.version}]]#` resolves to the version Maven selected, such as `2.0.16`. To always set the versions, put `<includeVersions>true</includeVersions>` in the `<configuration>` of the execution. `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 8651212a..44ba3256 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java @@ -153,7 +153,7 @@ class TestPropertiesMojo { @Test @InjectMojo(goal = "properties") - void testIncludeVersionsSkipsArtifactWithoutFile(PropertiesMojo mojo) throws Exception { + void testIncludeVersionsSetForArtifactWithoutFile(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))); @@ -162,7 +162,8 @@ class TestPropertiesMojo { mojo.execute(); - assertFalse(project.getProperties().containsKey("group:artifact:jar.version")); + assertFalse(project.getProperties().containsKey("group:artifact:jar")); + assertEquals("1.0", project.getProperties().getProperty("group:artifact:jar.version")); } @Test
