开发自己的Maven插件之三:使用插件

好,现在创建另一个Maven项目来使用第二篇创建的plugin.注意还是通过私服获取。

pom.xml配置如下:

<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.mycompany</groupId>
  <artifactId>mavenproject1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mavenproject1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
	<groupId>com.freebird</groupId>
	<artifactId>plugin-example1</artifactId>
	<version>1.0</version>
	<executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>touch</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

注意,第二篇产生的代码里面类的注释里包含了@goal touch

/**
 * Goal which touches a timestamp file.
 *
 * @goal touch
 * 
 * @phase process-sources
 */
public class MyMojo
    extends AbstractMojo

因此,在应用程序中,可以在plugin的execution里面设置在compile阶段执行touch goal.

那么当运行mvn compile的时候,就能看到hello world字样显示在终端上。

chenshu@chenshu-beijing:~/NetBeansProjects/mavenproject1$ mvn compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenproject1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ mavenproject1 ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/chenshu/NetBeansProjects/mavenproject1/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ mavenproject1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- plugin-example1:1.0:touch (default) @ mavenproject1 ---
[INFO] hello world
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.971s
[INFO] Finished at: Mon Jul 02 21:00:53 CST 2012
[INFO] Final Memory: 7M/105M
[INFO] ------------------------------------------------------------------------

当然,也可以在命令行里面显示的指定,不过有点罗嗦:

mvn com.freebird:plugin-example1:1.0:touch
格式是 groupId:artifactId:version:goal




你可能感兴趣的:(开发自己的Maven插件之三:使用插件)