Jenkins配置Maven 3.9.12

1. 背景

Jenkins升级了版本,Maven也升级了版本,重新配置Jenkins服务器的Maven,支持访问公有仓库和私有Nexus仓库。

2. 参考

官方文档:Apache Maven

3. 环境

  • 操作系统 Ubuntu24.04
  • 主服务器配置 VM 16C/64G/800G
  • Jenkins版本 2.528.3 LTS
  • Maven 3.9.12
  • Nexus 3.87.1

4. 安装

4.1 下载

官方文档:Downloading Apache Maven and Maven Daemon

bash 复制代码
cd /public/software/
wget https://dlcdn.apache.org/maven/maven-3/3.9.12/binaries/apache-maven-3.9.12-bin.tar.gz

4.2 安装

bash 复制代码
tar -zxvf apache-maven-3.9.12-bin.tar.gz
mv apache-maven-3.9.12 /public/

5. 配置

备份settings.xml

bash 复制代码
cd /public/apache-maven-3.9.12/conf/
cp settings.xml settings.xml.orig

编辑settings.xml

bash 复制代码
vim /public/apache-maven-3.9.12/conf/settings.xml
  1. 增加本地仓库
xml 复制代码
  <!-- 本地仓库路径   -->
  <localRepository>/public/repository</localRepository>
  1. 定义访问私有仓库账号密码
xml 复制代码
<!-- 连接私有仓库的账号密码   -->
<server>
    <id>xk-maven</id> <!-- 当定义了私有仓库的mirror时,这个id需要与mirror的id保持一致 -->
    <username>admin</username>
    <password>yourpassword</password> <!--这个密码就是你设置的密码-->
</server>
  1. 定义访问公有镜像仓库
xml 复制代码
<!-- 1. 更全面的阿里云镜像组(推荐) -->
<!-- 旧地址可以用,但不推荐
<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
-->

<mirror>
  <id>ali-maven</id>
  <name>aliyun maven</name>
  <url>https://maven.aliyun.com/repository/public</url>
  <mirrorOf>central</mirrorOf>
</mirror>

<!-- 腾讯镜像 -->
<mirror>
  <id>tencent-maven</id>
  <name>tencent maven</name>
  <url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
  <mirrorOf>central</mirrorOf>
</mirror>

<!-- 华为镜像 -->
<mirror>
  <id>huawei-maven</id>
  <name>huawei maven</name>
  <url>https://repo.huaweicloud.com/repository/maven/</url>
  <mirrorOf>central</mirrorOf>
</mirror>
  1. 定义访问私有Nexus仓库镜像
  • 定义私有仓库镜像的作用在于,允许使用http协议访问(maven 3.9+版本强制要求使用https协议访问)
  • 私有仓库镜像的定义必须放在"阻止器前",同时阻止器放开私有仓库镜像
  • 定义了私有仓库镜像(mirror)时,私有仓库账号和密码定义的id,需要与mirror的id保持一致
xml 复制代码
    <!-- 2. 私有仓库镜像(必须放在阻止器前面) -->
    <mirror>
      <id>xk-maven</id>  <!-- 需要与私有仓库账号密码中定义的id保持一致 -->
      <name>xk maven</name>
      <url>http://192.168.15.16:8081/repository/public/</url>
      <mirrorOf>xk-maven-repo</mirrorOf>  <!-- 只镜像特定私有仓库,值需要与私有仓库id保持一致 -->
    </mirror>

    <!-- 阻止器允许私有仓库使用http协议访问 -->
    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*,!xk-maven</mirrorOf>   <!-- 放开xk-maven镜像名对应的私有仓库 -->
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
  1. 定义私有仓库和属性
xml 复制代码
<profile>
   <!-- 定义配置id -->
   <id>default</id>

   <!-- 私有仓库设置 -->
   <repositories>
     <repository>
        <id>xk-maven-repo</id>  <!--这个ID需要与mirrorOf中定义一致 -->
        <name>xk repository</name>
        <url>http://192.168.15.16:8081/repository/public/</url>
        <releases>
          <enabled>true</enabled>
          <updatePolicy>always</updatePolicy>
        </releases>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>always</updatePolicy>
        </snapshots>
     </repository>
   </repositories>

  <!-- 属性设置 -->
  <properties>
      <!-- Maven 3.9+ 需要设置以下属性以允许HTTP -->
      <maven.wagon.http.ssl.insecure>true</maven.wagon.http.ssl.insecure>
      <maven.wagon.http.ssl.allowall>true</maven.wagon.http.ssl.allowall>
      <maven.wagon.http.ssl.ignore.validity.dates>true</maven.wagon.http.ssl.ignore.validity.dates>

      <!-- 强制使用基础认证 -->
      <maven.wagon.http.auth.preemptive>true</maven.wagon.http.auth.preemptive>
  </properties>
  
</profile>
  1. 定义访问私有仓库账号密码
xml 复制代码
  <!-- 激活配置 -->
  <activeProfiles>
    <activeProfile>default</activeProfile>
  </activeProfiles>
  1. 最终配置文件
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<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">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <!-- 本地仓库路径   -->
  <localRepository>/public/repository</localRepository>

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- TODO Since when can proxies be selected as depicted? -->
  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>xk-nexus</id>
      <username>admin</username>
      <password>yourpassword</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->

    <!-- 连接私有仓库的账号密码   -->
    <server>
        <id>xk-maven</id>
        <username>admin</username>
        <password>yourpassword</password><!--这个密码就是你设置的密码-->
    </server>

  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
    -->

    <!-- 1. 更全面的阿里云镜像组(推荐) -->
    <!-- 旧地址可以用,但不推荐
    <mirror>
        <id>alimaven</id>
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
    -->
    <mirror>
      <id>ali-maven</id>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/public</url>
      <mirrorOf>central</mirrorOf>
    </mirror>

    <mirror>
      <id>tencent-maven</id>
      <name>tencent maven</name>
      <url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
    
    <mirror>
      <id>huawei-maven</id>
      <name>huawei maven</name>
      <url>https://repo.huaweicloud.com/repository/maven/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>

    <!-- 2. 私有仓库镜像(必须放在阻止器前面) -->
    <mirror>
      <id>xk-maven</id>  <!-- 需要与私有仓库账号密码中定义的id保持一致 -->
      <name>xk maven</name>
      <url>http://192.168.15.16:8081/repository/public/</url>
      <mirrorOf>xk-maven-repo</mirrorOf>  <!-- 只镜像特定私有仓库,值需要与私有仓库id保持一致 -->
    </mirror>

    <!-- 阻止器允许私有仓库使用http协议访问 -->
    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*,!xk-maven</mirrorOf>   <!-- 放开xk-maven镜像名对应的私有仓库 -->
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <profile>
       <id>default</id>

       <!-- 私有仓库设置 -->
       <repositories>
         <repository>
            <id>xk-maven-repo</id>  <!--这个ID需要与mirrorOf定义一致 -->
            <name>xk repository</name>
            <url>http://192.168.15.16:8081/repository/public/</url>
            <releases>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
            </snapshots>
         </repository>
       </repositories>



      <!-- 属性设置 -->
      <properties>
          <!-- Maven 3.9+ 需要设置以下属性以允许HTTP -->
          <maven.wagon.http.ssl.insecure>true</maven.wagon.http.ssl.insecure>
          <maven.wagon.http.ssl.allowall>true</maven.wagon.http.ssl.allowall>
          <maven.wagon.http.ssl.ignore.validity.dates>true</maven.wagon.http.ssl.ignore.validity.dates>

          <!-- 强制使用基础认证 -->
          <maven.wagon.http.auth.preemptive>true</maven.wagon.http.auth.preemptive>
      </properties>

    <!--
     | Here is another profile, activated by the property 'target-env' with a value of 'dev', which
     | provides a specific path to the Tomcat instance. To use this, your plugin configuration might
     | hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
    </profile>
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->

  <!-- 激活配置 -->
  <activeProfiles>
    <activeProfile>default</activeProfile>
  </activeProfiles>

</settings>

6. 测试

6.1 命令验证仓库访问

  1. curl或get命令请求私有仓库测试
  • 仓库没有开启验证,允许匿名访问
  • 请使用有效私有仓库路径测试
bash 复制代码
# 测试命令示例
curl -O http://192.168.15.16:8081/repository/public/com/xk/xk-data-impl-single-index-v2/2.0/xk-data-impl-single-index-v2-2.0.jar
# 命令输出结果
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 3627k  100 3627k    0     0  22.2M      0 --:--:-- --:--:-- --:--:-- 22.4M

6.2 直接指向私有仓库测试

  1. 创建临时存储目录
bash 复制代码
mkdir -p /public/software
bash 复制代码
/public/apache-maven-3.9.12/bin/mvn dependency:copy -Dartifact=com.xk:xk-data-impl-single-index-v2:2.0:jar -DrepositoryUrl=http://192.168.15.16:8081/repository/public -DoutputDirectory=/public/software -Dtransitive=false
bash 复制代码
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- dependency:3.7.0:copy (default-cli) @ standalone-pom ---
[INFO] Configured Artifact: com.xk:xk-data-impl-single-index-v2:2.0:jar
[INFO] Copying artifact 'com.xk:xk-data-impl-single-index-v2:jar:2.0' (/public/repository/com/xk/xk-data-impl-single-index-v2/2.0/xk-data-impl-single-index-v2-2.0.jar) to /public/software/xk-data-impl-single-index-v2-2.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.969 s
[INFO] Finished at: 2025-12-30T09:36:11Z
[INFO] ------------------------------------------------------------------------

6.3 使用maven 配置文件(settings.xml)测试

bash 复制代码
/public/repository/com/xk/xk-data-impl-single-index-v2# /public/apache-maven-3.9.12/bin/mvn dependency:get   -Dartifact=com.xk:xk-data-impl-single-index-v2:2.0  -DremoteRepositories=xk-maven  -Dtransitive=false  -e
bash 复制代码
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- dependency:3.7.0:get (default-cli) @ standalone-pom ---
[INFO] Resolving com.xk:xk-data-impl-single-index-v2:jar:2.0
Downloading from xk-maven: http://192.168.15.16:8081/repository/public/com/xk/xk-data-impl-single-index-v2/2.0/xk-data-impl-single-index-v2-2.0.pom
Downloaded from xk-maven: http://192.168.15.16:8081/repository/public/com/xk/xk-data-impl-single-index-v2/2.0/xk-data-impl-single-index-v2-2.0.pom (9.4 kB at 93 kB/s)
Downloading from xk-maven: http://192.168.15.16:8081/repository/public/com/xk/xk-data-impl-single-index-v2/2.0/xk-data-impl-single-index-v2-2.0.jar
Downloaded from xk-maven: http://192.168.15.16:8081/repository/public/com/xk/xk-data-impl-single-index-v2/2.0/xk-data-impl-single-index-v2-2.0.jar (3.7 MB at 42 MB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.320 s
[INFO] Finished at: 2025-12-30T09:41:24Z
[INFO] ------------------------------------------------------------------------

7. Jenkins示例

7.1 SHELL命令直接调用

  1. 配置示例项目
  2. 构建控制台输出
相关推荐
人鱼传说10 分钟前
docker desktop是一个好东西
运维·docker·容器
阿梦Anmory44 分钟前
Ubuntu配置代理最详细教程
linux·运维·ubuntu
呉師傅1 小时前
【使用技巧】Adobe Photoshop 2024调整缩放与布局125%后出现点菜单项漂移问题的简单处理
运维·服务器·windows·adobe·电脑·photoshop
heartbeat..1 小时前
JVM 性能调优流程实战:从开发规范到生产应急排查
java·运维·jvm·性能优化·设计规范
小Tomkk1 小时前
数据库 变更和版本控制管理工具 --Bytebase 安装部署(linux 安装篇)
linux·运维·数据库·ci/cd·bytebase
赌博羊1 小时前
ImportError: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32‘ not found
linux·运维·gnu
消失的旧时光-19432 小时前
Linux 入门核心命令清单(工程版)
linux·运维·服务器
艾莉丝努力练剑2 小时前
【Linux:文件】Ext系列文件系统(初阶)
大数据·linux·运维·服务器·c++·人工智能·算法
小天源2 小时前
Cacti在Debian/Ubuntu中安装及其使用
运维·ubuntu·debian·cacti
Trouvaille ~2 小时前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket