官方链接
https://maven.apache.org/
解压并解压搭配/opt目录下
[root@centos7 ~]# tar -xf apache-maven-4.0.0-alpha-5-bin.tar.gz -C /opt
修改名称
[root@centos7 ~]# mv /opt/apache-maven-4.0.0-alpha-5 /opt/maven
配置环境变量
vi /etc/profile 在最后一行添加如下内容,我这里安装的jdk11的 export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/ export MAVEN_HOME=/opt/maven export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
生效系统环境变量
[root@centos7 ~]# source /etc/profi
测试是否生效
[root@centos7 ~]# mvn --version Apache Maven 4.0.0-alpha-5 (26d10a4bf9a2df75feef60da01d8706f2bf77a47) Maven home: /opt/maven Java version: 11.0.19, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64 Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"
全局工具配置关联JDK和Maven
如果这里提示JDK不存在这类的提醒只需要把JDK-devel相关的开发的包安装上即可
添加Jenkins全局变量
Manage Jenkins->Confifigure System->Global Properties ,添加三个全局变量 JAVA_HOME、M2_HOME、PATH+EXTRA
新增环境变量PATH+EXTRA
修改Maven的settings.xml
mkdir /root/repo 创建本地仓库目录 vi /opt/maven/conf/settings.xml #本地仓库改为:/opt/repo/ 找到 <!-- 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>/opt/repo</localRepository> 添加阿里云私服地址: 找到以下内容 <!-- 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> --> <mirror> <id>maven-default-http-blocker</id> <mirrorOf>external:http:*</mirrorOf> <name>Pseudo repository to mirror external repositories initially using HTTP.</name> <url>http://0.0.0.0/</url> <blocked>true</blocked> </mirror> 在下面加入以下内容,详细信息看下面的截图 <mirror> <id>aliyun-public</id> <mirrorOf>*</mirrorOf> <name>aliyun public</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> <mirror> <id>aliyun-central</id> <mirrorOf>*</mirrorOf> <name>aliyun central</name> <url>https://maven.aliyun.com/repository/central</url> </mirror> <mirror> <id>aliyun-spring</id> <mirrorOf>*</mirrorOf> <name>aliyun spring</name> <url>https://maven.aliyun.com/repository/spring</url> </mirror> <mirror> <id>aliyun-spring-plugin</id> <mirrorOf>*</mirrorOf> <name>aliyun spring-plugin</name> <url>https://maven.aliyun.com/repository/spring-plugin</url> </mirror> <mirror> <id>aliyun-apache-snapshots</id> <mirrorOf>*</mirrorOf> <name>aliyun apache-snapshots</name> <url>https://maven.aliyun.com/repository/apache-snapshots</url> </mirror> <mirror> <id>aliyun-google</id> <mirrorOf>*</mirrorOf> <name>aliyun google</name> <url>https://maven.aliyun.com/repository/google</url> </mirror> <mirror> <id>aliyun-gradle-plugin</id> <mirrorOf>*</mirrorOf> <name>aliyun gradle-plugin</name> <url>https://maven.aliyun.com/repository/gradle-plugin</url> </mirror> <mirror> <id>aliyun-jcenter</id> <mirrorOf>*</mirrorOf> <name>aliyun jcenter</name> <url>https://maven.aliyun.com/repository/jcenter</url> </mirror> <mirror> <id>aliyun-releases</id> <mirrorOf>*</mirrorOf> <name>aliyun releases</name> <url>https://maven.aliyun.com/repository/releases</url> </mirror> <mirror> <id>aliyun-snapshots</id> <mirrorOf>*</mirrorOf> <name>aliyun snapshots</name> <url>https://maven.aliyun.com/repository/snapshots</url> </mirror> <mirror> <id>aliyun-grails-core</id> <mirrorOf>*</mirrorOf> <name>aliyun grails-core</name> <url>https://maven.aliyun.com/repository/grails-core</url> </mirror> <mirror> <id>aliyun-mapr-public</id> <mirrorOf>*</mirrorOf> <name>aliyun mapr-public</name> <url>https://maven.aliyun.com/repository/mapr-public</url> </mirror> 要在这一行上面添加如上内容. </mirrors>
第一项修改截图
第二项修改截图.为了方便截图只添加一小部分内容,方便截图,一定要把所有的内容添加上
测试Maven是否配置成功
命令解释
写入以下内容,一定要此格式填写 #!/bin/bash -l mvn clean install mvn clean 执行清空操作,清空字节码和曾经构建的一些包 package 重新打包
控制台输入结果如下
Started by user itcast Running as SYSTEM Building in workspace /var/lib/jenkins/workspace/test02 The recommended git tool is: NONE using credential 304bf738-1cbb-4520-965b-ab2dc68d6ae2 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/test02/.git # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url git@10.0.0.20:zhangsan/web-demo.git # timeout=10 Fetching upstream changes from git@10.0.0.20:zhangsan/web-demo.git > git --version # timeout=10 > git --version # 'git version 1.8.3.1' using GIT_SSH to set credentials gitlab-auth-ssh Verifying host key using known hosts file > git fetch --tags --progress git@10.0.0.20:zhangsan/web-demo.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 Checking out Revision 54589616f1f071acd7006c369919c5ba3e98395b (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 54589616f1f071acd7006c369919c5ba3e98395b # timeout=10 Commit message: "第八次提交" > git rev-list --no-walk 54589616f1f071acd7006c369919c5ba3e98395b # timeout=10 [test02] $ /bin/sh -xe /tmp/jenkins4993178190035478849.sh [test02] $ /bin/bash -l /tmp/jenkins2696623148703012810.sh [INFO] Scanning for projects... [INFO] [INFO] ------------------------< com.itheima:web_demo >------------------------ [INFO] Building web_demo 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [WARNING] Version not locked for default bindings plugins [maven-clean-plugin, maven-resources-plugin, maven-compiler-plugin, maven-surefire-plugin, maven-war-plugin, maven-install-plugin], you should define versions in pluginManagement section of your pom.xml or parent [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ web_demo --- [INFO] Deleting /var/lib/jenkins/workspace/test02/target [INFO] [INFO] --- resources:3.3.0:resources (default-resources) @ web_demo --- [WARNING] File encoding has not been set, using platform encoding UTF-8. Build is platform dependent! [WARNING] See https://maven.apache.org/general.html#encoding-warning [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/test02/src/main/resources [INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/test02/src/main/resources-filtered [INFO] [INFO] --- compiler:3.10.1:compile (default-compile) @ web_demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/lib/jenkins/workspace/test02/target/classes [INFO] [INFO] --- resources:3.3.0:testResources (default-testResources) @ web_demo --- [WARNING] File encoding has not been set, using platform encoding UTF-8. Build is platform dependent! [WARNING] See https://maven.apache.org/general.html#encoding-warning [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/test02/src/test/resources [INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/test02/src/test/resources-filtered [INFO] [INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ web_demo --- [INFO] No sources to compile [INFO] [INFO] --- surefire:3.0.0:test (default-test) @ web_demo --- [INFO] No tests to run. [INFO] [INFO] --- war:3.3.2:war (default-war) @ web_demo --- [INFO] Packaging webapp [INFO] Assembling webapp [web_demo] in [/var/lib/jenkins/workspace/test02/target/web_demo-1.0-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [/var/lib/jenkins/workspace/test02/src/main/webapp] [INFO] Building war: /var/lib/jenkins/workspace/test02/target/web_demo-1.0-SNAPSHOT.war [INFO] [INFO] --- install:3.1.0:install (default-install) @ web_demo --- [INFO] Installing /var/lib/jenkins/workspace/test02/target/web_demo-1.0-SNAPSHOT.war to /opt/repo/com/itheima/web_demo/1.0-SNAPSHOT/web_demo-1.0-SNAPSHOT.war [INFO] Installing /var/lib/jenkins/workspace/test02/target/consumer3163221908713468455pom to /opt/repo/com/itheima/web_demo/1.0-SNAPSHOT/web_demo-1.0-SNAPSHOT.pom [INFO] Copying com.itheima:web_demo:pom:1.0-SNAPSHOT to project local repository [INFO] Copying com.itheima:web_demo:war:1.0-SNAPSHOT to project local repository [INFO] Copying com.itheima:web_demo:pom:consumer:1.0-SNAPSHOT to project local repository [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.484 s [INFO] Finished at: 2023-05-26T13:52:18+08:00 [INFO] ------------------------------------------------------------------------ Finished: SUCCESS