Example: Using Maven 3 lifecycle extension
Lifecyle Participation
You can extend multiple classes depending on your needs:
Build your extension
Create a Maven project with a dependency on org.apache.maven:maven-core:3.8.5
and other dependencies:
<groupId>org.apache.maven.extensions</groupId> <artifactId>beer-maven-lifecycle</artifactId> <version>1.0-SNAPSHOT</version> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-core</artifactId> <version>3.8.5</version> </dependency> <!-- dependency for plexus annotation --> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-component-annotations</artifactId> <version>1.7.1</version> </dependency>
Create your extension class
// your extension must be a "Plexus" component so mark it with the annotation @Component( role = AbstractMavenLifecycleParticipant.class, hint = "beer") public class BeerMavenLifecycleParticipant extends AbstractMavenLifecycleParticipant { @Override public void afterSessionStart( MavenSession session ) throws MavenExecutionException { // start the beer machine } @Override public void afterProjectsRead( MavenSession session ) throws MavenExecutionException { // ask a beer to the machine } }
Generate plexus metadata during the build of your extension jar:
<build> ... <plugins> ... <plugin> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-component-metadata</artifactId> <version>1.7.1</version> <executions> <execution> <goals> <goal>generate-metadata</goal> </goals> </execution> </executions> </plugin> ... </plugins> ... </build>
Use your extension in your build(s)
You have 3 ways to use your extension within your builds:
- add your extension jar in
${maven.home}/lib/ext
, - add it as a build extension in your pom,
- (since Maven 3.3.1) configure your extension in
.mvn/extensions.xml
.
NOTE: if you use the build extension mechanism, the method afterSessionStart
won't be called since the extension is loaded later in the build
To use a build extension in your project, declare it in your pom as:
<build> ... <extensions> ... <extension> <groupId>org.apache.maven.extensions</groupId> <artifactId>beer-maven-lifecycle</artifactId> <version>1.0-SNAPSHOT</version> </extension> ... </extensions> ... </build>