目录

maven-wrapper的使用

maven wrapper的使用

写在前面

考虑这样的场景,张三创建了一个maven项目使用了3.9版本,当李四下载下来去开发配置的却是3.6版本,此时李四就不得不再去配置一个3.9版本的maven,为了解决这个问题,maven引入了maven wrapper的机制(借鉴 (抄) 的gradle),本文就一起来看下。

1:正文

首先我们需要来创建一个maven项目:

https://i-blog.csdnimg.cn/direct/a549f0d980144e3fbd60af0c2dd29853.png

因为maven wrapper需要通过你本地的maven来生成,所以首先确保你本地安装了maven,执行命令 mvn wrapper:wrapper

D:\tmp\untitledwrapper> mvn wrapper:wrapper
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< org.example:untitledwrapper >---------------------
[INFO] Building untitledwrapper 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-wrapper-plugin:3.3.2:wrapper (default-cli) @ untitledwrapper ---
[INFO] Unpacked only-script type wrapper distribution org.apache.maven.wrapper:maven-wrapper-distribution:zip:only-script:3.3.2
[INFO] Configuring .mvn/wrapper/maven-wrapper.properties to use Maven 3.6.3 and download from https://repo.maven.apache.org/maven2
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.171 s
[INFO] Finished at: 2025-03-12T15:26:25+08:00
[INFO] ------------------------------------------------------------------------

执行成功后就会生成.mvn目录和mvnw(linux用),mvnw.cmd(win用),如下:

https://i-blog.csdnimg.cn/direct/cb718ec8cd7848479ffbf0a19674d017.png

其中.mvn中的maven-wrapper.properties指定了maven的信息,如果你本地没有则会自动下载,所以一般第一次都会比较慢,下载后会存储到如下位置:

https://i-blog.csdnimg.cn/direct/94ddf68020f44c32bd813cbf044e3750.png

后续再执行编译打包的话就要通过mvnw来完成了,这样李四的环境就再也不会出问题了。

1.1:指定setting.xml

首先在项目中的.mvn目录创建maven.config文件,并录入内容 -s.mvn/settings.xml ,如下:

https://i-blog.csdnimg.cn/direct/23d190f0f71d485daa9d7251c16f4775.png

然后在.mvn目录创建settings.xml文件即可,如下是一个可能的内容:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
  </servers>

  <mirrors>
  </mirrors>

  <profiles>
    <profile>
      <id>myRepository1</id>
      <repositories>
        <repository>
          <id>myRepository1_1</id>
          <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
    <profile>
      <id>myRepository2</id>
      <repositories>
        <repository>
          <id>myRepository2_1</id>
          <url>https://repo.maven.apache.org/maven2/</url>
<!--

          <url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url>
-->
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>

  </profiles>
  <activeProfiles>
<!--    <activeProfile>myRepository2</activeProfile>-->
    <activeProfile>myRepository1</activeProfile>
  </activeProfiles>
</settings>

最终如下:

https://i-blog.csdnimg.cn/direct/ebaead7139254dae84836ae2c4086f63.png

写在后面

参考文章列表