Maven2绑定到自定义阶段



我有一个自定义插件,它是使用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/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中指定的resources目录中创建一个lifecycles.xml文件

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

在另一个maven项目中,我想将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>
    <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

这似乎应该很简单,但文档中似乎没有指出如何做到这一点。

您不能为此使用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>

请在不带前缀的goal元素中指定目标。

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

最新更新