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

posted on 2021-06-19 17:48  肥兔子爱豆畜子  阅读(4177)  评论(0编辑  收藏  举报

导航