Creating a new phase

前端 未结 2 1185
我寻月下人不归
我寻月下人不归 2020-12-13 19:14

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,

2条回答
  •  忘掉有多难
    2020-12-13 19:34

    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 : or 
    :[:]:. 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

    
      
        
          org.apache.maven.lifecycle.mapping.LifecycleMapping
          fancy
          
            org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
          
        
        
          org.apache.maven.lifecycle.Lifecycle
          org.apache.maven.lifecycle.Lifecycle
          fancy
          
             fancy
             
                fancy
             
             
                org.codehaus.mojo:rpm-maven-plugin:version
             
          
          
        
      
    
    

    Extension 2

    
      
        
          org.apache.maven.lifecycle.mapping.LifecycleMapping
          poncy
          
            org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
          
        
        
          org.apache.maven.lifecycle.Lifecycle
          org.apache.maven.lifecycle.Lifecycle
          poncy
          
             poncy
             
                poncy
                fancy
             
             
                org.apache.maven.plugins:maven-enforcer-plugin:display-info
                org.codehaus.mojo:build-helper-maven-plugin:parse-version
             
          
        
      
    
    

    Parent pom

    
    
      4.0.0
    
      localdomain.localhost
      fancy-lifecycle-parent
      0.1-SNAPSHOT
      pom
    
      
        poncy-test
        fancy-test
      
    
    
    

    Fancy-test pom

    
    
      4.0.0
    
      localdomain.localhost
      fancy-test
      0.1-SNAPSHOT
      fancy
    
      
        
          
            localdomain.localhost
            fancy-lifecycle
            0.1-SNAPSHOT
          
        
      
    
    

    Poncy-test pom

    
    
      4.0.0
    
      localdomain.localhost
      poncy-test
      0.1-SNAPSHOT
      poncy
    
      
        
          
            localdomain.localhost
            poncy-lifecycle
            0.1-SNAPSHOT
          
        
      
    
    

    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 : or :[:]:. 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] ------------------------------------------------------------------------
    

提交回复
热议问题