diff --git a/maven-plugin-annotations/src/main/java/org/apache/maven/plugins/annotations/Parameter.java b/maven-plugin-annotations/src/main/java/org/apache/maven/plugins/annotations/Parameter.java
index 82ff5e3c0..3c59ab8c2 100644
--- a/maven-plugin-annotations/src/main/java/org/apache/maven/plugins/annotations/Parameter.java
+++ b/maven-plugin-annotations/src/main/java/org/apache/maven/plugins/annotations/Parameter.java
@@ -27,7 +27,7 @@
/**
* Used to configure your Mojo parameters to be injected by
- *
+ *
* MavenPluginManager.getConfiguredMojo(...).
*
* Beans injected into Mojo parameters are prepared by Sisu JSR330-based
@@ -36,6 +36,9 @@
*
* @author Olivier Lamy
* @since 3.0
+ * @see Configuring Plug-Ins
+ * @see Plexus Component Configurator API
+ * @see JSR 330
*/
@Documented
@Retention(RetentionPolicy.CLASS)
@@ -62,14 +65,30 @@
String property() default "";
/**
- * parameter default value, may contain ${...} expressions which will be interpreted at
+ * Parameter default value, may contain ${...} expressions which will be interpreted at
* inject time: see
- *
+ *
* PluginParameterExpressionEvaluator.
+ * The type conversion from String into the target type is described in Configuring Parameters.
+ * 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 collection or array type parameters.
+ * 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 true if the Mojo should fail when the parameter cannot be injected
diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java
index c8ab3c427..dc90ecbc9 100644
--- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java
+++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java
@@ -706,7 +706,6 @@ private List toMojoDescriptors(
mojoDescriptor.setGoal(mojo.name());
mojoDescriptor.setOnlineRequired(mojo.requiresOnline());
-
mojoDescriptor.setPhase(mojo.defaultPhase().id());
// Parameter annotations
@@ -714,35 +713,7 @@ private List toMojoDescriptors(
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);
}
@@ -774,6 +745,80 @@ private List 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("[]");
+ }
+
protected MojoAnnotatedClass findClassWithExecuteAnnotationInParentHierarchy(
MojoAnnotatedClass mojoAnnotatedClass, Map mojoAnnotatedClasses) {
if (mojoAnnotatedClass.getExecute() != null) {
diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java
index 921e30ad7..71fa2476a 100644
--- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java
+++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java
@@ -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;
@@ -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;
@@ -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;
@@ -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);
@@ -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);
@@ -220,6 +233,7 @@ public int hashCode() {
typeParameters,
property,
defaultValue,
+ Arrays.hashCode(defaultValues),
required,
readonly,
annotationOnMethod);
diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java
index e9d8dca34..03713d299 100644
--- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java
+++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java
@@ -275,6 +275,7 @@ private void analyzeClassStream(
protected void populateAnnotationContent(Object content, MojoAnnotationVisitor mojoAnnotationVisitor)
throws ReflectorException {
+ // This only iterates over explicitly defined annotation values, not implicit default values
for (Map.Entry entry :
mojoAnnotationVisitor.getAnnotationValues().entrySet()) {
reflector.invoke(content, entry.getKey(), new Object[] {entry.getValue()});
diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java
index 2e535a07f..70e2bcb44 100644
--- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java
+++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoAnnotationVisitor.java
@@ -43,6 +43,13 @@ public class MojoAnnotationVisitor extends AnnotationVisitor {
private Map 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