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

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

本文介绍了在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,重新添加一行

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>/app/maven-3.0.5/mvnrepository</localRepository>

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

<!-- 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>
     -->
  <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf> 
    </mirror>
  </mirrors>

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

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

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

export JAVA_HOME=/app/jdk1.8.0_201
export MAVEN_HOME=/app/maven-3.0.5
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,说明编译成功。

在会在maven设localRepository目录下,生成war包。
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-filtering/1.3/maven-filtering-1.3.jar
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)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-mapping/1.0/maven-mapping-1.0.jar
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)
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)
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)
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)
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)
[INFO] Packaging webapp
[INFO] Assembling webapp [bbs] in [/var/lib/jenkins/workspace/maven_t1/target/bbs-5.7]
[INFO] Processing war project
[INFO] Copying webapp webResources [/var/lib/jenkins/workspace/maven_t1/lib] to [/var/lib/jenkins/workspace/maven_t1/target/bbs-5.7]
[INFO] Copying webapp resources [/var/lib/jenkins/workspace/maven_t1/src/main/webapp]
[INFO] Webapp assembled in [480 msecs]
[INFO] Building war: /var/lib/jenkins/workspace/maven_t1/target/bbs-5.7.war
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ bbs ---
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
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)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom
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)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom
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)
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
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)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar
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)
[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
[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
[INFO] [1m------------------------------------------------------------------------[m
[INFO] [1;32mBUILD SUCCESS[m
[INFO] [1m------------------------------------------------------------------------[m
[INFO] Total time: 39.307 s
[INFO] Finished at: 2022-04-13T23:04:19+08:00
[INFO] [1m------------------------------------------------------------------------[m
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /var/lib/jenkins/workspace/maven_t1/pom.xml to bbs/bbs/5.7/bbs-5.7.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/maven_t1/target/bbs-5.7.war to bbs/bbs/5.7/bbs-5.7.war
channel stopped
Finished: SUCCESS

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容