mirror of https://github.com/apache/maven.git
o Adding ${plugin.*} expression support for mojos. Specifically, added support for ${plugin.artifacts} to retrieve the plugin's classpath artifacts. NOTE: There may be artifacts which are FILTERED by the resolution process, since they are provided in the maven distro. NOT SURE HOW BEST TO RESOLVE THAT...but it'll cause problems with people forking certain mojos, undoubtedly.
May resolve: MNG-455 git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@190413 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
64ac374aea
commit
a55fd04696
|
@ -22,6 +22,7 @@ import org.apache.maven.plugin.MojoExecutionException;
|
|||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @goal touch
|
||||
|
@ -39,6 +40,12 @@ public class CoreItMojo
|
|||
*/
|
||||
private String outputDirectory;
|
||||
|
||||
/** Test setting of plugin-artifacts on the PluginDescriptor instance.
|
||||
* @parameter expression="${plugin.artifacts}"
|
||||
* @required
|
||||
*/
|
||||
private List pluginArtifacts;
|
||||
|
||||
/**
|
||||
* @parameter expression="target/test-basedir-alignment"
|
||||
*/
|
||||
|
|
|
@ -185,7 +185,6 @@ public class DefaultPluginManager
|
|||
// TODO: this should be possibly outside
|
||||
if ( version == null )
|
||||
{
|
||||
|
||||
Plugin pluginConfig = null;
|
||||
|
||||
for ( Iterator it = project.getBuildPlugins().iterator(); it.hasNext(); )
|
||||
|
@ -299,6 +298,12 @@ public class DefaultPluginManager
|
|||
}
|
||||
|
||||
container.createChildContainer( pluginKey, files, Collections.EMPTY_MAP, Collections.singletonList( this ) );
|
||||
|
||||
// this plugin's descriptor should have been discovered by now, so we should be able to circle
|
||||
// around and set the artifacts.
|
||||
PluginDescriptor addedPlugin = (PluginDescriptor) pluginDescriptors.get( pluginKey );
|
||||
|
||||
addedPlugin.setArtifacts( new ArrayList( resolved.values() ) );
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -413,8 +418,11 @@ public class DefaultPluginManager
|
|||
// PlexusConfiguration mergedConfiguration = mergeConfiguration( pomConfiguration,
|
||||
// mojoDescriptor.getConfiguration() );
|
||||
|
||||
ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session, pathTranslator,
|
||||
ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session,
|
||||
pluginDescriptor,
|
||||
pathTranslator,
|
||||
getLogger() );
|
||||
|
||||
checkRequiredParameters( mojoDescriptor, mergedConfiguration, expressionEvaluator, plugin );
|
||||
|
||||
populatePluginFields( plugin, mojoDescriptor, mergedConfiguration, pluginContainer, expressionEvaluator );
|
||||
|
|
|
@ -17,6 +17,7 @@ package org.apache.maven.plugin;
|
|||
*/
|
||||
|
||||
import org.apache.maven.execution.MavenSession;
|
||||
import org.apache.maven.plugin.descriptor.PluginDescriptor;
|
||||
import org.apache.maven.project.path.PathTranslator;
|
||||
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
|
||||
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
|
||||
|
@ -39,9 +40,12 @@ public class PluginParameterExpressionEvaluator
|
|||
|
||||
private final Logger logger;
|
||||
|
||||
public PluginParameterExpressionEvaluator( MavenSession context, PathTranslator pathTranslator, Logger logger )
|
||||
private final PluginDescriptor pluginDescriptor;
|
||||
|
||||
public PluginParameterExpressionEvaluator( MavenSession context, PluginDescriptor pluginDescriptor, PathTranslator pathTranslator, Logger logger )
|
||||
{
|
||||
this.context = context;
|
||||
this.pluginDescriptor = pluginDescriptor;
|
||||
this.pathTranslator = pathTranslator;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
@ -108,6 +112,30 @@ public class PluginParameterExpressionEvaluator
|
|||
e );
|
||||
}
|
||||
}
|
||||
else if ( expression.startsWith( "plugin" ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
int pathSeparator = expression.indexOf( "/" );
|
||||
|
||||
if ( pathSeparator > 0 )
|
||||
{
|
||||
String pathExpression = expression.substring( 1, pathSeparator );
|
||||
value = ReflectionValueExtractor.evaluate( pathExpression, pluginDescriptor );
|
||||
value = value + expression.substring( pathSeparator );
|
||||
}
|
||||
else
|
||||
{
|
||||
value = ReflectionValueExtractor.evaluate( expression.substring( 1 ), pluginDescriptor );
|
||||
}
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
// TODO: don't catch exception
|
||||
throw new ExpressionEvaluationException( "Error evaluating plugin parameter expression: " + expression,
|
||||
e );
|
||||
}
|
||||
}
|
||||
else if ( "settings".equals( expression ) )
|
||||
{
|
||||
value = context.getSettings();
|
||||
|
|
|
@ -16,12 +16,15 @@ package org.apache.maven.plugin;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.DefaultArtifact;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
|
||||
import org.apache.maven.execution.MavenSession;
|
||||
import org.apache.maven.model.Build;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.monitor.event.DefaultEventDispatcher;
|
||||
import org.apache.maven.plugin.descriptor.PluginDescriptor;
|
||||
import org.apache.maven.project.MavenProject;
|
||||
import org.apache.maven.settings.Settings;
|
||||
import org.codehaus.plexus.PlexusContainer;
|
||||
|
@ -30,6 +33,7 @@ import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator
|
|||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
|
||||
|
@ -53,7 +57,7 @@ public class PluginParameterExpressionEvaluatorTest
|
|||
MavenProject project = new MavenProject( model );
|
||||
project.setFile( new File( "pom.xml" ).getCanonicalFile() );
|
||||
|
||||
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project );
|
||||
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project, null );
|
||||
|
||||
Object value = expressionEvaluator.evaluate( "${project.build.directory}/classes" );
|
||||
String actual = new File( value.toString() ).getCanonicalPath();
|
||||
|
@ -61,8 +65,7 @@ public class PluginParameterExpressionEvaluatorTest
|
|||
assertEquals( expected, actual );
|
||||
}
|
||||
|
||||
private static MavenSession createSession( MavenProject project, PlexusContainer container,
|
||||
ArtifactRepository repo )
|
||||
private static MavenSession createSession( MavenProject project, PlexusContainer container, ArtifactRepository repo )
|
||||
{
|
||||
return new MavenSession( project, container, new Settings(), repo, new DefaultEventDispatcher(),
|
||||
Collections.EMPTY_LIST );
|
||||
|
@ -71,7 +74,7 @@ public class PluginParameterExpressionEvaluatorTest
|
|||
public void testLocalRepositoryExtraction()
|
||||
throws Exception
|
||||
{
|
||||
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( createDefaultProject() );
|
||||
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( createDefaultProject(), null );
|
||||
Object value = expressionEvaluator.evaluate( "${localRepository}" );
|
||||
|
||||
assertEquals( "local", ( (ArtifactRepository) value ).getId() );
|
||||
|
@ -87,19 +90,43 @@ public class PluginParameterExpressionEvaluatorTest
|
|||
Model model = new Model();
|
||||
model.setBuild( build );
|
||||
|
||||
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( new MavenProject( model ) );
|
||||
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( new MavenProject( model ), null );
|
||||
|
||||
Object value = expressionEvaluator.evaluate( "${project.build.directory}/${project.build.finalName}" );
|
||||
|
||||
assertEquals( "expected-directory/expected-finalName", value );
|
||||
}
|
||||
|
||||
public void testShouldExtractPluginArtifacts()
|
||||
throws Exception
|
||||
{
|
||||
PluginDescriptor pd = new PluginDescriptor();
|
||||
|
||||
Artifact artifact = new DefaultArtifact( "testGroup", "testArtifact", "1.0", Artifact.SCOPE_COMPILE, "jar" );
|
||||
|
||||
pd.setArtifacts( Collections.singletonList( artifact ) );
|
||||
|
||||
ExpressionEvaluator ee = createExpressionEvaluator( createDefaultProject(), pd );
|
||||
|
||||
Object value = ee.evaluate( "${plugin.artifacts}" );
|
||||
|
||||
assertTrue( value instanceof List );
|
||||
|
||||
List artifacts = (List) value;
|
||||
|
||||
assertEquals( 1, artifacts.size() );
|
||||
|
||||
Artifact result = (Artifact) artifacts.get( 0 );
|
||||
|
||||
assertEquals( "testGroup", result.getGroupId() );
|
||||
}
|
||||
|
||||
private MavenProject createDefaultProject()
|
||||
{
|
||||
return new MavenProject( new Model() );
|
||||
}
|
||||
|
||||
private ExpressionEvaluator createExpressionEvaluator( MavenProject project )
|
||||
private ExpressionEvaluator createExpressionEvaluator( MavenProject project, PluginDescriptor pluginDescriptor )
|
||||
throws Exception
|
||||
{
|
||||
ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
|
||||
|
@ -110,8 +137,8 @@ public class PluginParameterExpressionEvaluatorTest
|
|||
PlexusContainer container = getContainer();
|
||||
MavenSession session = createSession( project, container, repo );
|
||||
|
||||
ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session, null,
|
||||
container.getLogger() );
|
||||
ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session, pluginDescriptor,
|
||||
null, container.getLogger() );
|
||||
return expressionEvaluator;
|
||||
}
|
||||
}
|
|
@ -40,6 +40,8 @@ public class PluginDescriptor
|
|||
private String source;
|
||||
|
||||
private boolean inheritedByDefault = true;
|
||||
|
||||
private List artifacts;
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
//
|
||||
|
@ -189,6 +191,16 @@ public class PluginDescriptor
|
|||
{
|
||||
this.inheritedByDefault = inheritedByDefault;
|
||||
}
|
||||
|
||||
public List getArtifacts()
|
||||
{
|
||||
return artifacts;
|
||||
}
|
||||
|
||||
public void setArtifacts( List artifacts )
|
||||
{
|
||||
this.artifacts = artifacts;
|
||||
}
|
||||
|
||||
public boolean equals( Object object )
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue