Maven--->學習心得--->maven 的生命週期(LifeCycle)

1.文章內容概述:

任何一個工程從建立開始,到編譯,到測試,到部署..,都是有完整的生命週期的,使用maven管理開發出的project也不例外,只不過全部使用maven管理的project都遵循一套標準的生命週期,即maven project LifeCycle.本文就針對maven管理的project的標準生命週期做詳細解說。html

2.maven管理的project的生命週期:

  2.1基礎知識點:

      1. maven的完整生命週期lifeCycle是由若干階段phase組合而成的,因此有必要先介紹一下組成maven project的lifeCycle的各個階段(phase)
          • validate - validate the project is correct and all necessary information is available
          • compile - compile the source code of the project
          • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
          • package - take the compiled code and package it in its distributable format, such as a JAR.
          • verify - run any checks on results of integration tests to ensure quality criteria are met
          • install - install the package into the local repository, for use as a dependency in other projects locally
          • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
          • ...     其餘phase,這裏沒有一一列舉
          • ...
          • ...
          • ...
      2. maven的完整生命週期又分紅多種,不一樣種類的lifeCycle實際上包含不一樣的phase。換句話說,phase的不一樣組合能夠組合成maven project的多個不一樣的maven lifeCycle,如「the default lifecycle」,「the clean lifecycle」,「the site lifecycle」,maven的相關文件定義了這些lifecycle具體是由哪些phase組成的,The full Maven lifecycle is defined by the components.xml file in the maven-core module, with associated documentation for reference.下面的表格列出了maven中各個lifecycle的組成結構(phase lists)
          • 其中the defaule lifecycle是由下面的phase   順序組合   而成的,
              •     
                validate validate the project is correct and all necessary information is available.
                initialize initialize build state, e.g. set properties or create directories.
                generate-sources generate any source code for inclusion in compilation.
                process-sources process the source code, for example to filter any values.
                generate-resources generate resources for inclusion in the package.
                process-resources copy and process the resources into the destination directory, ready for packaging.
                compile compile the source code of the project.
                process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
                generate-test-sources generate any test source code for inclusion in compilation.
                process-test-sources process the test source code, for example to filter any values.
                generate-test-resources create resources for testing.
                process-test-resources copy and process the resources into the test destination directory.
                test-compile compile the test source code into the test destination directory
                process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
                test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
                prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
                package take the compiled code and package it in its distributable format, such as a JAR.
                pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
                integration-test process and deploy the package if necessary into an environment where integration tests can be run.
                post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
                verify run any checks to verify the package is valid and meets quality criteria.
                install install the package into the local repository, for use as a dependency in other projects locally.
                deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

 

          • 其中the clean lifecycle是由下面的phase   順序組合    而成的,
pre-clean execute processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean execute processes needed to finalize the project cleaning
          • 其中the site lifecycle是由下面的phase   順序組合    而成的,
          • pre-site execute processes needed prior to the actual project site generation
            site generate the project's site documentation
            post-site execute processes needed to finalize the site generation, and to prepare for site deployment
            site-deploy deploy the generated site documentation to the specified web server

3.從上面一部份內容咱們已經知道,maven project中實際上是有三種lifeCycle(即the default/clean/site lifecycle),另外,還須要知道,有一些maven相關的命令來控制你的執行過程,包括控制你執行的是哪個生命週期(是default,clean仍是site 生命週期),還包括控制執行到具體某個生命週期的哪一個phase(階段)git

          •  執行相應的maven命令時,系統會按照上述表格,    順序執行   具體某個lifeCycle   的 若干phase 
          • 如命令mvn install,則順序執行the default lifecycle中的 validate、initialize、generate-sources.......verify、install這23個階段(phase) 
          • 再如 mvn clean,則順序執行the clean lifecycle中的pre-clean、clean這2個階段(phase)     
          • 再如 mvn clean install,則先執行the clean lifecycle的相應(pre-clean、clean)這兩個phase以後,又執行the default lifecycle中的validate、initialize、generate-sources.......verify、install這23個階段(phase))

4.phase能夠綁定plugin goal,同一個phase綁定不一樣的plugin goal的時候,實際運行時會產生不一樣的行爲web

        • if a goal is bound to one or more build phases, that goal will be called in all those phases.
        • Furthermore, a build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute.
        • But if it has one or more goals bound to it, it will execute all those goals
        • maven 工程自己已經爲不一樣package模式下的the default lifecycle的若干phase默認配置了一些 plugin,參見maven官網documentation
        • 例一,以下命令的運行機制
            mvn clean dependency:copy-dependencies package

          首先能夠看到該命令涉及了兩個phase,分別是the clean lifecycle的clean和the default lifecycle的package這兩個phase;其次能夠看到該命令還綁定了一個plugin goal(dependency:copy-dependencies)。因此這個命令的總體運行過程是這樣的:If this were to be executed, the clean phase will be executed first (meaning it will run all preceding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceding build phases of the default lifecycle).也就是說,先運行the clean lifecycle的pre-clean、clean這兩個phase,而後運行 dependency:copy-dependencies goal,再而後運行the default lifecycle的validate、initialize、generate-sources.......verify、install這23個階段(phase)apache

        • 除了上述例子中的 dependency:copy-dependencies goal,還有哪些plugin goal是能夠被綁定在phase上的呢,下表中列舉了能夠綁定在the default lifecycle的各個phase的plugin goal,
            • Some phases have goals bound to them by default.And for the default lifecycle, these bindings depend on the packaging value. (下面的表格列舉出maven內部默認綁定在各個phase上的plugin,這些都是默認狀況下就已經綁定好的)
            • Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war

              process-resources resources:resources
              compile compiler:compile
              process-test-resources resources:testResources
              test-compile compiler:testCompile
              test surefire:test
              package ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
              install install:install
              deploy deploy:deploy
            • Default Lifecycle Bindings - Packaging ear

              generate-resources ear:generate-application-xml
              process-resources resources:resources
              package ear:ear
              install install:install
              deploy deploy:deploy
            • Default Lifecycle Bindings - Packaging maven-plugin

              generate-resources plugin:descriptor
              process-resources resources:resources
              compile compiler:compile
              process-test-resources resources:testResources
              test-compile compiler:testCompile
              test surefire:test
              package jar:jar and plugin:addPluginArtifactMetadata
              install install:install
              deploy deploy:deploy
            • Default Lifecycle Bindings - Packaging pom

              package site:attach-descriptor
              install install:install
              deploy deploy:deploy
            • Clean Lifecycle Bindings

              clean clean:clean
            • Site Lifecycle Bindings

              site site:site
              site-deploy site:deploy
            • In Maven 2.x, default lifecycle bindings were included in components.xml, but in Maven 3.x, they are defined in a separate default-bindings.xml descriptor。             pom、jar、ejb、ejb三、maven-plugin、war、ear、rar、par packaging能夠綁定的plugin goal,參見maven官網documentation

        

  2.2相關命令:

     1)若干經常使用命令:

      1.   mvn install     在開發環境中執行此命令。 這個命令是執行default lifeCycle的相應phase,執行此命令時,會順序執行the default lifecycle 的validate、initialize、generate-sources.......verify、install這23個階段(phase)。命令執行的最終結果是 build and install artifacts into the local repository,也即將本身的項目打包好放到maven的本地倉庫。
      2.  mvn clean deploy  In a build environment中執行此命令,  這個命令是執行the clean lifecycle的相應phase(pre-clean、clean)以後又執行the default lifecycle中的相應phase(即validate、initialize、generate-sources.......verify、install這23個階段(phase)),命令執行的最終結果是 cleanly build and deploy artifacts into the shared repository,也即將本身的項目部署到maven的中央倉庫,從而使得其餘人可使用你的項目。
      3.  mvn clean dependency:copy-dependencies package這個命令的總體運行過程是這樣的:先運行the clean lifecycle的pre-clean、clean這兩個phase,而後運行 dependency:copy-dependencies goal,再而後運行the default lifecycle的validate、initialize、generate-sources.......verify、install這23個階段(phase)。
      4.  

 

     2)這些mvn命令的運行機制

        • 首先,他會根據mvn具體命令查找該命令所涉及的phase,
        • 其次,他會根據phase查找對應的lifeCycle(判斷涉及default、clean、site 生命週期中的哪個或者哪幾個lifeCycle)
        • 再次,順序執行相應lifecycle 的一系列phase
        • 最後要注意的是,在真正執行相應phase的時候,要注意phase具體綁定的plugin goals,由於plugin goals決定了相應phase的具體行爲(即便是相同的phase,綁定了一個plugin goal時會有一種行爲,綁定另外一個plugin時又會有另一種行爲)
      • 例一,以下命令的運行機制
          mvn clean dependency:copy-dependencies package

        首先能夠看到該命令涉及了兩個phase,分別是the clean lifecycle的clean和the default lifecycle的package這兩個phase;其次能夠看到該命令還綁定了一個plugin goal(dependency:copy-dependencies)。因此這個命令的總體運行過程是這樣的:If this were to be executed, the clean phase will be executed first (meaning it will run all preceding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceding build phases of the default lifecycle).也就是說,先運行the clean lifecycle的pre-clean、clean這兩個phase,而後運行 dependency:copy-dependencies goal,再而後運行the default lifecycle的validate、initialize、generate-sources.......verify、install這23個階段(phase)。app

相關文章
相關標籤/搜索