<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qizj.mu</groupId> <artifactId>maven_plugin_test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>maven-plugin</packaging> <name>maven_plugin_test Maven Plugin</name> <!-- FIXME change it to the project's website --> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <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> <scope>provided</scope> </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>3.0.8</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-plugin-plugin</artifactId> <version>3.2</version> <configuration> <goalPrefix>zhangsan</goalPrefix> </configuration> <executions> <execution> <id>mojo-descriptor</id> <goals> <goal>descriptor</goal> </goals> </execution> <execution> <id>help-goal</id> <goals> <goal>helpmojo</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
package com.plugin; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; /** * @goal generator */ public class GeneratorCode extends AbstractMojo { @Override public void execute() throws MojoExecutionException { getLog().info(「11111」); } }
3.發佈maven installweb
4.在其餘的maven項目中添加pluginapache
<build> <plugins> <plugin> <groupId>com.qizj.mu</groupId> <artifactId>maven_plugin_test</artifactId> <version>0.0.1-SNAPSHOT</version> </plugin> </plugins> </build>
5.右鍵項目 run as --> maven build -->goals中輸入zhangsan:generator-->runapi
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Io 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven_plugin_test:0.0.1-SNAPSHOT:generator (default-cli) @ Io --- [INFO] 111 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.365 s [INFO] Finished at: 2016-08-04T13:13:52+08:00 [INFO] Final Memory: 6M/123M [INFO] ------------------------------------------------------------------------