Maven Plug-In

Maven的一个很明显有别与Ant的优势就是在于明确了工程中所用资源包的版本信息 ,目前我们在工程开发过程中都不免需要用到大量的免费开源的第三方插件,而这些插件在升级过程中的向前兼容做的有时候确实不太理想,就想Hibernate在升级到3.0后包结构的变化一样,让很多开发人员在刚开始使用时都是一头雾水,但是由于Maven2可以明确指明所使用的资源包版本信息,这样就避免了工程中由于资源包版本混乱导致程序崩溃 的问题。

 

一、创建Plug-In项目

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文件,内容:

 

	<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就是一个供插件调用处理的普通类

/**
 * 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" 

 

四、定义前缀

在setting.xml文件加:

  <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加:

  <build>
    <plugins>
      <plugin>
        <artifactId>first-maven-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <goalPrefix>first</goalPrefix>
        </configuration>
      </plugin>
    </plugins>
  </build>
 

You can also configure your plugin to attach specific goals to a particular phase of the build lifecycle. Here is an example:

<build>
    <plugins>
      <plugin>
        <groupId>sample.plugin</groupId>
        <artifactId>hello-maven-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>sayhi</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

This causes the simple mojo to be executed whenever Java code is compiled. For more information on binding a mojo to phases in the lifecycle, please refer to the Build Lifecycle document.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值