| --- |
| Example: Maven 3 lifecycle extension |
| --- |
| Olivier Lamy |
| --- |
| 2012-03-07 |
| --- |
| |
| ~~ 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. |
| |
| ~~ NOTE: For help with the syntax of this file, see: |
| ~~ http://maven.apache.org/doxia/references/apt-format.html |
| |
| Example: Using Maven 3 lifecycle extension |
| |
| * Lifecyle Participation |
| |
| You can extend multiple classes depending on your needs: |
| |
| * {{{/ref/current/maven-core/apidocs/index.html?org/apache/maven/execution/AbstractExecutionListener.html}<<<org.apache.maven.execution.AbstractExecutionListener>>>}}, |
| |
| * {{{/ref/current/maven-core/apidocs/index.html?org/apache/maven/AbstractMavenLifecycleParticipant.html}<<<org.apache.maven.AbstractMavenLifecycleParticipant>>>}}, |
| |
| * {{{/ref/current/maven-core/apidocs/index.html?org/apache/maven/eventspy/AbstractEventSpy.html}<<<org.apache.maven.eventspy.AbstractEventSpy>>>}}, |
| |
| [] |
| |
| * Build your extension |
| |
| Create a Maven project with a dependency on <<<org.apache.maven:maven-core:${currentStableVersion}>>> 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>${currentStableVersion}</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 {{{/ref/current/maven-embedder/core-extensions.html}<<<.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> |
| +---+ |