This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch feature/support-string-array-default-values-on-parameter-annotation in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git
commit 9e833593f89eec6c9f639f5bdeff94e483be85ab Author: Konrad Windszus <[email protected]> AuthorDate: Mon Nov 9 20:49:22 2026 +0100 Support String array for @Parameter's element "defaultValue" Convert multi-value Strings into a comma-separated String of org.apache.maven.plugin.descriptor.Parameter#defaultValue The underlying Sisu implementaton supports comma separated Strings for collections (https://eclipse.dev/sisu/org.eclipse.sisu.plexus/configuration.html#Collection_Objects). as well as the Maven 4 equivalent at V4MojoConfigurator.class. This closes #1272 --- .../maven/plugins/annotations/Parameter.java | 5 +- .../JavaAnnotationsMojoDescriptorExtractor.java | 94 +++++++++++++++------- .../datamodel/ParameterAnnotationContent.java | 15 ++-- .../scanner/visitors/MojoAnnotationVisitor.java | 33 +++++++- ...JavaAnnotationsMojoDescriptorExtractorTest.java | 15 ++++ .../MultiDefaultValueOnCollectionTypesMojo.java | 53 ++++++++++++ .../MultiDefaultValueOnNonCollectionTypeMojo.java | 40 +++++++++ .../scanner/DefaultMojoAnnotationsScannerTest.java | 4 +- 8 files changed, 217 insertions(+), 42 deletions(-) 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 82ff5e3c..8ed926fc 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 @@ -62,13 +62,14 @@ public @interface Parameter { 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"> * PluginParameterExpressionEvaluator</a>. + * May either contain a single value or multiple values, in which case the values will be concatenated with a comma in the generated plugin descriptor. * @return the default value */ - String defaultValue() default ""; + String[] defaultValue() default ""; /** * is the parameter required? 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 c8ab3c42..7bfe795c 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 @@ public class JavaAnnotationsMojoDescriptorExtractor implements MojoDescriptorExt mojoDescriptor.setGoal(mojo.name()); mojoDescriptor.setOnlineRequired(mojo.requiresOnline()); - mojoDescriptor.setPhase(mojo.defaultPhase().id()); // Parameter annotations @@ -714,35 +713,7 @@ public class JavaAnnotationsMojoDescriptorExtractor implements MojoDescriptorExt 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,69 @@ public class JavaAnnotationsMojoDescriptorExtractor implements MojoDescriptorExt 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()); + // concatenate multi values with a comma + if (parameterAnnotationContent.defaultValue() != null) { + if (parameterAnnotationContent.defaultValue().length == 1) { + parameter.setDefaultValue(parameterAnnotationContent.defaultValue()[0]); + } else if (parameterAnnotationContent.defaultValue().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(",", parameterAnnotationContent.defaultValue())); + } + } + 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("[]"); + } + protected MojoAnnotatedClass findClassWithExecuteAnnotationInParentHierarchy( MojoAnnotatedClass mojoAnnotatedClass, Map<String, MojoAnnotatedClass> 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 921e30ad..a3a8caf2 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; @@ -36,7 +37,7 @@ public class ParameterAnnotationContent extends AnnotatedField implements Parame private String property; - private String defaultValue; + private String[] defaultValue = null; private boolean required = false; @@ -60,7 +61,7 @@ public class ParameterAnnotationContent extends AnnotatedField implements Parame String fieldName, String alias, String property, - String defaultValue, + String[] defaultValue, boolean required, boolean readonly, String className, @@ -102,11 +103,11 @@ public class ParameterAnnotationContent extends AnnotatedField implements Parame } @Override - public String defaultValue() { + public String[] defaultValue() { return defaultValue; } - public void defaultValue(String defaultValue) { + public void defaultValue(String[] defaultValue) { this.defaultValue = defaultValue; } @@ -161,7 +162,7 @@ public class ParameterAnnotationContent extends AnnotatedField implements Parame sb.append(", alias='").append(alias).append('\''); sb.append(", alias='").append(alias).append('\''); sb.append(", property='").append(property).append('\''); - sb.append(", defaultValue='").append(defaultValue).append('\''); + sb.append(", defaultValue='").append(Arrays.toString(defaultValue)).append('\''); sb.append(", required=").append(required); sb.append(", readonly=").append(readonly); sb.append(", methodSource=").append(annotationOnMethod); @@ -205,7 +206,7 @@ public class ParameterAnnotationContent extends AnnotatedField implements Parame if (!Objects.equals(alias, that.alias)) { return false; } - if (!Objects.equals(defaultValue, that.defaultValue)) { + if (!Arrays.equals(defaultValue, that.defaultValue)) { return false; } return Objects.equals(property, that.property); @@ -219,7 +220,7 @@ public class ParameterAnnotationContent extends AnnotatedField implements Parame getClassName(), typeParameters, property, - defaultValue, + Arrays.hashCode(defaultValue), required, readonly, annotationOnMethod); 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 2e535a07..22ba514a 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<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; @@ -54,7 +61,29 @@ public class MojoAnnotationVisitor extends AnnotationVisitor { @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 if ("defaultValue".equals(name) && value instanceof String) { + // annotations compiled against the former single-valued Parameter#defaultValue + annotationValues.put(name, new String[] {(String) 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 @@ -76,6 +105,8 @@ public class MojoAnnotationVisitor extends AnnotationVisitor { @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<>(); } diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractorTest.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractorTest.java index 5cea9d38..4d141d72 100644 --- a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractorTest.java +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractorTest.java @@ -38,6 +38,7 @@ import com.github.javaparser.ast.body.TypeDeclaration; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.descriptor.InvalidParameterException; import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor; @@ -147,6 +148,20 @@ class JavaAnnotationsMojoDescriptorExtractorTest { assertEquals("Says hello.", mojoDescriptor.getDescription()); } + @Test + void scanNonCollectionParametersWithMultiValueDefault() throws Exception { + assertThrows( + InvalidParameterException.class, + () -> extractDescriptorFromMojoClass(MultiDefaultValueOnNonCollectionTypeMojo.class)); + } + + @Test + void scanCollectionParametersWithMultiValueDefault() throws Exception { + MojoDescriptor mojoDescriptor = extractDescriptorFromMojoClass(MultiDefaultValueOnCollectionTypesMojo.class); + mojoDescriptor.getParameters().forEach(parameter -> { + assertEquals("value1,value2", parameter.getDefaultValue()); + }); + } /** * Compiles the named sources of {@code src/test/resources/mojo-sources} and extracts the single mojo they * contain, with the sources registered as compile source root so that the Javadoc is read. diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/MultiDefaultValueOnCollectionTypesMojo.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/MultiDefaultValueOnCollectionTypesMojo.java new file mode 100644 index 00000000..4c9e2fc6 --- /dev/null +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/MultiDefaultValueOnCollectionTypesMojo.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.tools.plugin.extractor.annotations; + +import java.util.Collection; +import java.util.Set; +import java.util.SortedSet; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Execute; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +@Mojo(name = "foo", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true) +@Execute(goal = "compiler", lifecycle = "my-lifecycle", phase = LifecyclePhase.PACKAGE) +public class MultiDefaultValueOnCollectionTypesMojo extends AbstractFooMojo { + + @Parameter(defaultValue = {"value1", "value2"}) + private String[] foo; + + @Parameter(defaultValue = {"value1", "value2"}) + private Set<String> bar; + + @Parameter(defaultValue = {"value1", "value2"}) + private SortedSet<String> baz; + + @Parameter(defaultValue = {"value1", "value2"}) + private Collection<Integer>[] qux; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + // TODO Auto-generated method stub + + } +} diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/MultiDefaultValueOnNonCollectionTypeMojo.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/MultiDefaultValueOnNonCollectionTypeMojo.java new file mode 100644 index 00000000..fe79891b --- /dev/null +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/MultiDefaultValueOnNonCollectionTypeMojo.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.tools.plugin.extractor.annotations; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Execute; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +@Mojo(name = "foo", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true) +@Execute(goal = "compiler", lifecycle = "my-lifecycle", phase = LifecyclePhase.PACKAGE) +public class MultiDefaultValueOnNonCollectionTypeMojo extends AbstractFooMojo { + + @Parameter(defaultValue = {"value1", "value2"}) + private String bar; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + // TODO Auto-generated method stub + + } +} diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java index 924063b4..00b22182 100644 --- a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScannerTest.java @@ -179,7 +179,7 @@ class DefaultMojoAnnotationsScannerTest { "bar", null, "thebar", - "coolbar", + new String[] {"coolbar"}, true, false, String.class.getName(), @@ -189,7 +189,7 @@ class DefaultMojoAnnotationsScannerTest { "beer", null, "thebeer", - "coolbeer", + new String[] {"coolbeer"}, false, false, String.class.getName(),
