Creating a new phase
Asked Answered
M

2

29

I am working on a project using Maven for which I need two new phases: 'analyze' and 'eval' (for different phases of data analysis). I've read all the docs I can find, and created versions of components.xml and lifecycle.xml that are as close as I can get to correct, but Maven refuses to run the new phases. (I should emphasize that I have no problem getting my plugins to work, and no problem binding them to the existing phases provided by the default lifecycle. My problem is that the new phases I create seem to be ignored by maven.) What do I need to do to get my new phases to work?

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>

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>
Mn answered 14/9, 2012 at 23:12 Comment(3)
Are you really talking about a new lifecycle phase or are you talking about a goal ? I don't the need for a new lifecycle phase.Sinless
My reasoning for wanting a new phase is an interface issue. I think it will be confusing to users to ask them to type, e.g., "mvn integration-tests" when they want to run the evaluations. I thought maven is so adaptable it should be easy to get a new phase to work, but it's been a complete fail so far.Mn
Evaluations is not the intention of mvn furthermore to run integration-tests its better to to mvn verify cause it will run also the post-integration-test phase.Sinless
M
25

One of the wizards on the Maven mailing list pointed me to a complete working example that does exactly what I wanted: it creates a custom lifecycle with phases named whatever you want, and lets you use that lifecycle from the maven command-line. The example is at:

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

The key missing insight is that the components.xml file must have both a LifecycleMapping component and a Lifecycle component. The correct, working components.xml is below.

Note that you may only define an additional lifecycle (i.e. additional to the three default lifecycles: the build, clean and site lifecycles). The original lifecycles and their phases will always be present and you cannot include any phases with names that match an existing lifecycle in your new lifecycle, which is too bad, since it makes it more awkward to redefine a complete lifecycle for a project. Still, this is a nice step forward.

Also, remember that when you use the new lifecycle, you must mark it as an extension:

<extensions>true</extensions>

so the plugin is allowed to redefine the lifecycle. To indicate that your pom.xml should use the new lifecycle, include the lifecycle name as the "packaging" for your project.

<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>
Mn answered 17/9, 2012 at 23:1 Comment(1)
Here's a newer link (on github) for that example component: github.com/apache/maven-scm-publish-pluginGamosepalous
K
15

There are a number of bugs in Maven that interfer with John's approach:

The following should actually be part of John's answer but my edits were confused with commenting about his approach rather than highlighting the current limitations of the approach... so here they are as a second answer :rolleyes:

This requires Maven 3.0 or newer to work. When you try to use this in Maven 2.x you will get a error such as:

$ 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] ------------------------------------------------------------------------

At least as of Maven 3.0.4 it does not list these phases in the help text when you invoke Maven without a goal or phase:

$ 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

In a multi-module build you can end up screwed.

Consider the following two extenions modules:

Extension 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>

Extension 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>

Parent 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>

Fancy-test 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-test 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>

Some tests

$ 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] ------------------------------------------------------------------------

This first test is from the parent pom and everything builds just fine. Notice that the two phases (poncy and fancy's executions are invoked for the poncy-test module but only one phase (fancy) is invoked for the fancy-test module, as you would expect.

Now try the same only with the poncy phase

$ 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

Notice that we now have a build failure because the phase is unknown for the fancy-test module.

If we build the parent and poncy-test explicitly all works just fine:

$ 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] ------------------------------------------------------------------------

And finally, on a positive note, if we add a jar packaging module without the extension defined into the mix, all hell does not break loose:

$ 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] ------------------------------------------------------------------------
Kinky answered 18/9, 2012 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.