Maven Wrapper的使用


Maven Wrapper是apache maven借鑒了gradle wrapper之后增加了的功能,可以為每個具體的項目添加一個專用的、指定版本的maven供其使用。
takari官方說maven wrapper將會整合到apache maven 4.0里邊,現在maven最新版本是3.8,等待好消息。

安裝

安裝方式,在maven項目的根目錄下,mvn -N io.takari:maven:0.7.7:wrapper,就可以安裝maven wrapper了。
使用方法跟maven一樣,./mvnw clean install,windows下mvnw.cmd clean install ,就可以安裝本項目到本地maven庫。
或者還有常用的 mvnw.cmd clean package, 使用maven打包。

詳細模式

將MVNW_VERBOSE環境變量設置為true開啟詳細模式

mvnw的工作原理

mvnw這個工具的主體是.mvn/wrapper/maven-wrapper.jar這個jar包和對應的shell腳本mvnw或者mvnw.cmd,使用的時候要添加到使用的項目的根目錄里,jar包用來在shell腳本的引導下、下載和調用maven ,如果發現這個jar包不可用,將從.mvn/wrapper/maven-wrapper.properties里指定的wrapperUrl下載這個jar包,下載是通過curl, wget,沒這倆的話就編譯./mvn/wrapper/MavenWrapperDownloader.java來生成class當作下載工具。wrapper下載的maven會默認保存在~.m2\wrapper\dists\apache-maven-xxx目錄下的。

指定使用maven版本

為當前項目安裝wrapper的時候就可以指定,

mvn -N io.takari:maven:0.7.7:wrapper -Dmaven=3.5.4  #指定使用maven3.5.4

或者如果已經安裝了maven wrapper之后,在maven-wrapper.properties文件里,修改

distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip

這樣也可以。

要從需要基本身份驗證的位置下載 Maven,有兩個方式:

  • 設置環境變量 MVNW_USERNAME 和 MVNW_PASSWORD

  • 將用戶和密碼添加到 distributionUrl 中,如下所示: distributionUrl=https://username:password@ /maven2/org/apache/maven/apache-maven/3.2.1/apache-maven-3.2.1-bin.zip

為wrapper指定本地的maven安裝目錄和本地maven庫

  1. 指定本地maven目錄,我們打開mvnw腳本文件,可以看到如下注釋,
Maven Start Up Batch script

Required ENV vars:
JAVA_HOME - location of a JDK home dir

Optional ENV vars
M2_HOME - location of maven2's installed home dir
MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
MAVEN_OPTS - parameters passed to the Java VM when running Maven
    e.g. to debug Maven itself, use
set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
MAVEN_SKIP_RC - flag to disable loading of mavenrc files

也就是說,設置M2_HOME可以指定本地的maven安裝目錄、相當於MAVEN_HOME 。
2. 為wrapper指定本地maven庫
https://github.com/takari/maven-wrapper#specifying-maven-distribution-base-path 中指出可以設置MAVEN_USER_HOME指定本地maven庫路徑。

使用Maven Repository Manager

使用內部的Maven repository manager有兩種方式:

  1. 直接在maven-wrapper.properties文件中設置wrapperUrl以及wrapperUrl
  2. 使用環境變量MVNW_REPOURL,比如MVNW_REPOURL=https://repo.example.com/central-repo-proxy
    如果在通過takari-maven-plugin插件安裝wrapper的時候設置了MVNW_REPOURL環境變量,那么就會寫入到maven-wrapper.properties文件了。
    如果沒設置,但在settings.xml配置了mirror URL,那么mirror也會生效。
Leave the default URL in the project pointing to Maven Central 
and set the environment variable MVNW_REPOURL to your repo manager URL such as https://repo.example.com/central-repo-proxy.
If MVNW_REPOURL is set during the wrapper installation with the takari-maven-plugin, 
the URL is used in the maven-wrapper.properties file.

If not set, but your mirror URL in your settings.xml is configured, it will be used.

優化

通過maven wrapper下載的maven是在~.m2\wrapper\dists\apache-maven-3.6.3-bin\1iopthnavndlasol9gbrbg6bf2\apache-maven-3.6.3
然后筆者修改了這個maven的settings.xml里邊的本地maven庫設置和aliMaven鏡像之后,再執行mvnw clean package構建項目就會快非常多了,從半個多小時縮短到5分鍾。
當然也可以使用設置環境變量的方法

export MVNW_VERBOSE=true
export MVNW_REPOURL=https://maven.aliyun.com/repository/public


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM