Maven的插件机制是完全依赖Maven的生命周期的,因此理解生命周期至关重要。本文参考官方文档后使用archetype创建,手动创建太麻烦。
- 创建创建项目
选择maven-archetype-mojo
创建的项目结构如下
这里要理解一个术语:mojo,就是Maven Plain Old Java Object,也就是一个普通的Java类,我们写的插件主要是实现AbstractMojo,而AbstractMojo只有一个public void execute()
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.oseye</groupId> <artifactId>MvnPluginDemo</artifactId> <packaging>maven-plugin</packaging> <version>0.0.1-SNAPSHOT</version> <name>MvnPluginDemo Maven Mojo</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
- 实现execute
我们修改MyMojo.java的代码,来做一个非常简单的插件:package net.oseye; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; /** * @goal oseye */ public class MyMojo extends AbstractMojo{ public void execute()throws MojoExecutionException { getLog().info("这是一个测试插件,goal是oseye"); } }
- getLog()获取的是AbstractMojo内部的log,类型是:org.apache.maven.plugin.logging.Log;至少在Mojo的开发中,不要使用其他的Log基础设施;
- @goal标注,goal是maven里定义任务的最小单元,它可以单独,也可以挂在phase上,一个phase可以挂多个goal;这里是单独的;
- 更多标注信息请参考官方文档;
- 部署&执行
部署插件到本地仓库
mvn install
<build> <plugins> <plugin> <groupId>net.oseye</groupId> <artifactId>MvnPluginDemo</artifactId> <version>0.0.1-SNAPSHOT</version> </plugin> </plugins> </build>
mvn groupId:artifactId:version:goal
mvn net.oseye:MvnPluginDemo:oseye
[INFO] 这是一个测试插件,goal是oseye
- 使用简单命令执行
如果你的artifacitId符合格式 ${prefix}-maven-plugin 或 maven-${prefix}-plugin,可以在你的settings.xml配置文件中加上<pluginGroups> <pluginGroup>net.oseye</pluginGroup> </pluginGroups>
mvn MvnPluginDemo:oseye
- @execute
这个标注是有三种方式:@execute phase="" lifecycle=""
@execute phase=""
@execute goal=""package net.oseye; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; /** * @goal oseye * @execute phase="compile" */ public class MyMojo extends AbstractMojo{ public void execute()throws MojoExecutionException { getLog().info("这是一个测试插件,goal是oseye"); } }
- @phase
这个标注表示默认的phase,如:package net.oseye; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; /** * @goal oseye * @phase validate */ public class MyMojo extends AbstractMojo{ public void execute()throws MojoExecutionException { getLog().info("这是一个测试插件,goal是oseye"); } }
<build> <plugins> <plugin> <groupId>net.oseye</groupId> <artifactId>MvnPluginDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <executions> <execution> <goals> <goal>oseye</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
mvn compile
<build> <plugins> <plugin> <groupId>net.oseye</groupId> <artifactId>MvnPluginDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <executions> <execution> <phase>compile</phase> <goals> <goal>oseye</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- 参数
插件可以接受从配置文件注入的数据,这里还是使用标注,格式是:@parameter expression="${aSystemProperty}" default-value="${anExpression}"
package net.oseye; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; /** * @goal oseye * @phase validate */ public class MyMojo extends AbstractMojo{ /** * @parameter expression=”${oseye.words}” default-value=”goodBye to you!” */ private String words; public void execute()throws MojoExecutionException { getLog().info("这是一个测试插件,goal是oseye"+words); } }
<build> <plugins> <plugin> <groupId>net.oseye</groupId> <artifactId>MvnPluginDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <executions> <execution> <phase>compile</phase> <goals> <goal>oseye</goal> </goals> <configuration> <words>goodbye everyone</words> </configuration> </execution> </executions> </plugin> </plugins> </build>
[INFO] 这是一个测试插件,goal是oseyegoodbye everyone