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 @@ -27,7 +27,7 @@

/**
* Used to configure your Mojo parameters to be injected by
* <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/MavenPluginManager.html">
* <a href="https://maven.apache.org/ref/current/maven-core/apidocs/org/apache/maven/plugin/MavenPluginManager.html">
* <code>MavenPluginManager.getConfiguredMojo(...)</code></a>.
* <p>
* Beans injected into Mojo parameters are prepared by <a href="https://www.eclipse.org/sisu/">Sisu</a> JSR330-based
Expand All @@ -36,6 +36,9 @@
*
* @author Olivier Lamy
* @since 3.0
* @see <a href="https://maven.apache.org/guides/mini/guide-configuring-plugins.html">Configuring Plug-Ins</a>
* @see <a href="https://eclipse.dev/sisu/org.eclipse.sisu.plexus/configuration.html#Collection_Objects">Plexus Component Configurator API</a>
* @see <a href="https://jcp.org/en/jsr/detail?id=330">JSR 330</a>
*/
@Documented
@Retention(RetentionPolicy.CLASS)
Expand All @@ -62,14 +65,30 @@
String property() default "";

/**
* parameter default value, may contain <code>${...}</code> expressions which will be interpreted at
* Parameter default value, may contain <code>${...}</code> expressions which will be interpreted at
* inject time: see
* <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
* <a href="https://maven.apache.org/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
* PluginParameterExpressionEvaluator</a>.
* The type conversion from String into the target type is described in <a href="https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Configuring_Parameters">Configuring Parameters</a>.
* Cannot be specified together with {@link #defaultValues()}.
* @return the default value
* @see #defaultValues()
*/
String defaultValue() default "";

/**
* Default values concatenated with commas in the generated plugin descriptor.
* Each String value has the format outlined in {@link #defaultValue()}.
* Note that the String itself must not contain commas as those are always interpreted as value separation characters (with no way to escape them).
* Multiple values are only supported for <a href="https://maven.apache.org/guides/mini/guide-configuring-plugins.html#mapping-collections-and-arrays">collection or array type parameters</a>.
* Cannot be specified together with {@link #defaultValue()}, even if either is explicitly empty.
* Descriptor generation requires maven-plugin-plugin 4.0.0-beta-4 or later.
* @return the default values
* @see #defaultValue()
* @since 4.0.0-beta-4
*/
String[] defaultValues() default {};

/**
* is the parameter required?
* @return <code>true</code> if the Mojo should fail when the parameter cannot be injected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,43 +706,14 @@ private List<MojoDescriptor> toMojoDescriptors(

mojoDescriptor.setGoal(mojo.name());
mojoDescriptor.setOnlineRequired(mojo.requiresOnline());

mojoDescriptor.setPhase(mojo.defaultPhase().id());

// Parameter annotations
Map<String, ParameterAnnotationContent> parameters =
getParametersParentHierarchy(mojoAnnotatedClass, mojoAnnotatedClasses);

for (ParameterAnnotationContent parameterAnnotationContent : new TreeSet<>(parameters.values())) {
org.apache.maven.plugin.descriptor.Parameter parameter =
new org.apache.maven.plugin.descriptor.Parameter();
String name = StringUtils.isEmpty(parameterAnnotationContent.name())
? parameterAnnotationContent.getFieldName()
: parameterAnnotationContent.name();
parameter.setName(name);
parameter.setAlias(parameterAnnotationContent.alias());
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, '}')) {
throw new InvalidParameterException(
"Invalid property for parameter '" + parameter.getName() + "', "
+ "forbidden characters ${}: " + property,
null);
}
parameter.setExpression((property == null || property.isEmpty()) ? "" : "${" + property + "}");
StringBuilder type = new StringBuilder(parameterAnnotationContent.getClassName());
if (!parameterAnnotationContent.getTypeParameters().isEmpty()) {
type.append(parameterAnnotationContent.getTypeParameters().stream()
.collect(Collectors.joining(", ", "<", ">")));
}
parameter.setType(type.toString());
parameter.setSince(parameterAnnotationContent.getSince());
parameter.setRequired(parameterAnnotationContent.required());
org.apache.maven.plugin.descriptor.Parameter parameter = extractParameter(parameterAnnotationContent);

mojoDescriptor.addParameter(parameter);
}
Expand Down Expand Up @@ -774,6 +745,80 @@ private List<MojoDescriptor> toMojoDescriptors(
return mojoDescriptors;
}

private org.apache.maven.plugin.descriptor.Parameter extractParameter(
ParameterAnnotationContent parameterAnnotationContent) throws InvalidParameterException {
org.apache.maven.plugin.descriptor.Parameter parameter = new org.apache.maven.plugin.descriptor.Parameter();
String name = StringUtils.isEmpty(parameterAnnotationContent.name())
? parameterAnnotationContent.getFieldName()
: parameterAnnotationContent.name();
parameter.setName(name);
parameter.setAlias(parameterAnnotationContent.alias());
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, '}')) {
throw new InvalidParameterException(
"Invalid property for parameter '" + parameter.getName() + "', " + "forbidden characters ${}: "
+ property,
null);
}
parameter.setExpression((property == null || property.isEmpty()) ? "" : "${" + property + "}");
StringBuilder type = new StringBuilder(parameterAnnotationContent.getClassName());
String rawType = type.toString();
if (!parameterAnnotationContent.getTypeParameters().isEmpty()) {
type.append(parameterAnnotationContent.getTypeParameters().stream()
.collect(Collectors.joining(", ", "<", ">")));
}
parameter.setType(type.toString());
parameter.setSince(parameterAnnotationContent.getSince());
parameter.setRequired(parameterAnnotationContent.required());
String defaultValue = parameterAnnotationContent.defaultValue();
String[] defaultValues = parameterAnnotationContent.defaultValues();
if (defaultValue != null && defaultValues != null) {
throw new InvalidParameterException(
"Invalid default value for parameter '" + parameter.getName()
+ "': defaultValue and defaultValues cannot both be specified",
null);
}
if (defaultValue != null) {
parameter.setDefaultValue(defaultValue);
} else if (defaultValues != null) {
if (defaultValues.length == 1) {
parameter.setDefaultValue(defaultValues[0]);
} else if (defaultValues.length > 1) {
// check if type is either a collection or an array, otherwise we cannot set a default value with
// multiple values
if (!isCollection(rawType)) {
throw new InvalidParameterException(
"Invalid default value for parameter '" + parameter.getName() + "' of type '"
+ type.toString() + "', "
+ "multiple values are only allowed for collection or array types",
null);
}
parameter.setDefaultValue(String.join(",", defaultValues));
}
}
return parameter;
}

/**
* Checks if the given class name represents a collection type (Collection, List, Set, or array).
* This is just a heuristics as the full class hierarchy is not known at this point.
* @param className
* @return true if the class name represents a collection type, false otherwise
*/
static boolean isCollection(String className) {
return className.endsWith("Collection")
|| className.endsWith("List")
|| className.endsWith("Set")
|| className.endsWith("Queue")
|| className.endsWith("Deque")
|| className.endsWith("[]");
Comment thread
gnodet marked this conversation as resolved.
Resolved
}

protected MojoAnnotatedClass findClassWithExecuteAnnotationInParentHierarchy(
MojoAnnotatedClass mojoAnnotatedClass, Map<String, MojoAnnotatedClass> mojoAnnotatedClasses) {
if (mojoAnnotatedClass.getExecute() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.maven.tools.plugin.extractor.annotations.datamodel;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand All @@ -38,6 +39,8 @@ public class ParameterAnnotationContent extends AnnotatedField implements Parame

private String defaultValue;

private String[] defaultValues;

private boolean required = false;

private boolean readonly = false;
Expand Down Expand Up @@ -110,6 +113,15 @@ public void defaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public String[] defaultValues() {
return defaultValues;
}

public void defaultValues(String[] defaultValues) {
this.defaultValues = defaultValues;
}

@Override
public boolean required() {
return required;
Expand Down Expand Up @@ -162,6 +174,7 @@ public String toString() {
sb.append(", alias='").append(alias).append('\'');
sb.append(", property='").append(property).append('\'');
sb.append(", defaultValue='").append(defaultValue).append('\'');
sb.append(", defaultValues='").append(Arrays.toString(defaultValues)).append('\'');
sb.append(", required=").append(required);
sb.append(", readonly=").append(readonly);
sb.append(", methodSource=").append(annotationOnMethod);
Expand Down Expand Up @@ -205,7 +218,7 @@ public boolean equals(Object o) {
if (!Objects.equals(alias, that.alias)) {
return false;
}
if (!Objects.equals(defaultValue, that.defaultValue)) {
if (!Objects.equals(defaultValue, that.defaultValue) || !Arrays.equals(defaultValues, that.defaultValues)) {
return false;
}
return Objects.equals(property, that.property);
Expand All @@ -220,6 +233,7 @@ public int hashCode() {
typeParameters,
property,
defaultValue,
Arrays.hashCode(defaultValues),
required,
readonly,
annotationOnMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.zip.ZipInputStream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.Component;

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-26.04 jdk-17-zulu 3.10.0

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-26.04 jdk-17-zulu 3.10.0

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-26.04 jdk-8-zulu 3.10.0

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-26.04 jdk-25-zulu 3.10.0

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-26.04 jdk-21-zulu 3.10.0

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / macos-latest jdk-25-zulu 4.0.0-rc-6

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / macos-latest jdk-25-zulu 3.10.0

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-26.04 jdk-25-zulu 4.0.0-rc-6

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated

Check warning on line 41 in maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-26.04 jdk-21-zulu 4.0.0-rc-6

org.apache.maven.plugins.annotations.Component in org.apache.maven.plugins.annotations has been deprecated
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -275,6 +275,7 @@

protected void populateAnnotationContent(Object content, MojoAnnotationVisitor mojoAnnotationVisitor)
throws ReflectorException {
// This only iterates over explicitly defined annotation values, not implicit default values
for (Map.Entry<String, Object> entry :
mojoAnnotationVisitor.getAnnotationValues().entrySet()) {
reflector.invoke(content, entry.getKey(), new Object[] {entry.getValue()});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public class MojoAnnotationVisitor extends AnnotationVisitor {

private Map<String, MojoAnnotationVisitor> arrayVisitors;

// only set on array sub-visitors: owner and attribute name to flush the collected elements to
private MojoAnnotationVisitor arrayOwner;

private String arrayName;

private List<Object> arrayElements;

MojoAnnotationVisitor(String annotationClassName) {
super(Opcodes.ASM9);
this.annotationClassName = annotationClassName;
Expand All @@ -54,7 +61,26 @@ public Map<String, Object> getAnnotationValues() {

@Override
public void visit(String name, Object value) {
annotationValues.put(name, value);
if (arrayOwner != null && name == null) {
if (arrayElements == null) {
arrayElements = new ArrayList<>();
}
arrayElements.add(value);
} else {
annotationValues.put(name, value);
}
}

@Override
public void visitEnd() {
if (arrayOwner == null || nestedAnnotationVisitors != null) {
return;
}
if (arrayElements == null) {
arrayOwner.annotationValues.put(arrayName, new String[0]);
} else if (arrayElements.stream().allMatch(String.class::isInstance)) {
arrayOwner.annotationValues.put(arrayName, arrayElements.toArray(new String[0]));
}
}

@Override
Expand All @@ -76,6 +102,8 @@ public AnnotationVisitor visitAnnotation(String name, String desc) {
@Override
public AnnotationVisitor visitArray(String name) {
MojoAnnotationVisitor arrayVisitor = new MojoAnnotationVisitor(this.annotationClassName);
arrayVisitor.arrayOwner = this;
arrayVisitor.arrayName = name;
if (this.arrayVisitors == null) {
this.arrayVisitors = new HashMap<>();
}
Expand Down
Loading
Loading