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 22f0d8357..6e46a72a7 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java @@ -20,11 +20,13 @@ import javax.inject.Inject; +import java.util.List; import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -68,7 +70,7 @@ public PropertiesMojo(MavenProject project) { /** * Main entry into mojo. Gets the list of dependencies and iterates through setting a property for each artifact. - * + * Gets the list of declared plugin's dependencies and iterates through setting a property for each artifact. * @throws MojoExecutionException with a message if an error occurs */ @Override @@ -86,6 +88,21 @@ public void execute() throws MojoExecutionException { artifact.getDependencyConflictId(), artifact.getFile().getAbsolutePath()); } + + PluginDescriptor pluginDescriptor = (PluginDescriptor) getPluginContext().get("pluginDescriptor"); + + if (pluginDescriptor != null) { + List pluginArtifacts = pluginDescriptor.getArtifacts(); + + if (pluginArtifacts != null) { + for (Artifact artifact : pluginArtifacts) { + this.project.getProperties() + .setProperty( + artifact.getDependencyConflictId(), + artifact.getFile().getAbsolutePath()); + } + } + } } /** 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 2e3db085f..9baf0ea38 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java @@ -19,10 +19,14 @@ package org.apache.maven.plugins.dependency; import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub; import org.apache.maven.project.MavenProject; @@ -77,4 +81,37 @@ public void testSetProperties() throws Exception { assertTrue(artifactFile.isFile()); } } + + /** + * tests the proper discovery and configuration of the mojo for plugin dependencies + * Each plugin dependency should set a property in the project + * @throws Exception in case of errors + */ + public void testSetPropertiesForPluginDependencies() throws Exception { + File testPom = new File(getBasedir(), "target/test-classes/unit/properties-test/plugin-config.xml"); + PropertiesMojo mojo = (PropertiesMojo) lookupMojo("properties", testPom); + + assertNotNull(mojo); + MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); + assertNotNull(project); + + Set artifacts = this.stubFactory.getScopedArtifacts(); + Set directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts(); + artifacts.addAll(directArtifacts); + + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + pluginDescriptor.setArtifacts(new ArrayList<>()); + Map pluginContext = new HashMap<>(); + pluginContext.put("pluginDescriptor", pluginDescriptor); + mojo.setPluginContext(pluginContext); + + mojo.execute(); + + for (Artifact artifact : artifacts) { + File artifactFile = artifact.getFile(); + String depId = artifact.getDependencyConflictId(); + assertTrue(project.getProperties().containsKey(depId)); + assertTrue(project.getProperties().getProperty(depId).equals(artifactFile.getAbsolutePath())); + } + } }