Maven懶人包(觀念)

傑克
發佈於MAVEN
2022/07/29閱讀時間約 12 分鐘

前言

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of infomation.
Apache Maven是一種軟體專案管理和解析工具。根據專案物件模型模型(POM)的概念,Maven可以管理專案從構建到報告和文件。
有時候我很難從原文網站去理解、了解一件事情,因為他們廢話太多了
直接進入主題

觀念

Maven Lifecycle and phase and goal

Maven的生命週期Lifecycle只有三個
Clean → Default → Site
每個生命週期都有各自步驟Phase
如果只想要執行某一段步驟Phase,你必須指定goal
Clean
    pre-clean  清理之前
    clean  清理
    post-clean 清理之後
Default
    validate 驗證所有的項目是否正確
    initialize 初始化,例如設置屬性或創建目錄
    generate-sources 產生sourceCode
    process-sources    處理sourceCode 過濾一些資料
    generate-resources 產生package資源檔案
    process-resources 複製資源檔案放入package的目錄中
    compile 開始編譯
    process-classes 產生 bytecode,例如class
    generate-test-sources 產生testCase code
    process-test-sources 處理testCase sourceCode 過濾一些資料
    generate-test-resources 產生 testCase package資源檔案
    process-test-resources 複製資源檔案放入測試的目錄中
    test-compile 開始testCase編譯
    process-test-classes 產生 testcase bytecode,例如class
    test 開始測試,這些東西不該被打包
    prepare-package    開始打包
    package 根據參數決定打包為war或是jar
    pre-integration-test 準備做整合測試,如整合測試所需的環境。
    integration-test 整合測試,如有必要 deploy the package to test
    post-integration-test 清潔環境
    verify 驗證package完整性以及品質
    install 安裝到本地的repository
    deploy 安裝到release目錄
Site
    pre-site
    site
    post-site
    site-deploy

執行maven的示範

mvn org.apache.maven.plugins:maven-clean-plugin:2.5:clean
在第一個部分 org.apache.maven.plugins 代表 gorupId,
在第二個部分 maven-clean-plugin 代表 artifactId,
在第三個部分 2.5 代表 version,
在第四個部分 clean 代表 Goal。(goal出現了!)
ex: mvn org.apache.maven.plugins:maven-clean-plugin:2.5:clean
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< tw.com.jack:SpringBootDemo >---------------------
[INFO] Building SpringBootDemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-cli) @ SpringBootDemo ---
[INFO] Deleting /home/jackchen/download/SpringBootDemo/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.646 s
[INFO] Finished at: 2022-07-29T15:21:46+08:00
[INFO] ------------------------------------------------------------------------
當然可以簡化成這樣,一目了然
mvn clean
以下我以簡化方式表達maven的用途

ex 0: 想要掠過testCase

mvn clean package -DskipTests

ex 1:  清除跟編譯JAR檔,jar 檔放在/home/xxxx/download/SpringBootDemo/target

mvn clean package

ex2:  清除跟編譯JAR檔跟執行,跟intelliJ run application 有同等效果

mvn clean package spring-boot:run
....
.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::                (v2.7.2)
......
[  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
.....
^C[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  44.548 s
[INFO] Finished at: 2022-07-29T15:31:10+08:00
[INFO] ------------------------------------------------------------------------

ex3: 透過maven 上版到測試機,一定會出錯,因為我沒有在pom.xml 指定deploy的對象

mvn site:deploy

ex4: 透過maven 安裝到本機

/home/xxxx/.m2/repository/tw/com/jack/SpringBootDemo/0.0.1-SNAPSHOT/SpringBootDemo-0.0.1-SNAPSHOT.jar
mvn install

ex5: 透過maven 安裝到nexus

這部份細節流到nexus篇再說
mvn deploy:deploy-file
-Durl="https:///repository/privatehosted"
-DgroupId=tw.com.jack
-DartifactId=SpringBootDemo
-Dversion=1.7
-Dpackaging=jar
-DgeneratePom=true
-DrepositoryId=
#先在Setting指定帳號密碼
-s settings.xml -DUSERNAME_VAR=JACK -DPASSWORD_VAR=SuperJack

ex6: 不想要每次都線上下載dependency jar

mvn -o <指令>

ex7: 分析專案dependency jar 有哪些依存性

mvn dependency:tree

ex8: 檢查Repositories清單

mvn dependency:list-repositories

ex9: 檢查maven執行時的settings參數

mvn help:effective-settings

ex10: 檢查maven執行時的系統變數

mvn help:system

ex11: 啟動SpringBoot並特別指定SpringBoot yml參數

mvn spring-boot:run –Dspring-boot.run.jvmArguments="-Dserver.port=9092"

ex12: 尋找最新的專案依存Jar的版次並更新版次

mvn versions:update-child-modules

ex13: 指定profile

mvn -P<profileName>

後述

透過指令的敘述,可以清楚maven在做什麼,就像前言所述,編譯、打包、產生報告、上測試機、或是更細部的操作例如set version、放上git Server、分析等…下次我們開始從pom.xml 跟Setting.xml 介紹,讓大家更了解maven

傑克
傑克
大家好,我是傑克,軟體工程師一枚,基本上只要鍵盤的三個按鍵沒壞,沒有什麼問題可以難倒工程師
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容