Maven 配置与命令

本地仓库位置

# 本地仓库位置
export MAVEN_OPTS="-Dmaven.repo.local=/software/apache-maven-repository"

忽略 SSL 证书验证

# 忽略 SSL 证书验证
mvn clean package -Dmaven.wagon.http.ssl.insecure=true

忽略 Tests 失败

# 忽略 Tests 失败
mvn clean package -Dmaven.test.failure.ignore=true

versions 命令

# 设置项目的新版本(会产生 pom.xml.versionsBackup 备份文件)
mvn versions:set -DnewVersion=2025.1.0.1-SNAPSHOT
# 移除所有 pom.xml.versionsBackup 备份文件,确认版本修改
mvn versions:commit
# 回滚到版本修改之前的状态(恢复备份文件)
mvn versions:revert
# 更新父项目版本
mvn versions:update-parent
# 更新 pom.xml 中 <properties> 里定义的版本号
mvn versions:update-properties
# 显示哪些依赖有可用的新版本
mvn versions:display-dependency-updates
# 显示哪些插件有可用的新版本
mvn versions:display-plugin-updates
# 将所有依赖替换为最新的 release 版本
mvn versions:use-latest-releases
# 将所有依赖替换为最新的 SNAPSHOT 版本
mvn versions:use-latest-snapshots

命令

# 运行测试类中的方法
mvn clean test -Dtest=cn.com.xuxiaowei.DataApplicationTests#contextLoads
# 下载源码
mvn clean -U package dependency:sources -DskipTests=true
# 下载文档
mvn clean -U package dependency:resolve -Dclassifier=javadoc -DskipTests=true
# 依赖分析
mvn dependency:analyze
# 依赖树
mvn dependency:tree
# 插件依赖分析
mvn dependency:resolve-plugins
# 查看当前激活的 profile
mvn help:active-profiles
# Maven 项目模块数:使用日志文件计算(必须启用 `module`,必须支持运行 `mvn` 命令,项目 `pom.xml` 必须正常)
mvn clean | grep -E '^\[INFO\] Building' | wc -l
# Maven 项目模块数:使用 pom.xml 文件计算
echo $(find . -name "pom.xml" | wc -l)
# 刪除快照
find /Applications/apache-maven-repo -type d -name '*-SNAPSHOT' -print -exec rm -r {} +

本地安装 jar 文件

mvn install:install-file \
  -Dfile=path/to/app-0.0.1.jar \
  -DgroupId=cn.com.xuxiaowei \ 
  -DartifactId=app \
  -Dversion=0.0.1 \
  -Dpackaging=jar \
  -DgeneratePom=true \
  -Dsources=path/to/sources.jar \
  -Djavadoc=path/to/javadoc.jar

上传 jar 文件

# 上传 jar
# -Dfile:指定 文件位置
# -Dfiles:指定 多个文件位置,使用英文 逗号 分隔,要与 -Dclassifiers、-Dtypes 的 数量、顺序 一致
# -DgroupId:指定 groupId
# -DartifactId:指定 artifactId
# -Dversion:指定 version
# -Dpackaging:指定 文件类型,可选值:jar、war、pom、rar、jar.asc、pom.asc 等
# -DrepositoryId:指定 仓库 ID(与 settings.xml 文件中 server id 相同)
# -Durl:指定 仓库 URL
# -Dclassifier:指定 分类器,可选值:空、sources、javadoc 等
# -Dclassifiers:指定 多个分类器,使用英文 逗号 分隔,要与 -Dfiles、-Dtypes 的 数量、顺序 一致,可为空
# -Dtypes:指定 多个文件后缀名,使用英文 逗号 分隔,要与 -Dclassifiers、-Dfiles 的 数量、顺序 一致
# -DpomFile:指定 pom.xml 文件位置,如果配置了此属性,可忽略 -DgroupId、-DartifactId、-Dversion
# -DgeneratePom:是否生成 pom,可选值:true、false
#
# 上传结果的文件名:
# 1. 当仅配置了 -DartifactId、-Dversion、-Dpackaging 时:$
# 2. 不存在 classifiers 时:$
# 3. 存在 classifiers 时:$
mvn deploy:deploy-file \
	-Dfile=/tmp/abc-0.0.1-SNAPSHOT.jar \
	-DgroupId=cn.com.xuxiaowei \
    -DartifactId=abc \
    -Dversion=0.0.1-SNAPSHOT \
    -Dpackaging=jar \
    -DrepositoryId=rdc-snapshots \
    -Durl=https://packages.aliyun.com/maven/repository/xuxiaowei-snapshot-Jltnfj/
# 一次上传多个文件
# 如果要上传的文件如下:
# ├── pom.xml
# └── target
#     ├── SF-CSIM-EXPRESS-SDK-V2.1.7.jar
#     ├── SF-CSIM-EXPRESS-SDK-V2.1.7.jar.asc
#     ├── SF-CSIM-EXPRESS-SDK-V2.1.7-javadoc.jar
#     ├── SF-CSIM-EXPRESS-SDK-V2.1.7-javadoc.jar.asc
#     ├── SF-CSIM-EXPRESS-SDK-V2.1.7.pom
#     ├── SF-CSIM-EXPRESS-SDK-V2.1.7.pom.asc
#     ├── SF-CSIM-EXPRESS-SDK-V2.1.7-sources.jar
#     └── SF-CSIM-EXPRESS-SDK-V2.1.7-sources.jar.asc
mvn deploy:deploy-file \
    -DpomFile=pom.xml \
    -Dfile=target/SF-CSIM-EXPRESS-SDK-V2.1.7.jar \
    -Dfiles=target/SF-CSIM-EXPRESS-SDK-V2.1.7.pom.asc,target/SF-CSIM-EXPRESS-SDK-V2.1.7.jar.asc,target/SF-CSIM-EXPRESS-SDK-V2.1.7-javadoc.jar,target/SF-CSIM-EXPRESS-SDK-V2.1.7-javadoc.jar.asc,target/SF-CSIM-EXPRESS-SDK-V2.1.7-sources.jar,target/SF-CSIM-EXPRESS-SDK-V2.1.7-sources.jar.asc \
    -Dclassifiers=,,javadoc,javadoc,sources,sources \
    -Dtypes=pom.asc,jar.asc,jar,jar.asc,jar,jar.asc \
    -DrepositoryId=ossrh \
    -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/

使用 Maven 编译 Node 项目

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.com.xuxiaowei</groupId>
  <artifactId>xuxiaowei-com-cn</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <frontend-maven-plugin.version>1.15.1</frontend-maven-plugin.version>
    <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>

    <node.version>v20.18.0</node.version>
    <npm.version>10.8.2</npm.version>

    <!-- https://mirrors.aliyun.com/nodejs-release/v20.18.0/win-x64/node.exe -->
    <node-download-root>https://mirrors.aliyun.com/nodejs-release/</node-download-root>
    <!-- https://registry.npmmirror.com/npm/-/npm-10.8.1.tgz -->
    <npm-download-root>https://registry.npmmirror.com/npm/-/</npm-download-root>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>${frontend-maven-plugin.version}</version>
        <executions>
          <execution>
            <id>install node and npm</id>
            <goals>
              <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
              <nodeVersion>${node.version}</nodeVersion>
              <npmVersion>${npm.version}</npmVersion>
              <nodeDownloadRoot>${node-download-root}</nodeDownloadRoot>
              <npmDownloadRoot>${npm-download-root}</npmDownloadRoot>
            </configuration>
          </execution>
          <execution>
            <id>npm install</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <arguments>install --registry https://registry.npmmirror.com</arguments>
            </configuration>
          </execution>
          <execution>
            <id>npm build</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>run docs:build</arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>${maven-resources-plugin.version}</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.basedir}/classes/static</outputDirectory>
              <resources>
                <resource>
                  <directory>${project.basedir}/.vitepress/dist</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.com.xuxiaowei</groupId>
    <artifactId>xuxiaowei-com-cn</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <frontend-maven-plugin.version>1.15.1</frontend-maven-plugin.version>
        <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>

        <node.version>v20.18.0</node.version>
        <pnpm.version>9.12.2</pnpm.version>

        <!-- https://mirrors.aliyun.com/nodejs-release/v20.18.0/win-x64/node.exe -->
        <node-download-root>https://mirrors.aliyun.com/nodejs-release/</node-download-root>
        <!-- https://registry.npmmirror.com/pnpm/-/pnpm-9.12.2.tgz -->
        <pnpm-download-root>https://registry.npmmirror.com/pnpm/-/</pnpm-download-root>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>$</version>
                <executions>
                    <execution>
                        <id>install node and pnpm</id>
                        <goals>
                            <goal>install-node-and-pnpm</goal>
                        </goals>
                        <configuration>
                            <nodeVersion>$</nodeVersion>
                            <pnpmVersion>$</pnpmVersion>
                            <nodeDownloadRoot>$</nodeDownloadRoot>
                            <pnpmDownloadRoot>$</pnpmDownloadRoot>
                        </configuration>
                    </execution>
                    <execution>
                        <id>pnpm install</id>
                        <goals>
                            <goal>pnpm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <arguments>install --registry https://registry.npmmirror.com</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>pnpm build</id>
                        <goals>
                            <goal>pnpm</goal>
                        </goals>
                        <configuration>
                            <arguments>run docs:build</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>$</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>$/classes/static</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>$/.vitepress/dist</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.com.xuxiaowei</groupId>
    <artifactId>xuxiaowei-com-cn</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <frontend-maven-plugin.version>1.15.1</frontend-maven-plugin.version>
        <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>

        <node.version>v20.18.0</node.version>
        <yarn.version>v1.22.22</yarn.version>

        <!-- https://mirrors.aliyun.com/nodejs-release/v20.18.0/win-x64/node.exe -->
        <node-download-root>https://mirrors.aliyun.com/nodejs-release/</node-download-root>
        <!-- https://mirrors.huaweicloud.com/repository/toolkit/yarn/v1.22.22/yarn-v1.22.22.tar.gz -->
        <yarn-download-root>https://mirrors.huaweicloud.com/repository/toolkit/yarn/</yarn-download-root>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>$</version>
                <executions>
                    <execution>
                        <id>install node and yarn</id>
                        <goals>
                            <goal>install-node-and-yarn</goal>
                        </goals>
                        <configuration>
                            <nodeVersion>$</nodeVersion>
                            <yarnVersion>$</yarnVersion>
                            <nodeDownloadRoot>$</nodeDownloadRoot>
                            <yarnDownloadRoot>$</yarnDownloadRoot>
                        </configuration>
                    </execution>
                    <execution>
                        <id>yarn install</id>
                        <goals>
                            <goal>yarn</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <arguments>install --registry https://registry.npmmirror.com</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>yarn build</id>
                        <goals>
                            <goal>yarn</goal>
                        </goals>
                        <configuration>
                            <arguments>run docs:build</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>$</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>$/classes/static</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>$/.vitepress/dist</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Maven settings.xml 文件 mirrors 节点 配置

  • 如果要使用 阿里云、腾讯云、华为云 等等国内厂商的 Maven 代理仓库,可以配置

    1. <id>aliyun</id>:必须唯一
    2. <mirrorOf>central,jcenter</mirrorOf>:代理仓库名称,支持使用 * 代理所有,常见的仓库名见:仓库服务
    3. <url>https://maven.aliyun.com/repository/public</url>:代理国内云厂商 Maven 代理地址,更多 Maven 地址见:Maven 仓库地址
    <mirrors>
      <mirror>
        <id>aliyun</id>
        <mirrorOf>central,jcenter</mirrorOf>
        <url>https://maven.aliyun.com/repository/public</url>
      </mirror>
    </mirrors>
    
  • 如果 Maven 项目中 pom.xml 文件使用了 <repositories>,可以在 settings.xml 文件 mirrors 节点添加与项目 pom.xml<repository><id>***</id> 相同的名字来覆盖 pom.xml<repository>,示例如下:

    1. GitHub - xuxiaowei-com-cn/spring-cloud-alibaba-example: Spring Cloud Alibaba 示例 · GitHub 仓库的 spring-cloud-2025.1.x 模块配置了:

      
      <repositories>
        <!-- 需要在 settings.xml 配置:
         <server>
           <id>github</id>
           <username>GitHub 用户名</username>
           <password>GitHub Token(需要 read:packages 权限)</password>
         </server>
         -->
        <repository>
          <id>github</id>
          <url>https://maven.pkg.github.com/alibaba/spring-cloud-alibaba</url>
          <releases>
            <enabled>false</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      
    2. 因为 GitHub Maven Package 无法匿名访问,需要在 settings.xml 文件中配置 GitHub Token(需要 read:packages 权限)

    3. 不想再工作电脑中配置个人 GitHub Token

    4. 作者自己搭建了一个 Nexus 服务,代理了 https://maven.pkg.github.com/alibaba/spring-cloud-alibaba ,地址:http://nexus.xuxiaowei.io:48081/repository/maven-github-spring-cloud-alibaba/

    5. 于是便可以在 settings.xml 中配置 mirrors,内容如下:

      <mirrors>
          <mirror>
              <id>aliyun</id>
              <mirrorOf>central,jcenter</mirrorOf>
              <url>https://maven.aliyun.com/repository/public</url>
          </mirror>
      
          <mirror>
              <!-- 当前文件唯一即可 -->
              <id>maven-github-spring-cloud-alibaba</id>
              <!-- 用于覆盖项目 pom.xml 中 <repository> 节点下同名的 <id> -->
              <mirrorOf>github</mirrorOf>
              <url>http://nexus.xuxiaowei.io:48081/repository/maven-github-spring-cloud-alibaba/</url>
          </mirror>
      
      </mirrors>
      
1 个赞