13 7月 2009

Maven: Basic Concepts(Lifecycle, Phase, Goal)

Lifecycle / Phase / Goal
  • A build lifecycle is made up of phases.
    lifecycle將各phase有順序性的串連起來,所以當你指定執行一個phase時,maven將會執行排在其之前的所有phases.


  • A phase is made up of goals.
    一個Phase可以綁定多個Goals,但一般只有預設的一個Goal會被執行,所以要執行其他的Goal時,
    必需明白指出所要執行Goal。一般來說是以plugin:goal來表示,例如compile:test-compile


  • 說明下上面兩者的差別,例如test-compile這個phase,實際上是要執行compiler:testCompile,但是分別執行的結果如下
  • mvn test-compile
    >$ mvn test-compile
    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven
    [INFO]    task-segment: [test-compile]
    [INFO] ------------------------------------------------------------------------
    [INFO] [resources:resources {execution: default-resources}]
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory /Users/elliot/Documents/workspacetesting/test/src/main/resources
    [INFO] [compiler:compile {execution: default-compile}]
    [INFO] No sources to compile
    [INFO] [resources:testResources {execution: default-testResources}]
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory /Users/elliot/Documents/workspacetesting/test/src/test/resources
    [INFO] [compiler:testCompile {execution: default-testCompile}]
    [INFO] No sources to compile
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    
    maven認出test-compile是一個Phase,所以執行了排在test-compile之前的Phase,
    如generate-resources、compile、generate-test-resource,最後在執行完test-compile
    (其實就是compiler:testCompile)後結束。



  • mvn compiler:testCompile
    >$ mvn compiler:testCompile
    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven
    [INFO]    task-segment: [compiler:testCompile]
    [INFO] ------------------------------------------------------------------------
    [INFO] [compiler:testCompile {execution: default-cli}]
    [INFO] No sources to compile
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    
    
    Maven認出這是一個Goal所以只執行了compiler:testCompile就結束,並未執行其他的Goals。



  • Maven預設了三個Lifecycle ,各包涵了下列Phases.
    1. Clean Lifecycle
      • pre-clean
      • clean
      • post-clean
    2. Default Lifecycle
      • validate
      • initialize
      • generate-sources
      • process-sources
      • generate-resources
      • process-resources
      • compile
      • process-classes
      • generate-test-sources
      • process-test-sources
      • process-test-resources
      • test-compile
      • process-test-classes
      • test
      • prepare-package
      • package
      • pre-integration-test
      • integration-test
      • post-integration-test
      • verify
      • install
      • deploy
    3. Site Lifecycle
      • pre-site
      • site
      • post-site
      • site-deploy
    並不是所有的Phase都需要被執行,例如validate,在一般的情形下,單獨執行這個Phase會出現
    No goals needed for project - skipping而結束。
    而有些Phase會因設定的不同而執行不同的Goal,
    例如package,會因為你所設的packaging而執行war:war或jar:jar等不同的Goal。

    沒有留言: