步驟:apache
1.建立Maven項目api
2. 添加pom依賴maven
<dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
3. 新建Mojo類ide
註解形式:ui
@Mojo(name = "hello", requiresDependencyResolution = ResolutionScope.COMPILE) public class HelloMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { System.out.println("Hello Mojo!"); } }
doc形式:插件
/** * @goal hello */ public class HelloMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { System.out.println("Hello Mojo!"); } }
Mojo 的查找機制:在處理源碼的時候,plugin-tools 會把使用了 @Mojo 註解或 Javadoc 裏包含 @goal 註釋的類來看成一個 Mojo 類code
4.構建發佈mojo插件本地blog
mvn install
5.如何用?ci
<plugin> <groupId>com.code</groupId> <artifactId>mojo-demo</artifactId> <version>1.0-SNAPSHOT</version> <executions> <execution> <phase>compile</phase> <goals> <goal>hello</goal> </goals> </execution> </executions> </plugin>
在使用使用的項目中引入此插件,<goal>指定運行的mojo,在項目compile時自動運行此插件。源碼