reverted r1340586: readonly is useful to avoid normal configuration of a plugin attribute at use time

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1340685 13f79535-47bb-0310-9956-ffa450edef68
master
Herve Boutemy 2012-05-20 11:48:46 +00:00
parent f794cc3566
commit 99ff18a545
12 changed files with 44 additions and 11 deletions

View File

@ -64,4 +64,16 @@ public @interface Parameter
* @return <code>true</code> if the Mojo should fail when the parameter cannot be injected
*/
boolean required() default false;
/**
* Specifies that this parameter cannot be configured directly by the user (as in the case of POM-specified
* configuration). This is useful when you want to force the user to use common POM elements rather than plugin
* configurations, as in the case where you want to use the artifact's final name as a parameter. In this case, you
* want the user to modify <code>&lt;build&gt;&lt;finalName/&gt;&lt;/build&gt;</code> rather than specifying a value
* for finalName directly in the plugin configuration section. It is also useful to ensure that - for example - a
* List-typed parameter which expects items of type Artifact doesn't get a List full of Strings.
*
* @return <code>true</code> if the user should not be allowed to configure the parameter directly
*/
boolean readonly() default false;
}

View File

@ -42,7 +42,7 @@ public abstract class AbstractFirstMojo
/**
* Project directory.
*/
@Parameter( defaultValue = "${basedir}" )
@Parameter( defaultValue = "${basedir}", readonly = true )
protected File basedir;
@Parameter( property = "first.touchFile", defaultValue = "${project.build.directory}/touch.txt",

View File

@ -69,7 +69,7 @@ assert parameter.alias.isEmpty()
assert parameter.type.text() == 'java.io.File'
assert parameter.deprecated.isEmpty()
assert parameter.required.text() == 'false'
assert parameter.editable.text() == 'true'
assert parameter.editable.text() == 'false'
assert parameter.description.text() == 'Project directory.'
mojo = pluginDescriptor.mojos.mojo.findAll{ it.goal.text() == "second"}[0]

View File

@ -42,7 +42,7 @@ public abstract class AbstractFirstMojo
/**
* Project directory.
*/
@Parameter( defaultValue = "${basedir}" )
@Parameter( defaultValue = "${basedir}", readonly = true )
protected File basedir;
@Parameter( property = "first.touchFile", defaultValue = "${project.build.directory}/touch.txt",

View File

@ -53,7 +53,7 @@ public class FirstMojo
@Component( role = "org.apache.maven.project.MavenProjectHelper" )//, roleHint = "default"
private Object projectHelper;
@Parameter( defaultValue = "${project.artifacts}", required = true )
@Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true )
private Set<Artifact> dependencies;
public void execute()

View File

@ -73,7 +73,7 @@ assert parameter.alias.isEmpty()
assert parameter.type.text() == 'java.io.File'
assert parameter.deprecated.isEmpty()
assert parameter.required.text() == 'false'
assert parameter.editable.text() == 'true'
assert parameter.editable.text() == 'false'
assert parameter.description.text() == 'Project directory.'
mojo = pluginDescriptor.mojos.mojo.findAll{ it.goal.text() == "second"}[0]

View File

@ -45,7 +45,7 @@ public class FirstMojo
/**
* Project directory.
*/
@Parameter( defaultValue = "${basedir}" )
@Parameter( defaultValue = "${basedir}", readonly = true )
private File basedir;
@Parameter( property = "first.touchFile", defaultValue = "${project.build.directory}/touch.txt",

View File

@ -67,7 +67,7 @@ assert parameter.alias.isEmpty()
assert parameter.type.text() == 'java.io.File'
assert parameter.deprecated.isEmpty()
assert parameter.required.text() == 'false'
assert parameter.editable.text() == 'true'
assert parameter.editable.text() == 'false'
assert parameter.description.text() == 'Project directory.'
mojo = pluginDescriptor.mojos.mojo.findAll{ it.goal.text() == "second" }[0]

View File

@ -58,7 +58,7 @@ public class DummyReport
/**
* The Maven Project.
*/
@Parameter( property = "project", required = true )
@Parameter( property = "project", readonly = true, required = true )
private MavenProject project;

View File

@ -490,6 +490,7 @@ public class JavaAnnotationsMojoDescriptorExtractor
parameter.setDefaultValue( parameterAnnotationContent.defaultValue() );
parameter.setDeprecated( parameterAnnotationContent.getDeprecated() );
parameter.setDescription( parameterAnnotationContent.getDescription() );
parameter.setEditable( !parameterAnnotationContent.readonly() );
String property = parameterAnnotationContent.property();
if ( StringUtils.contains( property, '$' ) || StringUtils.contains( property, '{' )
|| StringUtils.contains( property, '}' ) )

View File

@ -40,6 +40,8 @@ public class ParameterAnnotationContent
private boolean required = false;
private boolean readonly = false;
private String className;
public ParameterAnnotationContent( String fieldName, String className )
@ -49,13 +51,14 @@ public class ParameterAnnotationContent
}
public ParameterAnnotationContent( String fieldName, String alias, String property, String defaultValue,
boolean required, String className )
boolean required, boolean readonly, String className )
{
this( fieldName, className );
this.alias = alias;
this.property = property;
this.defaultValue = defaultValue;
this.required = required;
this.readonly = readonly;
}
public String alias()
@ -98,6 +101,16 @@ public class ParameterAnnotationContent
this.required = required;
}
public boolean readonly()
{
return readonly;
}
public void readonly( boolean readonly )
{
this.readonly = readonly;
}
public Class<? extends Annotation> annotationType()
{
return null;
@ -123,6 +136,7 @@ public class ParameterAnnotationContent
sb.append( ", property='" ).append( property ).append( '\'' );
sb.append( ", defaultValue='" ).append( defaultValue ).append( '\'' );
sb.append( ", required=" ).append( required );
sb.append( ", readonly=" ).append( readonly );
sb.append( '}' );
return sb.toString();
}
@ -141,6 +155,10 @@ public class ParameterAnnotationContent
ParameterAnnotationContent that = (ParameterAnnotationContent) o;
if ( readonly != that.readonly )
{
return false;
}
if ( required != that.required )
{
return false;
@ -175,6 +193,7 @@ public class ParameterAnnotationContent
result = 31 * result + ( property != null ? property.hashCode() : 0 );
result = 31 * result + ( defaultValue != null ? defaultValue.hashCode() : 0 );
result = 31 * result + ( required ? 1 : 0 );
result = 31 * result + ( readonly ? 1 : 0 );
return result;
}
}

View File

@ -83,7 +83,8 @@ public class TestAnnotationsReader
Collection<ParameterAnnotationContent> parameters = mojoAnnotatedClass.getParameters().values();
Assertions.assertThat( parameters ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
new ParameterAnnotationContent( "bar", null, "thebar", "coolbar", true, String.class.getName() ),
new ParameterAnnotationContent( "beer", null, "thebeer", "coolbeer", false, String.class.getName() ) );
new ParameterAnnotationContent( "bar", null, "thebar", "coolbar", true, false, String.class.getName() ),
new ParameterAnnotationContent( "beer", null, "thebeer", "coolbeer", false, false,
String.class.getName() ) );
}
}