Jenkins+git+maven自动构建并部署到远程tomcat上

了了

linux环境配置

1 安装Java

yum install java-1.8.0-openjdk.x86_64

2 安装maven

yum install maven

3 安装git

yum install git

4 安装tomcat

下载最新的tomcat包,放到/root目录下,运行tar zxvf tomcat-*.tgz

5 安装运行Jenkins

点击这里下载Jenkins最新的war包,并放到/root/apache-tomcat-7.0.86/webapps/目录下。
6 运行命令 cd apache-tomcat-7.0.86/webapps/ 进入war包所在目录,运行命令nohup java -jar jenkins.war --httpPort=8080 & 启动Jenkins。8080为默认端口,如果和其他服务冲突可改变为其他端口

7 浏览器访问 服务器地址:端口号(例如192.168.0.100:8080)即可访问Jenkins

Jenkins配置

1 安装必需的插件

Deploy to container Plugin; GitLab Plugin;Maven Integration

2 配置jenkins工具环境

  • 配置jdk

  • 配置Git

  • 配置Maven

3 新建任务,选择构建maven项目

4 source Code Management选择Git

需要先点击add添加一个账号,打开这个页面填写你在gitlab的账号密码即可

选择刚添加的账号,下面的分支记得改成自己需要的

5 勾选一下构建快照

6 Build这个pom文件不用修改,下面的语句可添加也可不添加

7 配置到这一步已经可以自动打包了,我们构建看一下
第一次运行会下载很多jar包,等待下载完成即可,打包成功后如下图所示

下面部分开始部署到tomcat上

tomcat准备

1 远程连接tomcat需要先配置tomcat账号
配置tomcat目录下的/conf/tomcat_user.xml ,把下面这段代码放进去

<tomcat-users>
    <role rolename="tomcat"/>
    <role rolename="role1"/>
    <role rolename="manager-script"/>
    <role rolename="manager-gui"/>
    <role rolename="manager-status"/> 
    <role rolename="admin-gui"/>
    <role rolename="admin-script"/> 
    <user username="tomcat" password="tomcat" roles="manager-gui,manager-script,tomcat,admin-gui,admin-script"/>
</tomcat-users>

2 默认的tomcat使用的端口号是8085,如果你需要部署多个tomcat的话要分别对应不同的端口号,可以在/conf/server.xml中查看,例如我使用的其中一个tomcat的端口号为8082

3 Jenkins运行任务前需要把tomcat先运行起来,否则会连接失败

进入/root/apache-tomcat-7.0.86-1/bin/目录下(或者你目标tomcat的bin目录下),
运行./startup.sh启动tomcat,
使用tail -f /root/apache-tomcat-7.0.86-1/logs/catalina.out查看运行日志,
使用ps -ef|grep tomcat查看tomcat是否启动成功

安装插件

1 在Jenkins的插件管理中搜索安装 Deploy to container Plugin

配置任务

1、新建任务,选择构建Maven项目
2、构建后操作选择Deploy war/ear to a container

WAR/EAR files选择你需要部署的war包在工作空间下的**相对路径**
Context path意为发布到目标tomcat的webapp后的文件夹名字,我这里选择和war包同名
Credentials 需要先添加一个用户,点击添加,用户名和密码分别填入tomcat(或者是你自己tomcat_user.xml中的配置)
Tomcat URL 自然就是你需要连接的tomcat地址和端口号了



3 保存任务,运行看一下,成果的话如下图所示

报错总结

1

[ERROR] Failed to execute goal on project *: Could not resolve dependencies for project * :jar:1.0-SNAPSHOT: The following artifacts could not be resolved: com.alibaba:dubbo:jar:2.8.4, com.cloopen:sms-rest-sdk:jar:2.6.3, com.taobao.pamirs.schedule:tbschedule:jar:3.3.3.2: Could not find artifact com.alibaba:dubbo:jar:2.8.4 in central (http://jcenter.bintray.com)` Failed to collect dependencies for [Pingplusplus:pingpp-java:jar:2.2.1 (compile), org.springframework:spring-core:jar:5.0.5.RELEASE (compile), org.springframework:spring-web:jar:5.0.5.RELEASE (compile), org.springframework:spring-oxm:jar:5.0.5.RELEASE (compile), org.springframework:spring-tx:jar:5.0.5.RELEASE (compile), org.springframework:spring-jdbc:jar:5.0.5.RELEASE (compile), org.springframework:spring-context:jar:5.0.5.RELEASE (compile), org.springframework:spring-context-support:jar:5.0.5.RELEASE (compile), org.springframework:spring-aop:jar:5.0.5.RELEASE (compile), org.springframework:spring-test:jar:5.0.5.RELEASE (compile), javax.validation:validation-api:jar:2.0.1.Final (compile), org.slf4j:slf4j-api:jar:1.7.18 (compile), org.slf4j:log4j-over-slf4j:jar:1.7.18 (compile), ch.qos.logback:logback-classic:jar:1.0.13 (compile), com.github.pagehelper:pagehelper:jar:5.1.4 (compile), org.aspectj:aspectjrt:jar:1.9.1 (compile), org.aspectj:aspectjweaver:jar:1.9.1 (compile), org.javassist:javassist:jar:3.18.1-GA (compile), org.jboss.netty:netty:jar:3.2.5.Final (compile), com.alibaba:dubbo:jar:2.8.4 (compile), org.apache.zookeeper:zookeeper:jar:3.4.6 (compile), com.101tec:zkclient:jar:0.10 (compile), junit:junit:jar:4.12 (test), org.apache.commons:commons-lang3:jar:3.7 (compile), commons-lang:commons-lang:jar:2.6 (compile), org.apache.commons:commons-text:jar:1.4 (compile), commons-beanutils:commons-beanutils:jar:1.9.3 (compile), redis.clients:jedis:jar:2.9.0 (compile), org.springframework.data:spring-data-redis:jar:1.7.2.RELEASE (compile), com.fasterxml.jackson.core:jackson-core:jar:2.9.5 (compile), com.fasterxml.jackson.core:jackson-databind:jar:2.9.5 (compile), com.fasterxml.jackson.core:jackson-annotations:jar:2.9.5 (compile), com.alibaba:fastjson:jar:1.2.47 (compile), com.cloopen:sms-rest-sdk:jar:2.6.3 (compile), com.aliyun:aliyun-java-sdk-core:jar:4.0.6 (compile), com.aliyun:aliyun-java-sdk-dysmsapi:jar:1.1.0 (compile), com.google.code.gson:gson:jar:2.8.2 (compile), com.taobao.pamirs.schedule:tbschedule:jar:3.3.3.2 (compile), org.apache.solr:solr-solrj:jar:4.7.2 (compile), org.apache.httpcomponents:httpcore:jar:4.4.4 (compile), commons-httpclient:commons-httpclient:jar:3.1 (compile), net.sf.json-lib:json-lib:jar:jdk15:2.4 (compile)]: Failed to read artifact descriptor for com.alibaba:dubbo:jar:2.8.4: Could not find artifact com.alibaba:dubbo-parent:pom:2.8.4 in nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public) -> [Help 1]
缺少对应的jar包,可以直接从原环境下复制过来

2

org.codehaus.cargo.container.ContainerException: Failed to redeploy [/root/.jenkins/workspace/git_1/*/target/*.war]
        at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:192)
        at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:77)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:147)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:117)
        at hudson.FilePath.act(FilePath.java:1074)
        at hudson.FilePath.act(FilePath.java:1057)
        at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:114)
        at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeploy(PasswordProtectedAdapterCargo.java:93)
        at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:64)
        at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
        at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
        at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
        at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
        at hudson.model.Run.execute(Run.java:1841)
        at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
        at hudson.model.ResourceController.execute(ResourceController.java:97)
        at hudson.model.Executor.run(Executor.java:429)
Caused by: java.io.FileNotFoundException: http://192.168.2.7:8082/manager/text/list
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1890)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:571)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.list(TomcatManager.java:876)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.getStatus(TomcatManager.java:889)
        at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:173)
        ... 17 more
java.io.FileNotFoundException: http://192.168.2.7:8082/manager/text/list
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1890)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:571)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.list(TomcatManager.java:876)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.getStatus(TomcatManager.java:889)
        at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:173)
        at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:77)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:147)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:117)
        at hudson.FilePath.act(FilePath.java:1074)
        at hudson.FilePath.act(FilePath.java:1057)
        at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:114)
        at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeploy(PasswordProtectedAdapterCargo.java:93)
        at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:64)
        at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
        at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
        at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
        at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
        at hudson.model.Run.execute(Run.java:1841)
        at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
        at hudson.model.ResourceController.execute(ResourceController.java:97)
        at hudson.model.Executor.run(Executor.java:429)
  • 原因: 缺少manager文件夹
  • 解决方案:可以从原来项目的/root/apache-tomcat-7.0.86-1/webapps/目录下拷贝过来

3

org.codehaus.cargo.container.ContainerException: Failed to redeploy [/root/.jenkins/workspace/git_1/*/target/*.war]
        at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:192)
        at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:77)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:147)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:117)
        at hudson.FilePath.act(FilePath.java:1074)
        at hudson.FilePath.act(FilePath.java:1057)
        at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:114)
        at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeploy(PasswordProtectedAdapterCargo.java:93)
        at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:64)
        at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
        at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
        at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
        at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
        at hudson.model.Run.execute(Run.java:1841)
        at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
        at hudson.model.ResourceController.execute(ResourceController.java:97)
        at hudson.model.Executor.run(Executor.java:429)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
        at sun.net.www.http.HttpClient.New(HttpClient.java:339)
        at sun.net.www.http.HttpClient.New(HttpClient.java:357)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:561)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.list(TomcatManager.java:876)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.getStatus(TomcatManager.java:889)
        at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:173)
        ... 17 more
java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
        at sun.net.www.http.HttpClient.New(HttpClient.java:339)
        at sun.net.www.http.HttpClient.New(HttpClient.java:357)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:561)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.list(TomcatManager.java:876)
        at org.codehaus.cargo.container.tomcat.internal.TomcatManager.getStatus(TomcatManager.java:889)
        at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:173)
        at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:77)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:147)
        at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:117)
        at hudson.FilePath.act(FilePath.java:1074)
        at hudson.FilePath.act(FilePath.java:1057)
        at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:114)
        at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeploy(PasswordProtectedAdapterCargo.java:93)
        at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:64)
        at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
        at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
        at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
        at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
        at hudson.model.Run.execute(Run.java:1841)
        at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
        at hudson.model.ResourceController.execute(ResourceController.java:97)
        at hudson.model.Executor.run(Executor.java:429)
Build step 'Deploy war/ear to a container' marked build as failure
Finished: FAILURE
  • 原因: 缺少manager文件夹
  • 解决方案:可以从原来项目的/root/apache-tomcat-7.0.86-1/webapps/目录下拷贝过来
阅读 3.1k

16 声望
3 粉丝
0 条评论
16 声望
3 粉丝
宣传栏