ASCII码 ASCII码

jenkins中maven的安装及配置,如何在jenkins中创建maven任务。

发布于:2022-05-28 14:54:47  栏目:技术文档

镜像下载、域名解析、时间同步请点击 阿里云开源镜像站

本文介绍了在jenkins中maven的安装及配置(安装maven及jdk的方法),以及如何在jenkins中创建maven任务。

有三点需要注意的地方。

  • maven一定要安装在jenkins服务器上。
  • maven安装之前要先安装jdk。
  • 建任务前,需要有git环境,并配置好用户名密码。

1. JDK的安装

centos7是自带OPEN JDK1.8的,但是不建议用,因为后面在jenkins中配置JDK目录的时候,自带的OPEN JDK无法识别,还是建议到官网下载安装。

1)到oracle官网下载JDK1.8文件,并上传至服务器解压缩,无需安装,直接使用。

file

2)执行java文件,看下版本。

file

2. maven的安装

1)到apache官网下载maven 包,并上传至服务器,无需安装,解压直接使用。

file

2)配置maven文件settings.xml,在/app/maven-3.0.5/conf下。

主要修改2个地方,localRepository目录 以及 MIRRORS镜像的URL(国外的太慢,改为阿里云的)。

localRepository目录修改方式:

默认在m2下,我们自己新建一个目录/app/maven-3.0.5/mvnrepository,重新添加一行

  1. <!-- localRepository
  2. | The path to the local repository maven will use to store artifacts.
  3. |
  4. | Default: ~/.m2/repository
  5. <localRepository>/path/to/local/repo</localRepository>
  6. -->
  7. <localRepository>/app/maven-3.0.5/mvnrepository</localRepository>

依赖包镜像的下载地址URL修改方式:

  1. <!-- mirrors
  2. | This is a list of mirrors to be used in downloading artifacts from remote repositories.
  3. |
  4. | It works like this: a POM may declare a repository to use in resolving certain artifacts.
  5. | However, this repository may have problems with heavy traffic at times, so people have mirrored
  6. | it to several places.
  7. |
  8. | That repository definition will have a unique id, so we can create a mirror reference for that
  9. | repository, to be used as an alternate download site. The mirror site will be the preferred
  10. | server for that repository.
  11. |-->
  12. <mirrors>
  13. <!-- mirror
  14. | Specifies a repository mirror site to use instead of a given repository. The repository that
  15. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  16. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  17. |
  18. <mirror>
  19. <id>mirrorId</id>
  20. <mirrorOf>repositoryId</mirrorOf>
  21. <name>Human Readable Name for this Mirror.</name>
  22. <url>http://my.repository.com/repo/path</url>
  23. </mirror>
  24. -->
  25. <mirror>
  26. <id>alimaven</id>
  27. <name>aliyun maven</name>
  28. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  29. <mirrorOf>central</mirrorOf>
  30. </mirror>
  31. </mirrors>

3. 服务器系统环境变量的设置

1)编辑环境变量文件 vi /etc/profile

2)添加如下变量(根据自己的安装目录设置)

  1. export JAVA_HOME=/app/jdk1.8.0_201
  2. export MAVEN_HOME=/app/maven-3.0.5
  3. export PATH=$JAVA_HOME/bin:$PATH:$MAVEN_HOME/bin

3)生效环境变量,source /etc/profile

4)验证环境变量是否生效。能正确显示版本、home路径即可。

file

4.jenkins中的变量设置

1)设置“全局工具配置”

新增JDK设置,目录为上面讲的安装目录。(不能用系统自带的OPENJDK,不然会提示报错)

file

新增MVN设置

file

2)设置“系统配置”,增加环境变量,共3个,根据自己的安装目录填写。

file

5. 在jenkins中新建maven项目

1) 构建一个maven项目

file

2)源码部分,根据自己的情况填写。

file

3)PRE STEP部分,”ROOT POM”还是使用pom.xml文件,”Goals and options”根据maven的命令填写,具体可以看提示“Specifies the goals to execute, such as “clean install” or “deploy”. This field can also accept any other command line options to Maven, such as “-e” or “-Djava.net.preferIPv4Stack=true”.

file

4)Post Steps我们先不填,先运行下看看是否可以编译成功。

5)运行任务,第一次运行会下载大量的java依赖包。

最后会出现success,说明编译成功。

  1. 在会在maven设localRepository目录下,生成war包。
  1. Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-filtering/1.3/maven-filtering-1.3.jar
  2. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar (25 KB at 52.2 KB/sec)
  3. Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-mapping/1.0/maven-mapping-1.0.jar
  4. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/commons/commons-compress/1.9/commons-compress-1.9.jar (370 KB at 564.8 KB/sec)
  5. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream/1.4.4/xstream-1.4.4.jar (473 KB at 723.0 KB/sec)
  6. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar (238 KB at 686.2 KB/sec)
  7. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-mapping/1.0/maven-mapping-1.0.jar (10 KB at 37.9 KB/sec)
  8. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-filtering/1.3/maven-filtering-1.3.jar (50 KB at 127.8 KB/sec)
  9. [INFO] Packaging webapp
  10. [INFO] Assembling webapp [bbs] in [/var/lib/jenkins/workspace/maven_t1/target/bbs-5.7]
  11. [INFO] Processing war project
  12. [INFO] Copying webapp webResources [/var/lib/jenkins/workspace/maven_t1/lib] to [/var/lib/jenkins/workspace/maven_t1/target/bbs-5.7]
  13. [INFO] Copying webapp resources [/var/lib/jenkins/workspace/maven_t1/src/main/webapp]
  14. [INFO] Webapp assembled in [480 msecs]
  15. [INFO] Building war: /var/lib/jenkins/workspace/maven_t1/target/bbs-5.7.war
  16. [INFO]
  17. [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ bbs ---
  18. Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
  19. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (2 KB at 3.9 KB/sec)
  20. Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom
  21. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5 KB at 17.0 KB/sec)
  22. Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom
  23. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom (8 KB at 25.2 KB/sec)
  24. Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom
  25. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom (8 KB at 26.6 KB/sec)
  26. Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar
  27. Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 KB at 43.7 KB/sec)
  28. [INFO] Installing /var/lib/jenkins/workspace/maven_t1/target/bbs-5.7.war to /app/maven-3.0.5/mvnrepository/bbs/bbs/5.7/bbs-5.7.war
  29. [INFO] Installing /var/lib/jenkins/workspace/maven_t1/pom.xml to /app/maven-3.0.5/mvnrepository/bbs/bbs/5.7/bbs-5.7.pom
  30. [INFO] [1m------------------------------------------------------------------------[m
  31. [INFO] [1;32mBUILD SUCCESS[m
  32. [INFO] [1m------------------------------------------------------------------------[m
  33. [INFO] Total time: 39.307 s
  34. [INFO] Finished at: 2022-04-13T23:04:19+08:00
  35. [INFO] [1m------------------------------------------------------------------------[m
  36. Waiting for Jenkins to finish collecting data
  37. [JENKINS] Archiving /var/lib/jenkins/workspace/maven_t1/pom.xml to bbs/bbs/5.7/bbs-5.7.pom
  38. [JENKINS] Archiving /var/lib/jenkins/workspace/maven_t1/target/bbs-5.7.war to bbs/bbs/5.7/bbs-5.7.war
  39. channel stopped
  40. Finished: SUCCESS

本文转自:https://blog.csdn.net/bjsunwei/article/details/124166661

相关推荐
阅读 +