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:
John Dennis Casey 2005-06-13 14:49:49 +00:00
parent 64ac374aea
commit a55fd04696
5 changed files with 93 additions and 11 deletions

View File

@ -22,6 +22,7 @@ import org.apache.maven.plugin.MojoExecutionException;
import java.io.IOException; import java.io.IOException;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.util.List;
/** /**
* @goal touch * @goal touch
@ -39,6 +40,12 @@ public class CoreItMojo
*/ */
private String outputDirectory; 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" * @parameter expression="target/test-basedir-alignment"
*/ */

View File

@ -185,7 +185,6 @@ public class DefaultPluginManager
// TODO: this should be possibly outside // TODO: this should be possibly outside
if ( version == null ) if ( version == null )
{ {
Plugin pluginConfig = null; Plugin pluginConfig = null;
for ( Iterator it = project.getBuildPlugins().iterator(); it.hasNext(); ) 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 ) ); 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 finally
{ {
@ -413,8 +418,11 @@ public class DefaultPluginManager
// PlexusConfiguration mergedConfiguration = mergeConfiguration( pomConfiguration, // PlexusConfiguration mergedConfiguration = mergeConfiguration( pomConfiguration,
// mojoDescriptor.getConfiguration() ); // mojoDescriptor.getConfiguration() );
ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session, pathTranslator, ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session,
pluginDescriptor,
pathTranslator,
getLogger() ); getLogger() );
checkRequiredParameters( mojoDescriptor, mergedConfiguration, expressionEvaluator, plugin ); checkRequiredParameters( mojoDescriptor, mergedConfiguration, expressionEvaluator, plugin );
populatePluginFields( plugin, mojoDescriptor, mergedConfiguration, pluginContainer, expressionEvaluator ); populatePluginFields( plugin, mojoDescriptor, mergedConfiguration, pluginContainer, expressionEvaluator );

View File

@ -17,6 +17,7 @@ package org.apache.maven.plugin;
*/ */
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.path.PathTranslator; import org.apache.maven.project.path.PathTranslator;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
@ -39,9 +40,12 @@ public class PluginParameterExpressionEvaluator
private final Logger logger; 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.context = context;
this.pluginDescriptor = pluginDescriptor;
this.pathTranslator = pathTranslator; this.pathTranslator = pathTranslator;
this.logger = logger; this.logger = logger;
} }
@ -108,6 +112,30 @@ public class PluginParameterExpressionEvaluator
e ); 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 ) ) else if ( "settings".equals( expression ) )
{ {
value = context.getSettings(); value = context.getSettings();

View File

@ -16,12 +16,15 @@ package org.apache.maven.plugin;
* limitations under the License. * 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.ArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build; import org.apache.maven.model.Build;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.monitor.event.DefaultEventDispatcher; import org.apache.maven.monitor.event.DefaultEventDispatcher;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings; import org.apache.maven.settings.Settings;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
@ -30,6 +33,7 @@ import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator
import java.io.File; import java.io.File;
import java.util.Collections; import java.util.Collections;
import java.util.List;
/** /**
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a> * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
@ -53,7 +57,7 @@ public class PluginParameterExpressionEvaluatorTest
MavenProject project = new MavenProject( model ); MavenProject project = new MavenProject( model );
project.setFile( new File( "pom.xml" ).getCanonicalFile() ); project.setFile( new File( "pom.xml" ).getCanonicalFile() );
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project ); ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( project, null );
Object value = expressionEvaluator.evaluate( "${project.build.directory}/classes" ); Object value = expressionEvaluator.evaluate( "${project.build.directory}/classes" );
String actual = new File( value.toString() ).getCanonicalPath(); String actual = new File( value.toString() ).getCanonicalPath();
@ -61,8 +65,7 @@ public class PluginParameterExpressionEvaluatorTest
assertEquals( expected, actual ); assertEquals( expected, actual );
} }
private static MavenSession createSession( MavenProject project, PlexusContainer container, private static MavenSession createSession( MavenProject project, PlexusContainer container, ArtifactRepository repo )
ArtifactRepository repo )
{ {
return new MavenSession( project, container, new Settings(), repo, new DefaultEventDispatcher(), return new MavenSession( project, container, new Settings(), repo, new DefaultEventDispatcher(),
Collections.EMPTY_LIST ); Collections.EMPTY_LIST );
@ -71,7 +74,7 @@ public class PluginParameterExpressionEvaluatorTest
public void testLocalRepositoryExtraction() public void testLocalRepositoryExtraction()
throws Exception throws Exception
{ {
ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( createDefaultProject() ); ExpressionEvaluator expressionEvaluator = createExpressionEvaluator( createDefaultProject(), null );
Object value = expressionEvaluator.evaluate( "${localRepository}" ); Object value = expressionEvaluator.evaluate( "${localRepository}" );
assertEquals( "local", ( (ArtifactRepository) value ).getId() ); assertEquals( "local", ( (ArtifactRepository) value ).getId() );
@ -87,19 +90,43 @@ public class PluginParameterExpressionEvaluatorTest
Model model = new Model(); Model model = new Model();
model.setBuild( build ); 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}" ); Object value = expressionEvaluator.evaluate( "${project.build.directory}/${project.build.finalName}" );
assertEquals( "expected-directory/expected-finalName", value ); 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() private MavenProject createDefaultProject()
{ {
return new MavenProject( new Model() ); return new MavenProject( new Model() );
} }
private ExpressionEvaluator createExpressionEvaluator( MavenProject project ) private ExpressionEvaluator createExpressionEvaluator( MavenProject project, PluginDescriptor pluginDescriptor )
throws Exception throws Exception
{ {
ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
@ -110,8 +137,8 @@ public class PluginParameterExpressionEvaluatorTest
PlexusContainer container = getContainer(); PlexusContainer container = getContainer();
MavenSession session = createSession( project, container, repo ); MavenSession session = createSession( project, container, repo );
ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session, null, ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session, pluginDescriptor,
container.getLogger() ); null, container.getLogger() );
return expressionEvaluator; return expressionEvaluator;
} }
} }

View File

@ -41,6 +41,8 @@ public class PluginDescriptor
private boolean inheritedByDefault = true; private boolean inheritedByDefault = true;
private List artifacts;
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// //
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@ -190,6 +192,16 @@ public class PluginDescriptor
this.inheritedByDefault = inheritedByDefault; this.inheritedByDefault = inheritedByDefault;
} }
public List getArtifacts()
{
return artifacts;
}
public void setArtifacts( List artifacts )
{
this.artifacts = artifacts;
}
public boolean equals( Object object ) public boolean equals( Object object )
{ {
if ( this == object ) if ( this == object )