创建一个新阶段

Joh*_*edl 27 maven-3 maven

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

lifecycles.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>
Run Code Online (Sandbox Code Playgroud)

components.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>
Run Code Online (Sandbox Code Playgroud)

Joh*_*edl 24

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

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

缺少洞察力的关键是components.xml文件必须同时具有LifecycleMapping组件 Lifecycle组件.正确的工作components.xml如下所示.

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

另外,请记住,在使用新生命周期时,必须将其标记为扩展名:

<extensions>true</extensions>
Run Code Online (Sandbox Code Playgroud)

所以允许插件重新定义生命周期.要指示您的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>
Run Code Online (Sandbox Code Playgroud)


Ste*_*lly 14

Maven中存在许多干扰John的方法的错误:

以下实际上应该是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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

至少从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
Run Code Online (Sandbox Code Playgroud)

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

考虑以下两个扩展模块:

扩展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>
Run Code Online (Sandbox Code Playgroud)

扩展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>
Run Code Online (Sandbox Code Playgroud)

父母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-lifecycle-parent</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

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

</project>
Run Code Online (Sandbox Code Playgroud)

花式测试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>
Run Code Online (Sandbox Code Playgroud)

庞西测试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>
Run Code Online (Sandbox Code Playgroud)

一些测试

$ 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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

第一次测试来自父pom,一切都很好.请注意,正如您所期望的那样,为模块调用了两个阶段(poncyfancyponcy-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
Run Code Online (Sandbox Code Playgroud)

请注意,我们现在有一个构建失败,因为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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

最后,从积极的方面来说,如果我们添加一个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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)