2012-03-13 35 views
1
阶段

我有一个使用pom.xml中Maven2的绑定到一个自定义

<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>pram.plugintest</groupId> 
    <artifactId>pram.plugintest</artifactId> 
    <packaging>maven-plugin</packaging> 
    <version>1.1-SNAPSHOT</version> 
    <name>pram.plugintest Maven Mojo</name> 
    <url>http://maven.apache.org</url> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-plugin-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <goalPrefix>blah</goalPrefix> 
       </configuration> 
      </plugin> 
     </plugins> 
     <resources> 
      <resource> 
       <directory>src/main/resources</directory> 
      </resource> 
     </resources> 
    </build> 
    <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> 
</project> 

定义运行

mvn blah:touch 

创建在目标目录中的一个文本文件,如预期的那样自定义插件。我现在建立在POM

<lifecycles> 
    <lifecycle> 
     <id>touch</id> 
     <phases> 
      <phase> 
       <id>package</id> 
       <executions> 
        <execution> 
         <goals> 
          <goal>touch</goal> 
         </goals> 
        </execution> 
       </executions> 
      </phase> 
     </phases> 
    </lifecycle> 
</lifecycles> 

在另一个Maven项目指定的资源目录中的文件lifecycles.xml,我想的mvn blah:touch运行绑定到与此类似

... 
<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.2.1</version> 
    <executions> 
     <execution> 
     <id>test1</id> 
     <phase>blah:touch</phase> 
     <goals> 
      <goal>java</goal> 
     </goals> 
     <configuration> 
<mainClass>mainClass=org.sonatype.mavenbook.weather.Main</mainClass> 
     </configuration> 
    </execution> 
</executions> 
</plugin> 
... 
执行任务

但是,运行此操作会创建文本文件,但不会尝试运行org.sonatype.mavenbook.weather.Main

这是正确的方法吗?

最终我想在exec-maven-plugin中有多个执行部分没有绑定到默认阶段。从逻辑上讲它应该是这样的

... 
<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.2.1</version> 
    <executions> 
     <execution> 
     <id>test1</id> 
     <phase>blah:touch</phase> 
     <goals> 
      <goal>java</goal> 
     </goals> 
     <configuration> 
<mainClass>mainClass=org.sonatype.mavenbook.weather.Main</mainClass> 
     </configuration> 
    </execution> 
<execution> 
     <id>test2</id> 
     <phase>blah:touch2</phase> 
     <goals> 
      <goal>java</goal> 
     </goals> 
     <configuration> 
<mainClass>mainClass=org.sonatype.mavenbook.weather.SomeOtherClass</mainClass> 
     </configuration> 
    </execution> 
</executions> 
</plugin> 
... 

所以,如果我跑mvn blah:touch然后org.sonatype.mavenbook.weather.Main将被执行,如果我跑mvn blah:touch2然后org.sonatype.mavenbook.weather.SomeOtherClass将转而执行。

它似乎应该是直接做,但在documentation似乎没有指出如何做到这一点。

回答

1

不能使用对于此exec-Maven的插件,你不需要lifecycle.xml如果你只是想在构建过程中执行你的插件。

要在特定的Maven阶段执行你的插件,你只需要添加

<plugins> 
     <plugin> 
      <groupId>your.group.id</groupId> 
      <artifactId>your.artifact.id</artifactId> 
      <executions> 
       <execution> 
        <id>unique-execution-id</id> 
        <goals> 
         <goal>the.goal.of.your.plugin</goal> 
        </goals> 
        <phase>maven.phase</phase> 
        <configuration> 
        .... 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 

请注明在目标元素的目标,而没有前缀。

你看过http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-sect-plugins-lifecycle.html吗?

+0

我想实现的是将exec-maven-plugin中的不同执行部分链接到非默认阶段。我将在原文中对此进行扩展 – Pram 2012-03-13 16:00:58