From 49c11e9187d7d0ed47ef088e089633f1121d7757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Wed, 7 Oct 2026 23:04:40 +0300 Subject: [PATCH] Fix Maven 4 @Mojo descriptor attributes --- .../maven/its/v4api/CollectionMojo.java | 31 +++++++++++++++++++ .../apache/maven/its/v4api/OnlineMojo.java | 28 +++++++++++++++++ .../maven/its/v4api/ResolutionMojo.java | 28 +++++++++++++++++ .../src/it/v4api-4x/verify.groovy | 11 +++++++ ...avaAnnotationsMojoDescriptorExtractor.java | 21 ++++++++++--- .../datamodel/MojoAnnotationContent.java | 24 ++++++++++++++ 6 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/CollectionMojo.java create mode 100644 maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/OnlineMojo.java create mode 100644 maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/ResolutionMojo.java diff --git a/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/CollectionMojo.java b/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/CollectionMojo.java new file mode 100644 index 000000000..885b01d0c --- /dev/null +++ b/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/CollectionMojo.java @@ -0,0 +1,31 @@ +/* + * 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.its.v4api; + +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; + +@Mojo( + name = "collection", + dependencyCollection = true, + dependencyResolutionPathScopes = "main-compile") +public class CollectionMojo implements org.apache.maven.api.plugin.Mojo { + @Override + public void execute() throws MojoException {} +} diff --git a/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/OnlineMojo.java b/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/OnlineMojo.java new file mode 100644 index 000000000..ec25d371b --- /dev/null +++ b/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/OnlineMojo.java @@ -0,0 +1,28 @@ +/* + * 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.its.v4api; + +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; + +@Mojo(name = "online", onlineRequired = true) +public class OnlineMojo implements org.apache.maven.api.plugin.Mojo { + @Override + public void execute() throws MojoException {} +} diff --git a/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/ResolutionMojo.java b/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/ResolutionMojo.java new file mode 100644 index 000000000..f15049e64 --- /dev/null +++ b/maven-plugin-plugin/src/it/v4api-4x/src/main/java/org/apache/maven/its/v4api/ResolutionMojo.java @@ -0,0 +1,28 @@ +/* + * 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.its.v4api; + +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; + +@Mojo(name = "resolution", dependencyResolutionPathScopes = "main-compile") +public class ResolutionMojo implements org.apache.maven.api.plugin.Mojo { + @Override + public void execute() throws MojoException {} +} diff --git a/maven-plugin-plugin/src/it/v4api-4x/verify.groovy b/maven-plugin-plugin/src/it/v4api-4x/verify.groovy index 1fed69783..89daa03e8 100644 --- a/maven-plugin-plugin/src/it/v4api-4x/verify.groovy +++ b/maven-plugin-plugin/src/it/v4api-4x/verify.groovy @@ -80,4 +80,15 @@ assert parameter.expression.isEmpty() assert new File( basedir, "target/classes/org/apache/maven/its/v4api/HelpMojo.class" ).isFile() assert new File( basedir, "target/generated-sources/plugin/org/apache/maven/its/v4api/HelpMojo.java" ).isFile() +def collectionMojo = pluginDescriptor.mojos.mojo.find { it.goal.text() == 'collection' } +assert collectionMojo.dependencyCollection.text() == 'main-compile' +assert collectionMojo.dependencyResolution.isEmpty() + +def resolutionMojo = pluginDescriptor.mojos.mojo.find { it.goal.text() == 'resolution' } +assert resolutionMojo.dependencyResolution.text() == 'main-compile' +assert resolutionMojo.dependencyCollection.isEmpty() + +def onlineMojo = pluginDescriptor.mojos.mojo.find { it.goal.text() == 'online' } +assert onlineMojo.onlineRequired.text() == 'true' + return true; 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..f6c338dd8 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 @@ -663,10 +663,7 @@ private List toMojoDescriptors( mojoDescriptor.setInstantiationStrategy(mojo.instantiationStrategy().id()); mojoDescriptor.setAggregator(mojo.aggregator()); - mojoDescriptor.setDependencyResolutionRequired( - mojo.requiresDependencyResolution().id()); - mojoDescriptor.setDependencyCollectionRequired( - mojo.requiresDependencyCollection().id()); + setDependencyRequirements(mojoDescriptor, mojo, mojoAnnotatedClass.isV4Api()); mojoDescriptor.setDirectInvocationOnly(mojo.requiresDirectInvocation()); mojoDescriptor.setDeprecated(mojo.getDeprecated()); @@ -774,6 +771,22 @@ private List toMojoDescriptors( return mojoDescriptors; } + private void setDependencyRequirements( + ExtendedMojoDescriptor mojoDescriptor, MojoAnnotationContent mojo, boolean isV4Api) { + mojoDescriptor.setDependencyResolutionRequired( + mojo.requiresDependencyResolution().id()); + mojoDescriptor.setDependencyCollectionRequired( + mojo.requiresDependencyCollection().id()); + + if (isV4Api && StringUtils.isNotEmpty(mojo.getDependencyResolutionPathScopes())) { + if (mojo.isDependencyCollection()) { + mojoDescriptor.setDependencyCollectionRequired(mojo.getDependencyResolutionPathScopes()); + } else { + mojoDescriptor.setDependencyResolutionRequired(mojo.getDependencyResolutionPathScopes()); + } + } + } + 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/MojoAnnotationContent.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/MojoAnnotationContent.java index 121969905..cfc2da664 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/MojoAnnotationContent.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/MojoAnnotationContent.java @@ -53,6 +53,10 @@ public class MojoAnnotationContent extends AnnotatedContent implements Mojo { private boolean requiresOnline = false; + private boolean dependencyCollection; + + private String dependencyResolutionPathScopes; + private boolean inheritByDefault = true; private String configurator; @@ -172,6 +176,26 @@ public void requiresOnline(boolean requiresOnline) { this.requiresOnline = requiresOnline; } + public void onlineRequired(boolean requiresOnline) { + this.requiresOnline = requiresOnline; + } + + public boolean isDependencyCollection() { + return dependencyCollection; + } + + public void dependencyCollection(boolean dependencyCollection) { + this.dependencyCollection = dependencyCollection; + } + + public String getDependencyResolutionPathScopes() { + return dependencyResolutionPathScopes; + } + + public void dependencyResolutionPathScopes(String dependencyResolutionPathScopes) { + this.dependencyResolutionPathScopes = dependencyResolutionPathScopes; + } + @Override public boolean inheritByDefault() { return inheritByDefault;