Maven mojo插件,如何定义在此目标之前必须触发的阶段?

时间:2010-10-14 15:27:39

标签: maven-2 mojo

哎, 我有一个部署pojo插件(将战争部署到远程服务器)。我在pom定义的构建部分中有远程部署插件,我需要在部署远程目标之前触发包阶段,因为在我将其安全复制到远程服务器之前已经创建了战争。

使用执行元素(根据文档),我可以将我的目标附加到特定阶段,例如将其绑定到之后的阶段,因此在我的情况下,安装阶段......但这只是一种解决方法。 / p>

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

简单地说,如果我只将我的目标放入构建部分并运行它,则之前不运行包阶段。请帮忙

2 个答案:

答案 0 :(得分:1)

  

Maven Mojo插件,如何定义在此目标之前必须触发的阶段?

你不能。

  

我在pom定义的构建部分中有远程部署插件,我需要在部署远程目标之前触发包阶段,因为在我将其安全复制到远程服务器之前已经创建了战争。 / p>

只需将其绑定到package阶段,默认情况下目标就会被package限制后调用你的目标(因此战争会在那里)。

以下是使用如下配置的Maven AntRun插件演示此行为的示例:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
        <phase>package</phase>
        <configuration>
          <target>
            <echo message="Hi!!!!!"/>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

mvn package的输出:

$ mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3934833 Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ Q3934833 ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.5:test (default-test) @ Q3934833 ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:2.1:war (default-war) @ Q3934833 ---
[INFO] Packaging webapp
[INFO] Assembling webapp [Q3934833] in [/home/pascal/Projects/stackoverflow/Q3934833/target/Q3934833]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/pascal/Projects/stackoverflow/Q3934833/src/main/webapp]
[INFO] Webapp assembled in [317 msecs]
[INFO] Building war: /home/pascal/Projects/stackoverflow/Q3934833/target/Q3934833.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] --- maven-antrun-plugin:1.6:run (default) @ Q3934833 ---
[INFO] Executing tasks

main:
     [echo] Hi!!!!!
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

antrun插件按照预期在package之后执行。

答案 1 :(得分:0)

您可以尝试在@Mojo注释中使用PREPARE_PACKAGE阶段:

@Mojo(name = "myName", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)