编写自定义Maven2插件

from:http://jwu.iteye.com/blog/612944

一、创建一个插件项目
    > mvn archetype:create -DgroupId=org.sonatype.mavenbook.plugins -DartifactId=first-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo

    maven会自动到远程库去下载maven-archetype-mojo的插件;
    创建成功会生成一个first-maven-plugin的文件夹,里有一个pom.xml文件,内容:
Java代码 复制代码
  1. <groupId>org.sonatype.mavenbook.plugins</groupId>   
  2. <artifactId>first-maven-plugin</artifactId>   
  3. <packaging>maven-plugin</packaging>   
  4. <version>1.0-SNAPSHOT</version>   
  5. <name>first-maven-plugin Maven Mojo</name>   
  6. <url>http://maven.apache.org</url>   
  7.   
  8. <dependencies>   
  9.     <dependency>   
  10.         <groupId>org.apache.maven</groupId>   
  11.         <artifactId>maven-plugin-api</artifactId>   
  12.         <version>2.0</version>   
  13.     </dependency>   
  14.     <dependency>   
  15.         <groupId>junit</groupId>   
  16.         <artifactId>junit</artifactId>   
  17.         <version>3.8.1</version>   
  18.         <scope>test</scope>   
  19.     </dependency>   
  20. </dependencies>  
	<groupId>org.sonatype.mavenbook.plugins</groupId>
	<artifactId>first-maven-plugin</artifactId>
	<packaging>maven-plugin</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>first-maven-plugin 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>


二、创建一个MOJO
   MOJO就是一个供插件调用处理的普通类。
Java代码 复制代码
  1. /**  
  2.  * Echos an object string to the output screen.  
  3.  * @goal echo  
  4.  */  
  5. public class MyMojo extends AbstractMojo {   
  6.     /**  
  7.      * Any Object to print out.  
  8.      * @parameter expression="${echo.message}" default-value="Hello Maven World..."  
  9.      */  
  10.     private Object message;   
  11.   
  12.     public void execute() throws MojoExecutionException {   
  13.         getLog().info(message.toString());   
  14.     }  
/**
 * Echos an object string to the output screen.
 * @goal echo
 */
public class MyMojo extends AbstractMojo {
	/**
	 * Any Object to print out.
	 * @parameter expression="${echo.message}" default-value="Hello Maven World..."
	 */
	private Object message;

	public void execute() throws MojoExecutionException {
		getLog().info(message.toString());
	}

}

创建了一个MyMojo类,必须继承AbstractMojo类,实现execute方法,这个方法就是插件调用的入口;

三、build, run自定义插件
   > mvn clean install
   插件运行遵循groupId:artifactId:version:goal格式;
   > mvn org.sonatype.mavenbook.plugins:first-maven-plugin:1.0-SNAPSHOT:echo -Decho.message="My first Maven plugin"
上面命令中,出来一个echo,这个就是goal,在MyMojo类里用注释定义@goal echo;
可能上面的命令太长,怎样做到像archetype:create一样写法?可以定义前缀;

四、定义前缀
   在setting.xml文件加:
Java代码 复制代码
  1. <pluginGroups>   
  2.   <pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>   
  3. </pluginGroups>  
  <pluginGroups>
    <pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>
  </pluginGroups>

然后:
    > mvn first:echo -Decho.message="My first Maven plugin"
非常简单。
如果插件的artifactId遵循maven-first-plugin,或者first-maven-plugin模式。Maven就会自动为你的插件赋予前缀first。
${prefix}-maven-plugin, OR maven-${prefix}-plugin
也可自定义前缀,在pom.xml加:
Java代码 复制代码
  1. <build>   
  2.   <plugins>   
  3.     <plugin>   
  4.       <artifactId>first-maven-plugin</artifactId>   
  5.       <version>2.3</version>   
  6.       <configuration>   
  7.         <goalPrefix>first</goalPrefix>   
  8.       </configuration>   
  9.     </plugin>   
  10.   </plugins>   
  11. </build>  
  <build>
    <plugins>
      <plugin>
        <artifactId>first-maven-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <goalPrefix>first</goalPrefix>
        </configuration>
      </plugin>
    </plugins>
  </build>
一、创建一个插件项目
    > mvn archetype:create -DgroupId=org.sonatype.mavenbook.plugins -DartifactId=first-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo

    maven会自动到远程库去下载maven-archetype-mojo的插件;
    创建成功会生成一个first-maven-plugin的文件夹,里有一个pom.xml文件,内容:
Java代码 复制代码
  1. <groupId>org.sonatype.mavenbook.plugins</groupId>   
  2. <artifactId>first-maven-plugin</artifactId>   
  3. <packaging>maven-plugin</packaging>   
  4. <version>1.0-SNAPSHOT</version>   
  5. <name>first-maven-plugin Maven Mojo</name>   
  6. <url>http://maven.apache.org</url>   
  7.   
  8. <dependencies>   
  9.     <dependency>   
  10.         <groupId>org.apache.maven</groupId>   
  11.         <artifactId>maven-plugin-api</artifactId>   
  12.         <version>2.0</version>   
  13.     </dependency>   
  14.     <dependency>   
  15.         <groupId>junit</groupId>   
  16.         <artifactId>junit</artifactId>   
  17.         <version>3.8.1</version>   
  18.         <scope>test</scope>   
  19.     </dependency>   
  20. </dependencies>  
	<groupId>org.sonatype.mavenbook.plugins</groupId>
	<artifactId>first-maven-plugin</artifactId>
	<packaging>maven-plugin</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>first-maven-plugin 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>


二、创建一个MOJO
   MOJO就是一个供插件调用处理的普通类。
Java代码 复制代码
  1. /**  
  2.  * Echos an object string to the output screen.  
  3.  * @goal echo  
  4.  */  
  5. public class MyMojo extends AbstractMojo {   
  6.     /**  
  7.      * Any Object to print out.  
  8.      * @parameter expression="${echo.message}" default-value="Hello Maven World..."  
  9.      */  
  10.     private Object message;   
  11.   
  12.     public void execute() throws MojoExecutionException {   
  13.         getLog().info(message.toString());   
  14.     }  
/**
 * Echos an object string to the output screen.
 * @goal echo
 */
public class MyMojo extends AbstractMojo {
	/**
	 * Any Object to print out.
	 * @parameter expression="${echo.message}" default-value="Hello Maven World..."
	 */
	private Object message;

	public void execute() throws MojoExecutionException {
		getLog().info(message.toString());
	}

}

创建了一个MyMojo类,必须继承AbstractMojo类,实现execute方法,这个方法就是插件调用的入口;

三、build, run自定义插件
   > mvn clean install
   插件运行遵循groupId:artifactId:version:goal格式;
   > mvn org.sonatype.mavenbook.plugins:first-maven-plugin:1.0-SNAPSHOT:echo -Decho.message="My first Maven plugin"
上面命令中,出来一个echo,这个就是goal,在MyMojo类里用注释定义@goal echo;
可能上面的命令太长,怎样做到像archetype:create一样写法?可以定义前缀;

四、定义前缀
   在setting.xml文件加:
Java代码 复制代码
  1. <pluginGroups>   
  2.   <pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>   
  3. </pluginGroups>  
  <pluginGroups>
    <pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>
  </pluginGroups>

然后:
    > mvn first:echo -Decho.message="My first Maven plugin"
非常简单。
如果插件的artifactId遵循maven-first-plugin,或者first-maven-plugin模式。Maven就会自动为你的插件赋予前缀first。
${prefix}-maven-plugin, OR maven-${prefix}-plugin
也可自定义前缀,在pom.xml加:
Java代码 复制代码
  1. <build>   
  2.   <plugins>   
  3.     <plugin>   
  4.       <artifactId>first-maven-plugin</artifactId>   
  5.       <version>2.3</version>   
  6.       <configuration>   
  7.         <goalPrefix>first</goalPrefix>   
  8.       </configuration>   
  9.     </plugin>   
  10.   </plugins>   
  11. </build>  
  <build>
    <plugins>
      <plugin>
        <artifactId>first-maven-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <goalPrefix>first</goalPrefix>
        </configuration>
      </plugin>
    </plugins>
  </build>

猜你喜欢

转载自rept.iteye.com/blog/693517