Skip to content

Commit 23bca28

Browse files
authored
[MNG-7947] Plugin API (#1309)
* Add an InternalSession interface to avoid casting to the implementation in various places * Upgrade to Guice 6.0.0 and add support for jakarta.inject annotations * Maven 4 Plugin API
1 parent 6392717 commit 23bca28

File tree

73 files changed

+1481
-695
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1481
-695
lines changed

api/maven-api-core/pom.xml

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@
5151
<artifactId>maven-api-toolchain</artifactId>
5252
<version>4.0.0-alpha-9-SNAPSHOT</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.apache.maven</groupId>
56+
<artifactId>maven-api-plugin</artifactId>
57+
<version>4.0.0-alpha-9-SNAPSHOT</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>jakarta.inject</groupId>
61+
<artifactId>jakarta.inject-api</artifactId>
62+
</dependency>
5463
</dependencies>
5564

5665
</project>

api/maven-api-core/src/main/java/org/apache/maven/api/MojoExecution.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,37 @@
2222

2323
import org.apache.maven.api.annotations.Experimental;
2424
import org.apache.maven.api.annotations.Nonnull;
25-
import org.apache.maven.api.model.Plugin;
25+
import org.apache.maven.api.model.PluginExecution;
26+
import org.apache.maven.api.plugin.descriptor.MojoDescriptor;
2627
import org.apache.maven.api.xml.XmlNode;
2728

2829
/**
29-
* A {@code MojoExecution}
30+
* A {@code MojoExecution} represents a single execution of a Maven Plugin during a given build.
31+
* An instance of this object is bound to the {@link org.apache.maven.api.di.MojoExecutionScoped}
32+
* and available as {@code mojoExecution} within {@link org.apache.maven.api.plugin.annotations.Parameter}
33+
* expressions.
3034
*/
3135
@Experimental
3236
public interface MojoExecution {
37+
3338
@Nonnull
3439
Plugin getPlugin();
3540

41+
@Nonnull
42+
PluginExecution getModel();
43+
44+
@Nonnull
45+
MojoDescriptor getDescriptor();
46+
3647
@Nonnull
3748
String getExecutionId();
3849

3950
@Nonnull
4051
String getGoal();
4152

53+
@Nonnull
54+
String getLifecyclePhase();
55+
4256
@Nonnull
4357
Optional<XmlNode> getConfiguration();
4458
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.api;
20+
21+
import java.util.Collection;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.apache.maven.api.annotations.Experimental;
26+
import org.apache.maven.api.annotations.Nonnull;
27+
import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
28+
import org.apache.maven.api.plugin.descriptor.lifecycle.Lifecycle;
29+
30+
/**
31+
* Represents a maven plugin runtime
32+
*/
33+
@Experimental
34+
public interface Plugin {
35+
36+
@Nonnull
37+
org.apache.maven.api.model.Plugin getModel();
38+
39+
@Nonnull
40+
PluginDescriptor getDescriptor();
41+
42+
@Nonnull
43+
List<Lifecycle> getLifecycles();
44+
45+
@Nonnull
46+
ClassLoader getClassLoader();
47+
48+
@Nonnull
49+
Artifact getArtifact();
50+
51+
@Nonnull
52+
default Collection<Dependency> getDependencies() {
53+
return getDependenciesMap().values();
54+
}
55+
56+
@Nonnull
57+
Map<String, Dependency> getDependenciesMap();
58+
}

api/maven-api-core/src/main/java/org/apache/maven/api/Session.java

+7
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ ArtifactCoordinate createArtifactCoordinate(
214214
@Nonnull
215215
DependencyCoordinate createDependencyCoordinate(@Nonnull ArtifactCoordinate coordinate);
216216

217+
/**
218+
* Shortcut for <code>getService(DependencyFactory.class).create(...)</code>
219+
* @see DependencyCoordinateFactory#create(Session, Dependency)
220+
*/
221+
@Nonnull
222+
DependencyCoordinate createDependencyCoordinate(@Nonnull Dependency dependency);
223+
217224
/**
218225
* Shortcut for <code>getService(ArtifactFactory.class).create(...)</code>
219226
* @see org.apache.maven.api.services.ArtifactFactory#create(Session, String, String, String, String)

api/maven-api-core/src/main/java/org/apache/maven/api/VersionRange.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@Experimental
3030
public interface VersionRange {
3131

32-
// TODO: add access to the version information
32+
// TODO: v4: add access to the version information
3333

3434
/**
3535
* Determines whether the specified version is contained within this range.

api/maven-api-core/src/main/java/org/apache/maven/api/plugin/annotations/Component.java api/maven-api-core/src/main/java/org/apache/maven/api/di/MojoExecutionScoped.java

+12-28
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,26 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.api.plugin.annotations;
19+
package org.apache.maven.api.di;
2020

2121
import java.lang.annotation.Documented;
22-
import java.lang.annotation.ElementType;
23-
import java.lang.annotation.Inherited;
2422
import java.lang.annotation.Retention;
25-
import java.lang.annotation.RetentionPolicy;
2623
import java.lang.annotation.Target;
2724

28-
import org.apache.maven.api.annotations.Experimental;
29-
import org.apache.maven.api.annotations.Nonnull;
25+
import jakarta.inject.Scope;
26+
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.ElementType.TYPE;
29+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
3030

3131
/**
32-
* Used to configure injection of Plexus components by
33-
* <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/MavenPluginManager.html">
34-
* <code>MavenPluginManager.getConfiguredMojo(...)</code></a>.
32+
* Indicates that the annotated bean has a lifespan limited to a given mojo execution,
33+
* which means each mojo execution will result in a different instance being injected.
3534
*
3635
* @since 4.0.0
3736
*/
38-
@Experimental
37+
@Scope
3938
@Documented
40-
@Retention(RetentionPolicy.CLASS)
41-
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
42-
@Inherited
43-
public @interface Component {
44-
/**
45-
* role of the component to inject.
46-
* @return the role
47-
*/
48-
@Nonnull
49-
Class<?> role() default Object.class;
50-
51-
/**
52-
* hint of the component to inject.
53-
* @return the hint
54-
*/
55-
@Nonnull
56-
String hint() default "";
57-
}
39+
@Retention(RUNTIME)
40+
@Target({TYPE, METHOD})
41+
public @interface MojoExecutionScoped {}
+17-20
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,26 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.api.plugin.annotations;
19+
package org.apache.maven.api.di;
2020

21-
import org.apache.maven.api.annotations.Experimental;
21+
import java.lang.annotation.Documented;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.Target;
24+
25+
import jakarta.inject.Scope;
26+
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.ElementType.TYPE;
29+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
2230

2331
/**
24-
* Component instantiation strategy.
32+
* Indicates that annotated component should be instantiated before session execution starts
33+
* and discarded after session execution completes.
2534
*
2635
* @since 4.0.0
2736
*/
28-
@Experimental
29-
public enum InstantiationStrategy {
30-
PER_LOOKUP("per-lookup"),
31-
SINGLETON("singleton"),
32-
KEEP_ALIVE("keep-alive"),
33-
POOLABLE("poolable");
34-
35-
private final String id;
36-
37-
InstantiationStrategy(String id) {
38-
this.id = id;
39-
}
40-
41-
public String id() {
42-
return this.id;
43-
}
44-
}
37+
@Scope
38+
@Documented
39+
@Retention(RUNTIME)
40+
@Target({TYPE, METHOD})
41+
public @interface SessionScoped {}

api/maven-api-core/src/main/java/org/apache/maven/api/feature/Features.java

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public final class Features {
3434

3535
/**
3636
* Name of the Maven user property to enable or disable the build/consumer POM feature.
37+
*
38+
* TODO: v4: remove experimental bit
3739
*/
3840
public static final String BUILDCONSUMER = "maven.experimental.buildconsumer";
3941

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.api.plugin;
20+
21+
import java.util.List;
22+
23+
import org.apache.maven.api.annotations.Consumer;
24+
import org.apache.maven.api.annotations.Experimental;
25+
import org.apache.maven.api.plugin.descriptor.lifecycle.Lifecycle;
26+
27+
/**
28+
* Interface that can be provided by the plugin to wire in custom lifecycles
29+
* leveraged using the {@link org.apache.maven.api.plugin.annotations.Execute}
30+
* annotation. If a {@code META-INF/maven/lifecycle.xml} file is packaged
31+
* in the plugin, Maven will provide a default implementation that will parse
32+
* the file and return the contained lifecycle definitions.
33+
*/
34+
@Experimental
35+
@Consumer
36+
public interface LifecycleProvider {
37+
38+
List<Lifecycle> getLifecycles();
39+
}

api/maven-api-core/src/main/java/org/apache/maven/api/plugin/annotations/Mojo.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
/**
3333
* This annotation will mark your class as a Mojo (ie. goal in a Maven plugin).
34+
* The mojo can be annotated with {@code jakarta.inject.*} annotations.
35+
* The {@link Parameter} annotation can be added on fields to inject data
36+
* from the plugin configuration or from other components.
3437
*
3538
* @since 4.0.0
3639
*/
@@ -59,27 +62,20 @@
5962
* @return the required dependency resolution scope
6063
*/
6164
@Nonnull
62-
ResolutionScope requiresDependencyResolution() default ResolutionScope.NONE;
65+
ResolutionScope dependencyResolutionRequired() default ResolutionScope.NONE;
6366

6467
/**
6568
* the required dependency collection scope.
6669
* @return the required dependency collection scope
6770
*/
6871
@Nonnull
69-
ResolutionScope requiresDependencyCollection() default ResolutionScope.NONE;
70-
71-
/**
72-
* your Mojo instantiation strategy. (Only <code>per-lookup</code> and <code>singleton</code> are supported)
73-
* @return the instantiation strategy
74-
*/
75-
@Nonnull
76-
InstantiationStrategy instantiationStrategy() default InstantiationStrategy.PER_LOOKUP;
72+
ResolutionScope dependencyCollectionRequired() default ResolutionScope.NONE;
7773

7874
/**
7975
* does your mojo requires a project to be executed?
8076
* @return requires a project
8177
*/
82-
boolean requiresProject() default true;
78+
boolean projectRequired() default true;
8379

8480
/**
8581
* if the Mojo uses the Maven project and its child modules.
@@ -91,9 +87,10 @@
9187
* does this Mojo need to be online to be executed?
9288
* @return need to be online
9389
*/
94-
boolean requiresOnline() default false;
90+
boolean onlineRequired() default false;
9591

9692
/**
93+
* TODO: v4: add a SPI for the configurator
9794
* configurator bean name.
9895
* @return the configurator bean name
9996
*/

0 commit comments

Comments
 (0)