27

我正在使用 Maven 开展一个项目,为此我需要两个新阶段:“分析”和“评估”(用于数据分析的不同阶段)。我已经阅读了我能找到的所有文档,并创建了尽可能接近正确的 components.xml 和生命周期.xml 版本,但 Maven 拒绝运行新阶段。(我应该强调,让我的插件工作没有问题,将它们绑定到默认生命周期提供的现有阶段也没有问题。我的问题是我创建的新阶段似乎被 maven 忽略了。)我该怎么办需要做什么才能让我的新阶段发挥作用?

生命周期.xml:

<lifecycles>
  <lifecycle>
    <id>lenskit</id>
    <phases>
      <phase>
        <id>analyze</id>
        <executions>
          <execution>
            <goals>
              <goal>greet</goal>
            </goals>
          </execution>
        </executions>
      </phase>
      <phase>
        <id>validate</id>
        <executions>
          <execution>
            <goals>
              <goal>greet</goal>
            </goals>
          </execution>
        </executions>
      </phase>
    </phases>
  </lifecycle>
</lifecycles>

组件.xml:

<component-set>
  <components>
    <component>
      <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
      <role-hint>lenskit</role-hint>
      <implementation>
        org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
      </implementation>
      <configuration>
         <id>lenskit</id>
         <phases>
            <phase>get-data</phase>
            <phase>analyze</phase>
            <phase>eval</phase>
         </phases>
         <default-phases>
            <verify> org.apache.maven.plugins:maven-resources-plugin:resources </verify>
            <get-data> org.riedl:hello-lenskit-plugin:greet </get-data>
            <analyze> org.riedl:hello-lenskit-plugin:greet </analyze>
            <eval> org.riedl:hello-lenskit-plugin:greet </eval>
            <package> org.riedl:hello-lenskit-plugin:greet </package>
         </default-phases>
      </configuration>
    </component>
  </components>
</component-set>
4

2 回答 2

23

Maven 邮件列表中的一个向导向我指出了一个完整的工作示例,该示例完全符合我的要求:它创建了一个自定义生命周期,其中包含您想要的任何阶段,并允许您从 Maven 命令行使用该生命周期。该示例位于:

https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-scm-publish-plugin

缺少的关键见解是 components.xml 文件必须同时具有 LifecycleMapping 组件Lifecycle 组件。正确的、有效的 components.xml 如下。

请注意,您只能定义一个附加生命周期(即除了三个默认生命周期:构建、清理和站点生命周期之外)。原始生命周期及其阶段将始终存在,并且您不能在新生命周期中包含名称与现有生命周期匹配的任何阶段,这太糟糕了,因为重新定义项目的完整生命周期会更加尴尬。不过,这是向前迈出的一大步。

另外,请记住,当您使用新的生命周期时,您必须将其标记为扩展

<extensions>true</extensions>

因此允许插件重新定义生命周期。为了表明你的 pom.xml 应该使用新的生命周期,包括生命周期名称作为你项目的“包装”。

<component-set>
  <components>
    <component>
      <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
      <role-hint>lenskit</role-hint>
      <implementation>
    org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
      </implementation>
    </component>
    <component>
      <role>org.apache.maven.lifecycle.Lifecycle</role>
      <implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
      <role-hint>lenskit</role-hint>
      <configuration>
     <id>lenskit</id>
     <phases>
        <phase>get-data</phase>
        <phase>analyze</phase>
        <phase>eval</phase>
     </phases>
     <default-phases>
        <get-data>org.riedl:hello-lenskit-plugin:greet</get-data>
        <analyze>org.riedl:hello-lenskit-plugin:greet</analyze>
        <eval>org.riedl:hello-lenskit-plugin:greet</eval>
     </default-phases>
      </configuration>
    </component>
  </components>
</component-set>
于 2012-09-17T23:01:47.570 回答
15

Maven 中有许多错误会干扰 John 的方法:

以下内容实际上应该是约翰答案的一部分,但我的编辑与评论他的方法而不是强调该方法的当前局限性相混淆......所以这里它们是第二个答案:rolleyes:

这需要 Maven 3.0 或更高版本才能工作。当您尝试在 Maven 2.x 中使用它时,您将收到如下错误:

$ mvn eval
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Invalid task 'eval': you must specify a valid lifecycle phase, or a goal 
in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Sep 18 15:58:12 IST 2012
[INFO] Final Memory: 2M/81M
[INFO] ------------------------------------------------------------------------

至少从 Maven 3.0.4 开始,当您在没有目标或阶段的情况下调用 Maven 时,它不会在帮助文本中列出这些阶段:

$ mvn
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.224s
[INFO] Finished at: Tue Sep 18 16:03:20 IST 2012
[INFO] Final Memory: 2M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] No goals have been specified for this build. You must specify a valid 
lifecycle phase or a goal in the format <plugin-prefix>:<goal> or 
<plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available 
lifecycle phases are: validate, initialize, generate-sources, process-sources, 
generate-resources, process-resources, compile, process-classes, 
generate-test-sources, process-test-sources, generate-test-resources, 
process-test-resources, test-compile, process-test-classes, test, 
prepare-package, package, pre-integration-test, integration-test, 
post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, 
pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException

在多模块构建中,您最终可能会搞砸。

考虑以下两个扩展模块:

扩展 1

<component-set>
  <components>
    <component>
      <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
      <role-hint>fancy</role-hint>
      <implementation>
        org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
      </implementation>
    </component>
    <component>
      <role>org.apache.maven.lifecycle.Lifecycle</role>
      <implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
      <role-hint>fancy</role-hint>
      <configuration>
         <id>fancy</id>
         <phases>
            <phase>fancy</phase>
         </phases>
         <default-phases>
            <fancy>org.codehaus.mojo:rpm-maven-plugin:version</fancy>
         </default-phases>
      </configuration>
      </configuration>
    </component>
  </components>
</component-set>

扩展 2

<component-set>
  <components>
    <component>
      <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
      <role-hint>poncy</role-hint>
      <implementation>
        org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
      </implementation>
    </component>
    <component>
      <role>org.apache.maven.lifecycle.Lifecycle</role>
      <implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
      <role-hint>poncy</role-hint>
      <configuration>
         <id>poncy</id>
         <phases>
            <phase>poncy</phase>
            <phase>fancy</phase>
         </phases>
         <default-phases>
            <poncy>org.apache.maven.plugins:maven-enforcer-plugin:display-info</poncy>
            <fancy>org.codehaus.mojo:build-helper-maven-plugin:parse-version</fancy>
         </default-phases>
      </configuration>
    </component>
  </components>
</component-set>

家长会

<?xml version="1.0" encoding="utf-8"?>
<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>localdomain.localhost</groupId>
  <artifactId>fancy-lifecycle-parent</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>poncy-test</module>
    <module>fancy-test</module>
  </modules>

</project>

花式测试 pom

<?xml version="1.0" encoding="utf-8"?>
<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>localdomain.localhost</groupId>
  <artifactId>fancy-test</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>fancy</packaging>

  <build>
    <extensions>
      <extension>
        <groupId>localdomain.localhost</groupId>
        <artifactId>fancy-lifecycle</artifactId>
        <version>0.1-SNAPSHOT</version>
      </extension>
    </extensions>
  </build>
</project>

Poncy 测试 pom

<?xml version="1.0" encoding="utf-8"?>
<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>localdomain.localhost</groupId>
  <artifactId>poncy-test</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>poncy</packaging>

  <build>
    <extensions>
      <extension>
        <groupId>localdomain.localhost</groupId>
        <artifactId>poncy-lifecycle</artifactId>
        <version>0.1-SNAPSHOT</version>
      </extension>
    </extensions>
  </build>
</project>

一些测试

$ mvn -f pom.xml fancy
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] poncy-test
[INFO] fancy-test
[INFO] fancy-lifecycle-parent
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building poncy-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-enforcer-plugin:1.1.1:display-info (default-display-info) @ poncy-test ---
[INFO] Maven Version: 3.0.4
[INFO] JDK Version: 1.6.0_35 normalized as: 1.6.0-35
[INFO] OS Info: Arch: x86_64 Family: mac Name: mac os x Version: 10.8.1
[INFO] 
[INFO] --- build-helper-maven-plugin:1.7:parse-version (default-parse-version) @ poncy-test ---
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building fancy-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- rpm-maven-plugin:2.1-alpha-2:version (default-version) @ fancy-test ---
[WARNING] rpm version string truncated to 0.1
[INFO] setting [rpm.version] property to value [0.1].
[INFO] setting [rpm.release] property to value [SNAPSHOT20120918152051].
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building fancy-lifecycle-parent 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] poncy-test ........................................ SUCCESS [0.727s]
[INFO] fancy-test ........................................ SUCCESS [0.196s]
[INFO] fancy-lifecycle-parent ............................ SUCCESS [0.001s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.152s
[INFO] Finished at: Tue Sep 18 16:20:51 IST 2012
[INFO] Final Memory: 4M/81M
[INFO] ------------------------------------------------------------------------

第一个测试来自父 pom,一切正常。请注意,两个阶段 (poncyfancy的执行是为poncy-test模块调用的,但只有一个阶段 ( fancy) 是为fancy-test模块调用的,正如您所期望的那样。

poncy现在只在阶段尝试相同

$ mvn poncy
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] poncy-test
[INFO] fancy-test
[INFO] fancy-lifecycle-parent
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building poncy-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-enforcer-plugin:1.1.1:display-info (default-display-info) @ poncy-test ---
[INFO] Maven Version: 3.0.4
[INFO] JDK Version: 1.6.0_35 normalized as: 1.6.0-35
[INFO] OS Info: Arch: x86_64 Family: mac Name: mac os x Version: 10.8.1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building fancy-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] poncy-test ........................................ SUCCESS [0.588s]
[INFO] fancy-test ........................................ FAILURE [0.033s]
[INFO] fancy-lifecycle-parent ............................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.853s
[INFO] Finished at: Tue Sep 18 16:23:27 IST 2012
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Unknown lifecycle phase "poncy". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, fancy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

请注意,我们现在有一个构建失败,因为该fancy-test模块的阶段是未知的。

如果我们构建父级并poncy-test明确地一切正常:

$ mvn poncy -pl :fancy-lifecycle-parent,:poncy-test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] poncy-test
[INFO] fancy-lifecycle-parent
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building poncy-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-enforcer-plugin:1.1.1:display-info (default-display-info) @ poncy-test ---
[INFO] Maven Version: 3.0.4
[INFO] JDK Version: 1.6.0_35 normalized as: 1.6.0-35
[INFO] OS Info: Arch: x86_64 Family: mac Name: mac os x Version: 10.8.1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building fancy-lifecycle-parent 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] poncy-test ........................................ SUCCESS [5.247s]
[INFO] fancy-lifecycle-parent ............................ SUCCESS [0.001s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.488s
[INFO] Finished at: Tue Sep 18 16:24:45 IST 2012
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------

最后,积极的一面是,如果我们添加一个jar没有定义扩展的打包模块,所有的地狱都不会松散:

$ mvn fancy
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] jar-test
[INFO] poncy-test
[INFO] fancy-test
[INFO] fancy-lifecycle-parent
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jar-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building poncy-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-enforcer-plugin:1.1.1:display-info (default-display-info) @ poncy-test ---
[INFO] Maven Version: 3.0.4
[INFO] JDK Version: 1.6.0_35 normalized as: 1.6.0-35
[INFO] OS Info: Arch: x86_64 Family: mac Name: mac os x Version: 10.8.1
[INFO] 
[INFO] --- build-helper-maven-plugin:1.7:parse-version (default-parse-version) @ poncy-test ---
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building fancy-test 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- rpm-maven-plugin:2.1-alpha-2:version (default-version) @ fancy-test ---
[WARNING] rpm version string truncated to 0.1
[INFO] setting [rpm.version] property to value [0.1].
[INFO] setting [rpm.release] property to value [SNAPSHOT20120918152733].
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building fancy-lifecycle-parent 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] jar-test .......................................... SUCCESS [0.001s]
[INFO] poncy-test ........................................ SUCCESS [0.809s]
[INFO] fancy-test ........................................ SUCCESS [0.198s]
[INFO] fancy-lifecycle-parent ............................ SUCCESS [0.001s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.244s
[INFO] Finished at: Tue Sep 18 16:27:33 IST 2012
[INFO] Final Memory: 4M/81M
[INFO] ------------------------------------------------------------------------
于 2012-09-18T15:47:25.810 回答