Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -86,6 +88,21 @@ public void execute() throws MojoExecutionException {
artifact.getDependencyConflictId(),
artifact.getFile().getAbsolutePath());
}

PluginDescriptor pluginDescriptor = (PluginDescriptor) getPluginContext().get("pluginDescriptor");

if (pluginDescriptor != null) {
List<Artifact> pluginArtifacts = pluginDescriptor.getArtifacts();

if (pluginArtifacts != null) {
for (Artifact artifact : pluginArtifacts) {
this.project.getProperties()
.setProperty(
artifact.getDependencyConflictId(),
artifact.getFile().getAbsolutePath());
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
Set<Artifact> 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()));
}
}
}