我有一个使用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中指定的资源目录中创建一个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中具有多个未绑定到默认阶段的执行部分。逻辑上看起来像这样

...
<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中没有任何内容指出如何执行此操作。

最佳答案

您不能为此使用exec-maven-plugin,如果只想在构建过程中执行插件,则不需要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吗?

07-28 00:48