mirror of https://github.com/apache/maven.git
PR: MNG-2293
Submitted by: Jerome Lacoste Reviewed by: Kenney Westerhof Modifications: minor cosmetic changes. Add an 'implementation' parameter to @parameter annotation, so a default implementation can be specified in Mojo's in case the Mojo parameter is an interface. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@413054 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6e4e175bb5
commit
45748e167d
|
@ -0,0 +1,7 @@
|
|||
package org.apache.maven.plugin.coreit;
|
||||
|
||||
/**
|
||||
*/
|
||||
public interface Bla
|
||||
{
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package org.apache.maven.plugin.coreit;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
|
||||
/**
|
||||
* Check that we correctly use the implementation parameter. See MNG-2293
|
||||
*
|
||||
* @goal param-implementation
|
||||
* @description Prints out the name of the implementation of the bla field.
|
||||
*/
|
||||
public class ParameterImplementationMojo
|
||||
extends AbstractMojo
|
||||
{
|
||||
|
||||
/**
|
||||
* @parameter implementation="org.apache.maven.plugin.coreit.sub.MyBla"
|
||||
* @required
|
||||
*/
|
||||
private Bla bla;
|
||||
|
||||
/**
|
||||
* The expected value of bla.toString().
|
||||
*
|
||||
* @parameter
|
||||
* @required
|
||||
*/
|
||||
private String expected;
|
||||
|
||||
public void execute()
|
||||
throws MojoExecutionException
|
||||
{
|
||||
|
||||
getLog().info( "bla: " + bla );
|
||||
|
||||
if ( ! expected.equals( bla.toString() ) )
|
||||
{
|
||||
throw new MojoExecutionException( "Expected '" + expected + "'; found '" + bla + "'" );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package org.apache.maven.plugin.coreit.sub;
|
||||
|
||||
import org.apache.maven.plugin.coreit.Bla;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class MyBla
|
||||
implements Bla
|
||||
{
|
||||
private String field;
|
||||
|
||||
public String getField()
|
||||
{
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField( String field )
|
||||
{
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return getClass() + "-" + field;
|
||||
}
|
||||
}
|
|
@ -1,3 +1,11 @@
|
|||
Notes:
|
||||
- today, 3 sets of integration tests, categorized by their ids (it0xxx, it1xxx, it2xxx).
|
||||
see below for what these groups represent
|
||||
- creating a new test:
|
||||
- you can add mojos to the integration-tests plugins/maven-core-it-plugin
|
||||
- add log.txt and target to your it test svn ignore list
|
||||
|
||||
Details:
|
||||
-------------------------------------------------------------------------------
|
||||
it0000: The simplest of builds. We have one application class and one test
|
||||
class. There are no resources, no source generation, no resource
|
||||
|
@ -285,6 +293,8 @@ it0104: Verify that plugin configurations are resolved correctly, particularly
|
|||
when they contain ${project.build.directory} in the string value of a
|
||||
Map.Entry.
|
||||
|
||||
it0107: Verify that default implementation of an implementation for a complex object works as
|
||||
expected [MNG-2293]
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
- generated sources
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
it0107
|
||||
#it0104 Commenting out, not fixed until post-2.0.4, due to dependency on new plexus-container-default version.
|
||||
it0103
|
||||
it0102
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
-X
|
|
@ -0,0 +1 @@
|
|||
core-it:param-implementation
|
|
@ -0,0 +1,22 @@
|
|||
<model>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-core-it0107</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>3.8.1</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-core-it-plugin</artifactId>
|
||||
<version>1.1-SNAPSHOT</version>
|
||||
<goals>
|
||||
<goal>param-implementation</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<bla><field>foobar</field></bla>
|
||||
<expected>class org.apache.maven.plugin.coreit.sub.MyBla-foobar</expected>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</model>
|
|
@ -872,6 +872,7 @@ public class DefaultPluginManager
|
|||
|
||||
String paramName = parameter.getName();
|
||||
String alias = parameter.getAlias();
|
||||
String implementation = parameter.getImplementation();
|
||||
|
||||
PlexusConfiguration pomConfig = fromPom.getChild( paramName );
|
||||
PlexusConfiguration aliased = null;
|
||||
|
@ -895,7 +896,7 @@ public class DefaultPluginManager
|
|||
pomConfig = buildTopDownMergedConfiguration( pomConfig, aliased );
|
||||
}
|
||||
|
||||
boolean addedPomConfig = false;
|
||||
PlexusConfiguration toAdd = null;
|
||||
|
||||
if ( pomConfig != null )
|
||||
{
|
||||
|
@ -903,15 +904,29 @@ public class DefaultPluginManager
|
|||
|
||||
if ( StringUtils.isNotEmpty( pomConfig.getValue( null ) ) || pomConfig.getChildCount() > 0 )
|
||||
{
|
||||
result.addChild( pomConfig );
|
||||
|
||||
addedPomConfig = true;
|
||||
toAdd = pomConfig;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !addedPomConfig && mojoConfig != null )
|
||||
if ( toAdd == null && mojoConfig != null )
|
||||
{
|
||||
result.addChild( copyConfiguration( mojoConfig ) );
|
||||
toAdd = copyConfiguration( mojoConfig );
|
||||
}
|
||||
|
||||
if ( toAdd != null )
|
||||
{
|
||||
if ( implementation != null
|
||||
&& toAdd.getAttribute( "implementation", null ) == null )
|
||||
{
|
||||
|
||||
XmlPlexusConfiguration implementationConf = new XmlPlexusConfiguration( paramName );
|
||||
|
||||
implementationConf.setAttribute( "implementation", parameter.getImplementation() );
|
||||
|
||||
toAdd = buildTopDownMergedConfiguration( toAdd, implementationConf );
|
||||
}
|
||||
|
||||
result.addChild( toAdd );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,8 @@ public class Parameter
|
|||
|
||||
private String defaultValue;
|
||||
|
||||
private String implementation;
|
||||
|
||||
private Requirement requirement;
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
@ -160,4 +162,14 @@ public class Parameter
|
|||
{
|
||||
this.requirement = requirement;
|
||||
}
|
||||
|
||||
public String getImplementation()
|
||||
{
|
||||
return implementation;
|
||||
}
|
||||
|
||||
public void setImplementation( String implementation )
|
||||
{
|
||||
this.implementation = implementation;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -250,6 +250,8 @@ public class PluginDescriptorBuilder
|
|||
|
||||
parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
|
||||
|
||||
parameter.setImplementation( d.getChild( "implementation" ).getValue() );
|
||||
|
||||
parameters.add( parameter );
|
||||
}
|
||||
|
||||
|
|
|
@ -335,6 +335,11 @@ public class PluginDescriptorGenerator
|
|||
element( w, "deprecated", parameter.getDeprecated() );
|
||||
}
|
||||
|
||||
if ( parameter.getImplementation() != null )
|
||||
{
|
||||
element( w, "implementation", parameter.getImplementation() );
|
||||
}
|
||||
|
||||
element( w, "required", Boolean.toString( parameter.isRequired() ) );
|
||||
|
||||
element( w, "editable", Boolean.toString( parameter.isEditable() ) );
|
||||
|
|
|
@ -59,6 +59,11 @@ public class JavaMojoDescriptorExtractor
|
|||
|
||||
public static final String PARAMETER_DEFAULT_VALUE = "default-value";
|
||||
|
||||
/**
|
||||
* This defines the default implementation in the case the parameter type is an interface.
|
||||
*/
|
||||
public static final String PARAMETER_IMPLEMENTATION = "implementation";
|
||||
|
||||
/**
|
||||
* This indicates the base name of the bean properties used to read/write this parameter's value.
|
||||
* So:
|
||||
|
@ -422,6 +427,9 @@ public class JavaMojoDescriptorExtractor
|
|||
}
|
||||
|
||||
pd.setDefaultValue( parameter.getNamedParameter( PARAMETER_DEFAULT_VALUE ) );
|
||||
|
||||
pd.setImplementation( parameter.getNamedParameter( PARAMETER_IMPLEMENTATION ) );
|
||||
|
||||
}
|
||||
|
||||
mojoDescriptor.addParameter( pd );
|
||||
|
|
|
@ -19,12 +19,16 @@ package org.apache.maven.tools.plugin.extractor.java;
|
|||
import junit.framework.TestCase;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.project.MavenProject;
|
||||
import org.apache.maven.plugin.descriptor.MojoDescriptor;
|
||||
import org.apache.maven.plugin.descriptor.Parameter;
|
||||
import org.apache.maven.plugin.descriptor.PluginDescriptor;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import source2.sub.MyBla;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
|
@ -56,6 +60,40 @@ public class JavaMojoDescriptorExtractorTest
|
|||
assertEquals( 2, results.size() );
|
||||
}
|
||||
|
||||
public void testShouldPropagateImplementationParameter()
|
||||
throws Exception
|
||||
{
|
||||
JavaMojoDescriptorExtractor extractor = new JavaMojoDescriptorExtractor();
|
||||
|
||||
File sourceFile = fileOf( "dir-flag.txt" );
|
||||
System.out.println( "found source file: " + sourceFile );
|
||||
|
||||
File dir = sourceFile.getParentFile();
|
||||
|
||||
Model model = new Model();
|
||||
model.setArtifactId( "maven-unitTesting-plugin" );
|
||||
|
||||
MavenProject project = new MavenProject( model );
|
||||
|
||||
project.setFile( new File( dir, "pom.xml" ) );
|
||||
project.addCompileSourceRoot( new File( dir, "source2" ).getPath() );
|
||||
|
||||
PluginDescriptor pluginDescriptor = new PluginDescriptor();
|
||||
pluginDescriptor.setGoalPrefix( "test" );
|
||||
List results = extractor.execute( project, pluginDescriptor );
|
||||
assertEquals( 1, results.size() );
|
||||
|
||||
MojoDescriptor mojoDescriptor = (MojoDescriptor) results.get( 0 );
|
||||
|
||||
List parameters = mojoDescriptor.getParameters();
|
||||
|
||||
assertEquals( 1, parameters.size() );
|
||||
|
||||
Parameter parameter = (Parameter) parameters.get( 0 );
|
||||
|
||||
assertEquals( MyBla.class.getName(), parameter.getImplementation() );
|
||||
}
|
||||
|
||||
private File fileOf( String classpathResource )
|
||||
{
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package source2;
|
||||
|
||||
public interface Bla
|
||||
{
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package source2;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
|
||||
/**
|
||||
* Tests the implementation argument of the parameter annotation.
|
||||
*
|
||||
* @goal ideaThree
|
||||
* @requiresDependencyResolution compile
|
||||
*/
|
||||
public class JavaExtractorTestThree
|
||||
extends AbstractMojo
|
||||
{
|
||||
/**
|
||||
* @parameter implementation=source2.sub.MyBla
|
||||
* @required
|
||||
*/
|
||||
private Bla bla;
|
||||
|
||||
public JavaExtractorTestThree()
|
||||
{
|
||||
}
|
||||
|
||||
public void execute()
|
||||
{
|
||||
if ( getLog() != null )
|
||||
{
|
||||
getLog().info( "bla: " + bla );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package source2.sub;
|
||||
|
||||
import source2.Bla;
|
||||
|
||||
public class MyBla
|
||||
implements Bla
|
||||
{
|
||||
}
|
|
@ -216,7 +216,7 @@ public class DefaultModelValidatorTest
|
|||
private ModelValidationResult validate( String testName )
|
||||
throws Exception
|
||||
{
|
||||
Reader input = new FileReader( getFileForClasspathResource( "/validation/" + testName ) );
|
||||
Reader input = new FileReader( getFileForClasspathResource( "validation/" + testName ) );
|
||||
|
||||
MavenXpp3Reader reader = new MavenXpp3Reader();
|
||||
|
||||
|
|
Loading…
Reference in New Issue