利用jenkins+maven部署java应用到远程服务器

jenkins作为持续集成的利器,在很多公司都会使用,他可以很方便集成其他的工具,提供对项目代码管理,代码review,打包,发布一站式服务。jenkins使用java语言写的,安装的前提是需要安装jdk,既可以在直接通过java -jar jenkins.war运行,也可以将它放到tomcat的webapps目录下,随着tomcat启动而启动。

这里要进行的试验是利用jenkins持续集成将git远程代码拉取到jenkins目录下,然后利用maven对项目进行构建,然后将构建的jar包,发布到远程服务器上,并运行起来。

这里对试验资源的规划:两台虚拟机,一台作为jenkins持续集成的机器server,另一台作为远程发布java项目的机器node01。他们都需要安装jdk.

一、开始安装

server上需要安装的相关程序:

1、jdk安装,略过。

2、jenkins的安装,这里我依赖于一个tomcat8,将下载的jenkins.war包放入tomcat/webapps目录下,启动tomcat来启动jenkins。

  a)tomcat启动之后,会启动jenkins,jenkins项目启动之后,会在/root目录下生成.jenkins目录,这里存放jenkins插件,配置,登录密码,项目空间等,如下图所示:

密码在secrets/initialAdminPassword中。


  b)jenkins启动之后,访问地址为http://192.168.42.128:8080/jenkins,登录用户名和密码:admin/d2a9c843d2114f109d0572ce9ebf27fa

	

  c)jenkins只是一个管理工具,他需要集成其他工具才能进行后续代码打包发布。

3、git安装,centos下,可以直接用yum来安装,装完即可使用git命令,这里我们只需要git作为客户端命令来使用。

yum install git
查看git版本

4、maven安装,上apache官网,找到maven,下载解压缩,设置环境变量即可使用。

	

node01机器:

jdk安装,略过。

二、配置

git,maven安装完成,就可以开始着手构建项目了,这里需要将系统的git,maven集成到jenkins,所以,需要安装对应的插件。

插件安装和配置,左侧菜单中依次选择系统管理->插件管理,选择可选插件tab页,可以搜索以下插件:

1)maven integration plugin //该插件安装了,才能创建maven项目。

2)git plugin                             //从远程拉取代码

3)deploy to container plugin //发布

4)publish over ssh                //ssh远程登录


选择了插件,可以直接安装,该过程会等待,不是立即就可以完成的,视网络情况而定。

下面就开始进行配置。

1、系统设置,这里需要设置远程机器node01的key,name,hostname,username。

先在远程机器node01上通过ssh-keygen命令生成无密码登录的秘钥。然后将id_rsa的内容粘贴到publish over ssh部分的key文本框中。

然后填写node01机器名称,ip,登录名,默认root.

2、系统工具配置,依次选择系统管理->Global Tool Configuration

配置jdk安装路径,git可执行路径(默认不用配置git),maven安装路径,并且都将自动安装选项取消。


下面真正的从创建项目到构建发布,还需要一些配置。

三、构建项目并发布

1、新建任务,maven项目webapp;


接着配置源码管理,这里需要配置一个远程gitserver上的项目仓库地址,可以使github,也可以是公司内部的git服务器上的项目仓库地址。


2、配置构建策略;

这里就是构建成功之后要做的事情,比如是要将打包之后的jar通过ssh登录远程机器的方式部署到远程服务器,并执行服务器上的脚本deploy.sh来启动服务。


3、开始构建。

	

点击立即构建之后,可以在左侧Build History看到一个正在构建的记录,还有进度条,这里可以通过查看console output,看构建输出日志。


构建日志:

构建成功:


远程发布并启动程序成功。

SSH: Connecting from host [server]
SSH: Connecting with configuration [node01] ...
channel stopped
SSH: EXEC: STDOUT/STDERR from command [/root/deploy.sh] ...
Archive:  /root/webapp-0.0.1-SNAPSHOT-bin.zip
   creating: webapp-0.0.1-SNAPSHOT/
   creating: webapp-0.0.1-SNAPSHOT/lib/
 extracting: webapp-0.0.1-SNAPSHOT/lib/test-jetty-servlet-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-webapp-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-xml-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-util-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-servlet-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-security-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-server-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/servlet-api-3.0.20100224.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-continuation-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-http-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/jetty-io-8.1.0.RC5.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/log4j-1.2.17.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/slf4j-log4j12-1.7.12.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/slf4j-api-1.7.12.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/spring-beans-4.2.0.RELEASE.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/spring-core-4.2.0.RELEASE.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/commons-logging-1.2.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/spring-context-4.2.0.RELEASE.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/spring-aop-4.2.0.RELEASE.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/aopalliance-1.0.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/spring-expression-4.2.0.RELEASE.jar  
 extracting: webapp-0.0.1-SNAPSHOT/lib/webapp-0.0.1-SNAPSHOT.jar  
   creating: webapp-0.0.1-SNAPSHOT/bin/
  inflating: webapp-0.0.1-SNAPSHOT/bin/app.sh  
   creating: webapp-0.0.1-SNAPSHOT/conf/
  inflating: webapp-0.0.1-SNAPSHOT/conf/applicationContext.xml  
  inflating: webapp-0.0.1-SNAPSHOT/conf/log4j.xml  
PID: 
/root/webapp-0.0.1-SNAPSHOT/conf:/root/webapp-0.0.1-SNAPSHOT/lib/aopalliance-1.0.jar:/root/webapp-0.0.1-SNAPSHOT/lib/commons-logging-1.2.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-continuation-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-http-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-io-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-security-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-server-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-servlet-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-util-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-webapp-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/jetty-xml-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/log4j-1.2.17.jar:/root/webapp-0.0.1-SNAPSHOT/lib/servlet-api-3.0.20100224.jar:/root/webapp-0.0.1-SNAPSHOT/lib/slf4j-api-1.7.12.jar:/root/webapp-0.0.1-SNAPSHOT/lib/slf4j-log4j12-1.7.12.jar:/root/webapp-0.0.1-SNAPSHOT/lib/spring-aop-4.2.0.RELEASE.jar:/root/webapp-0.0.1-SNAPSHOT/lib/spring-beans-4.2.0.RELEASE.jar:/root/webapp-0.0.1-SNAPSHOT/lib/spring-context-4.2.0.RELEASE.jar:/root/webapp-0.0.1-SNAPSHOT/lib/spring-core-4.2.0.RELEASE.jar:/root/webapp-0.0.1-SNAPSHOT/lib/spring-expression-4.2.0.RELEASE.jar:/root/webapp-0.0.1-SNAPSHOT/lib/test-jetty-servlet-8.1.0.RC5.jar:/root/webapp-0.0.1-SNAPSHOT/lib/webapp-0.0.1-SNAPSHOT.jar
starting...\c
2896
ok
SSH: EXEC: completed after 1,204 ms
SSH: Disconnecting configuration [node01] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS

总结:

        整个过程,前面的安装和配置占了大部分时间,这是从无到有的一个过程,所以很繁琐,但是后续的构建项目,是很快的,这就是持续集成的好处,将人的工作由工具来完成,可以减少出错的概率,提高效率。

        另外,这还是没有集成代码review的部分。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值