Example: Using Maven 3 lifecycle extension

Lifecyle Participation

You can extends the class org.apache.maven.AbstractMavenLifecycleParticipant see javadoc.

Build your extension

Create a Maven project with a dependency on org.apache.maven:maven-core:3.3.9 and other dependencies :

  1. <groupId>org.apache.maven.extensions</groupId>
  2. <artifactId>beer-maven-lifecycle</artifactId>
  3. <version>1.0-SNAPSHOT</version>
  4.  
  5. <dependency>
  6. <groupId>org.apache.maven</groupId>
  7. <artifactId>maven-core</artifactId>
  8. <version>3.0.4</version>
  9. </dependency>
  10.  
  11. <!-- dependency for plexus annotation -->
  12. <dependency>
  13. <groupId>org.codehaus.plexus</groupId>
  14. <artifactId>plexus-component-annotations</artifactId>
  15. <version>1.5.5</version>
  16. <exclusions>
  17. <exclusion>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. </exclusion>
  21. </exclusions>
  22. </dependency>

Create your extension class

  1. // your extension must be a "Plexus" component so mark it with the annotation
  2. @Component( role = AbstractMavenLifecycleParticipant.class, hint = "beer")
  3. public class BeerMavenLifecycleParticipant extends AbstractMavenLifecycleParticipant
  4. {
  5.  
  6. @Override
  7. public void afterSessionStart( MavenSession session )
  8. throws MavenExecutionException
  9. {
  10. // start the beer machine
  11. }
  12.  
  13.  
  14. @Override
  15. public void afterProjectsRead( MavenSession session )
  16. throws MavenExecutionException
  17. {
  18. // ask a beer to the machine
  19. }
  20.  
  21. }

Generate plexus metadatas during building your extension jar

  1. <build>
  2. ...
  3. <plugins>
  4. ...
  5. <plugin>
  6. <groupId>org.codehaus.plexus</groupId>
  7. <artifactId>plexus-component-metadata</artifactId>
  8. <version>1.5.5</version>
  9. <executions>
  10. <execution>
  11. <goals>
  12. <goal>generate-metadata</goal>
  13. </goals>
  14. </execution>
  15. </executions>
  16. </plugin>
  17. ...
  18. </plugins>
  19. ...
  20. </build>

Use your extension in your build(s)

You have two 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.

NOTE: if you use a build extension mechanism the method afterSessionStart won't be called

Use a extension in your project, declare as it in your pom:

  1. <build>
  2. ...
  3. <extensions>
  4. ...
  5. <extension>
  6. <groupId>org.apache.maven.extensions</groupId>
  7. <artifactId>beer-maven-lifecycle</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. </extension>
  10. ...
  11. </extensions>
  12. ...
  13. </build>