maven生命週期

 maven 多多少少用過,通常只是用,遇到問題還是不行(各種鬧笑話),琢磨着把幾個核心命令啃一遍。maven是基於構建build的生命週期,內置的builddistribute的流程是有清晰定義的,內置構建build生命週期包含三個:defaultcleansite

  • default:處理項目部署;
  • clean:處理項目清理;
  • site:處理項目網站文檔;

1. 項目構建生命週期的各個階段

 上述提到了3個構建的生命週期,每個構建週期都是由一系列的構建階段組成,一個構建階段代表一個生命週期。下面的構建階段已經去除了很多pre..post..等階段的信息,除非開發maven插件,正常情況不太用得到。

1.1 default構建的生命週期

default構建生命週期,內部包含的階段有:

  • validate:驗證項目是正常的且所有必要的信息都是可獲取的;
  • compile:編譯項目代碼;
  • test:對編譯的代碼使用合適的單元測試框架進行測試,這個過程不需要打包和部署;
  • package:將編譯後的代碼打包爲合適的形式,比如jar或war;
  • verify:運行集成測試以檢查結果,確保質量達到要求;
  • install:將打包好的jar安裝到本地倉庫,以便於其他模塊的使用;
  • deploy:將上述的jar包發佈到遠程倉庫,以供其他開發者使用;

上述的7個階段會按序執行,一個都不會少(不太恰當,可以理解,可以看下面的註解),這樣一整套完整的流程就是default構建的生命週期了。注意:clean不屬於default構建階段,它是單獨的構建週期

【注】

 這裏需要注意的是,如果想直接進入上述7個階段中的某個階段(不含第一階段validate),默認是不可能的,默認maven就是按照上述的7個階段按序執行的,即要想進入某個階段,那該階段之前的階段一個都不能少,除非手動指定跳過某個可跳過的階段。比如當執行mvn install時,它會依次執行install階段之前的所有階段(validatecompiletestpackageverify),最後執行進入install階段。

1.2 clean構建的生命週期

clean構建週期比較單純,它就一個階段:

  • clean:清除階段,將之前構建的文件清除,即模塊目錄${basedir}下整個target目錄。

1.3 site構建的聲明週期(沒有用過)

site構建也比較單純,也一個階段,但劃分爲兩個階段會更好理解一點:

  • site:生成項目網站文檔;
  • site-deploy:將上述生成的文檔部署到指定服務器上;

2. 具體的maven命令

2.1 mvn compile

 編譯,這個階段maven會去下載模塊做需要的各種資源(如果本地倉庫存在則優先從本地抓取)進行編譯(不包含測試類的編譯),編譯的類統一放在模塊的${basedir}/target/classes文件夾下,同時產生sources文件夾,然後將上述的兩個統一打包爲形式爲xxx.sources.jar的資源jar包。下面是編譯的小栗子:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.glodon:wu-pull >-------------------------
[INFO] Building wu-pull 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ wu-pull ---
[INFO] Compiling 8 source files to D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\classes
[INFO]
[INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ wu-pull >>>
[INFO]
[INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ wu-pull <<<
[INFO]
[INFO]
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ wu-pull ---
[INFO] Building jar: D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\wu-util-pull-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.596 s
[INFO] Finished at: 2019-01-10T11:05:07+08:00
[INFO] ------------------------------------------------------------------------

2.2 mvn test

 測試,這個過程中maven會下載額外的資源以編譯測試類並執行(這個階段必須建立在已經編譯獲得類資源了,因爲測試通常是針對模塊中的某些類進行測試,依賴於它們),如果只是單純的想編譯一下測試類但不執行,使用mvn test-compile(這個編譯過程和mvn compile類型會產生test-sourcestest-classes文件夾),所以可以理解爲mvn test是包含了編譯測試類和測試測試類兩個過程,會輸出測試類的結果的測試報告,如果測試通過會生成一份測試報告放在對應模塊中的surefire-reports文件夾下。下面是兩個命令打印出來的日誌:

1.mvn test-compile

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.glodon:wu-pull >-------------------------
[INFO] Building wu-pull 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ wu-pull ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ wu-pull >>>
[INFO]
[INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ wu-pull <<<
[INFO]
[INFO]
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ wu-pull ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ wu-pull ---
[INFO] Compiling 1 source file to D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\test-classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.459 s
[INFO] Finished at: 2019-01-10T11:07:37+08:00
[INFO] ------------------------------------------------------------------------

2.mvn test

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.glodon:wu-pull >-------------------------
[INFO] Building wu-pull 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ wu-pull ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ wu-pull >>>
[INFO]
[INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ wu-pull <<<
[INFO]
[INFO]
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ wu-pull ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ wu-pull ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ wu-pull ---
[INFO] Surefire report directory: D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.goujianwu.service.impl.DownloadServiceImplTest
測試。。。。。。。。。。
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.058 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

2.3 mvn package

 打包,編譯執行成功部署時需要打包(jar或war等等),至於什麼形式注意查看pom文件中設置的格式(Springboot項目默認是打成jar包),此時會在${basedir}/target目錄下生成jar包,正常情況我們部署服務時就是需要這個jar包。下面是打包的日誌:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] -------------------------< com.glodon:wu-pull >-------------------------
[INFO] Building wu-pull 1.0-SNAPSHOT:22+08:00
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] rogram Files\JetBrains\IDEAProjects\wu-util\wu-pull>
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ wu-pull ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ wu-pull >>>
[INFO]
[INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ wu-pull <<<
[INFO]
[INFO]
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ wu-pull ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ wu-pull ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ wu-pull ---
[INFO] Surefire report directory: D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.goujianwu.service.impl.DownloadServiceImplTest
測試。。。。。。。。。。
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ wu-pull ---
[INFO] Building jar: D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\wu-util-pull.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ wu-pull ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.689 s
[INFO] Finished at: 2019-01-10T13:54:21+08:00
[INFO] ------------------------------------------------------------------------

2.4 mvn install

 安裝,這裏的安裝是指將剛剛生成的模塊jar包安裝到本地的maven倉庫(可能安裝這個詞比較彆扭,使用“放到”或許更適合一點),具體倉庫中安裝路徑(文件組織方式)在pom文件中指定。下面是安裝過程中的日誌:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.glodon:wu-pull >-------------------------
[INFO] Building wu-pull 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ wu-pull ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] >>> maven-source-plugin:2.2.1:jar (default) > generate-sources @ wu-pull >>>
[INFO]
[INFO] <<< maven-source-plugin:2.2.1:jar (default) < generate-sources @ wu-pull <<<
[INFO]
[INFO]
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ wu-pull ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ wu-pull ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ wu-pull ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ wu-pull ---
[INFO] Surefire report directory: D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.goujianwu.service.impl.DownloadServiceImplTest
測試。。。。。。。。。。
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.054 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ wu-pull ---
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ wu-pull ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ wu-pull ---
[INFO] Installing D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\wu-util-pull.jar to D:\Program Files\Maven\maven_repository\com\glodon\wu-pull\1.0-SNAPSHOT\wu-pull-1.0-SNAPSHOT.jar
[INFO] Installing D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\pom.xml to D:\Program Files\Maven\maven_repository\com\glodon\wu-pull\1.0-SNAPSHOT\wu-pull-1.0-SNAPSHOT.pom
[INFO] Installing D:\Program Files\JetBrains\IDEAProjects\wu-util\wu-pull\target\wu-util-pull-sources.jar to D:\Program Files\Maven\maven_repository\com\glodon\wu-pull\1.0-SNAPSHOT\wu-pull-1.0-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.381 s
[INFO] Finished at: 2019-01-10T14:04:38+08:00
[INFO] ------------------------------------------------------------------------

【注】

上述的流程已經包含了maven項目的一般流程:編譯(mvn compile)–>測試(mvn test)–>打包(mvn package)–>安裝(mvn install)。

2.5 mvn clean

 清理,這命令會移除模塊中${basedir}目錄下的target文件夾(即清理上次maven運行遺留的內容)。

正常部署應用時,建議在模塊目錄下使用mvn clean -DskipTests package(單模塊應用),如果項目是多模塊應用且模塊間有依賴關係,直接在項目目錄下執行mvn clean -DskipTests install

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章