gnodet opened a new pull request, #13658:
URL: https://github.com/apache/maven/pull/13658
## Summary
Implements a JSR 269 annotation processor (`maven-api-plugin-processor`)
that generates Maven plugin descriptors (`META-INF/maven/plugin.xml`) and DI
factory classes at **compile time** from `@Mojo`-annotated classes, eliminating
the need for the external `maven-plugin-plugin` (from maven-plugin-tools) for
Maven 4 plugins.
Closes #13644
## Design
Follows the `maven-api-di` / `DiIndexProcessor` pattern:
- **Zero external dependencies** — only `java.compiler` + `java.xml` JDK
modules
- Self-processing disabled via
`<maven.compiler.proc>none</maven.compiler.proc>`
- Service registration via both
`META-INF/services/javax.annotation.processing.Processor` and
`module-info.java` `provides` clause
- Returns `false` from `process()` — does not claim `@Mojo`; other
processors and IDEs can still see it
- Annotation references are **string-based** — no compile dependency on
`maven-api-core` or `maven-api-di`
## What gets generated
### 1. `META-INF/maven/plugin.xml` (v2.0.0 namespace)
Extracts:
- `@Mojo` attributes (goal name, defaultPhase, projectRequired, aggregator,
onlineRequired, configurator, dependencyCollection,
dependencyResolutionPathScopes)
- `@Execute` (phase, goal, lifecycle)
- `@After` / `@Afters` (repeatable) → `<afterLinks>`
- `@Parameter` on fields/methods — walks full type hierarchy via
`TypeElement.getSuperclass()`
- `@Resolution` on fields — same hierarchy walk
- Javadoc via `Elements.getDocComment()` — description (with inline tag
stripping), `@since`, `@deprecated`
### 2. Factory source file per mojo
For `com.example.MyMojo` with `@Mojo(name = "goal")`:
```java
@org.apache.maven.api.di.Named("groupId:artifactId:version:goal")
public class MyMojo$MojoFactory extends MyMojo {}
```
The `DiIndexProcessor` (from `maven-api-di`) picks up the `@Named` factory
in the next round and adds it to
`META-INF/maven/org.apache.maven.api.di.Inject` automatically. This factory is
required because `DefaultMavenPluginManager.loadV4Mojo()` looks up mojos via
`Key.of(implementationClass, roleHint)` where `roleHint =
"groupId:artifactId:version:goal"`.
## Required compiler options (via `-A`)
| Option | Required | Description |
|--------|----------|-------------|
| `maven.plugin.groupId` | yes | Plugin group ID |
| `maven.plugin.artifactId` | yes | Plugin artifact ID |
| `maven.plugin.version` | yes | Plugin version |
| `maven.plugin.goalPrefix` | no | Derived from artifactId if absent |
## Files
- `api/maven-api-plugin-processor/pom.xml` — module POM (parent:
`maven-api`, no deps, proc=none)
- `api/maven-api-plugin-processor/src/main/java/module-info.java` — exports
package, requires `java.compiler` + `java.xml`, provides `Processor`
-
`api/maven-api-plugin-processor/src/main/java/org/apache/maven/plugin/processor/MojoDescriptorProcessor.java`
— the processor
-
`api/maven-api-plugin-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor`
— service registration
- `api/pom.xml` — adds `maven-api-plugin-processor` module (after
`maven-api-core`)
## Verification
Manually tested against the `mng-12534` IT `TouchMojo` — generated output:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://maven.apache.org/PLUGIN/2.0.0">
<name>mng12534-plugin</name>
<groupId>org.apache.maven.its.mng12534</groupId>
<artifactId>mng12534-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<goalPrefix>mng12534</goalPrefix>
<mojos>
<mojo>
<goal>touch</goal>
<phase>compile</phase>
<implementation>org.apache.maven.its.mng12534.TouchMojo</implementation>
<language>java</language>
<description>...</description>
<projectRequired>true</projectRequired>
<onlineRequired>false</onlineRequired>
<aggregator>false</aggregator>
<afterLinks>
<afterLink>
<phase>resources</phase>
<type>PROJECT</type>
</afterLink>
</afterLinks>
</mojo>
</mojos>
</plugin>
```
## Next steps (not in this PR)
- Unit test using `javax.tools.JavaCompiler` API
- Update `mng-12534-after-annotation` IT to use the processor instead of
handcrafted `plugin.xml` + factory
- Update issue #13644 (factory classes ARE needed for DI lookup)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]