基础环境部署:
Ubuntu安装配置JDK、Tomcat、SVN服务器
链接: http://blog.51cto.com/13505030/2054688
========================================================
一、安装Jenkins
Jenkins在自动化方面使用很广泛,那么怎么部署呢?
工具/原料
-
Jenkins war包
-
Tomcat
方法/步骤
-
从官网下载Jenkins的war包
将下载好的War放到Tomcat的网站根目录webapps下,然后启动Tomcat。
root@iZuf6ea6asxmy2epuhqiodZ:/usr/java/tomcat/apache-tomcat-7.0.82/webapps# ll
total 72588
drwxr-xr-x 8 root root 4096 Dec 26 13:22 ./
drwxr-xr-x 9 root root 4096 Dec 26 12:47 ../
drwxr-xr-x 14 root root 4096 Dec 26 12:47 docs/
drwxr-xr-x 7 root root 4096 Dec 26 12:47 examples/
drwxr-xr-x 5 root root 4096 Dec 26 12:47 host-manager/
drwxr-xr-x 10 root root 4096 Dec 26 13:22 jenkins/
-rw-r--r-- 1 root root 74294776 Dec 26 13:18 jenkins.war
drwxr-xr-x 5 root root 4096 Dec 26 12:47 manager/
drwxr-xr-x 3 root root 4096 Dec 26 12:47 ROOT/
解压jenkins.war包
root@iZuf6ea6asxmy2epuhqiodZ:/usr/java/tomcat/apache-tomcat-7.0.82/webapps# java -jar jenkins.war
Running from: /usr/java/tomcat/apache-tomcat-7.0.82/webapps/jenkins.war
webroot: $user.home/.jenkins
Dec 26, 2017 1:27:41 PM Main deleteWinstoneTempContents
WARNING: Failed to delete the temporary Winstone file /tmp/winstone/jenkins.war
Dec 26, 2017 1:27:41 PM org.eclipse.jetty.util.log.Log initialized
INFO: Logging initialized @260ms to org.eclipse.jetty.util.log.JavaUtilLog
Dec 26, 2017 1:27:41 PM winstone.Logger logInternal
INFO: Beginning extraction from war file
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.handler.ContextHandler setContextPath
WARNING: Empty contextPath
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.Server doStart
INFO: jetty-9.4.z-SNAPSHOT
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.webapp.StandardDescriptorProcessor visitServlet
INFO: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.session.DefaultSessionIdManager doStart
INFO: DefaultSessionIdManager workerName=node0
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.session.DefaultSessionIdManager doStart
INFO: No SessionScavenger set, using defaults
Dec 26, 2017 1:27:42 PM org.eclipse.jetty.server.session.HouseKeeper startScavenging
INFO: Scavenging every 660000ms
Jenkins home directory: /root/.jenkins found at: $user.home/.jenkins
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.handler.ContextHandler doStart
INFO: Started w.@65e61854{/,file:///root/.jenkins/war/,AVAILABLE}{/root/.jenkins/war}
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.AbstractConnector doStop
INFO: Stopped ServerConnector@40dd3977{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.session.HouseKeeper stopScavenging
INFO: Stopped scavenging
Dec 26, 2017 1:27:43 PM hudson.WebAppMain contextDestroyed
INFO: Shutting down a Jenkins instance that was still starting up
java.lang.Throwable: reason
at hudson.WebAppMain.contextDestroyed(WebAppMain.java:388)
at org.eclipse.jetty.server.handler.ContextHandler.callContextDestroyed(ContextHandler.java:898)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextDestroyed(ServletContextHandler.java:545)
at org.eclipse.jetty.server.handler.ContextHandler.stopContext(ContextHandler.java:873)
at org.eclipse.jetty.servlet.ServletContextHandler.stopContext(ServletContextHandler.java:355)
at org.eclipse.jetty.webapp.WebAppContext.stopWebapp(WebAppContext.java:1507)
at org.eclipse.jetty.webapp.WebAppContext.stopContext(WebAppContext.java:1471)
at org.eclipse.jetty.server.handler.ContextHandler.doStop(ContextHandler.java:927)
at org.eclipse.jetty.servlet.ServletContextHandler.doStop(ServletContextHandler.java:271)
at org.eclipse.jetty.webapp.WebAppContext.doStop(WebAppContext.java:569)
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
at org.eclipse.jetty.server.handler.AbstractHandler.doStop(AbstractHandler.java:124)
at org.eclipse.jetty.server.Server.doStop(Server.java:523)
at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
at winstone.Launcher.shutdown(Launcher.java:307)
at winstone.Launcher.<init>(Launcher.java:167)
at winstone.Launcher.main(Launcher.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Main._main(Main.java:294)
at Main.main(Main.java:132)
Exception in thread "Jenkins initialization thread" java.lang.NoClassDefFoundError: hudson/util/HudsonFailedToLoad
at hudson.WebAppMain$3.run(WebAppMain.java:247)
Caused by: java.lang.ClassNotFoundException: hudson.util.HudsonFailedToLoad
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:560)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Dec 26, 2017 1:27:43 PM org.eclipse.jetty.server.handler.ContextHandler doStop
INFO: Stopped w.@65e61854{/,null,UNAVAILABLE}{/root/.jenkins/war}
Dec 26, 2017 1:27:43 PM winstone.Logger logInternal
INFO: Winstone shutdown successfully
java.io.IOException: Failed to start Jetty
at winstone.Launcher.<init>(Launcher.java:156)
at winstone.Launcher.main(Launcher.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Main._main(Main.java:294)
at Main.main(Main.java:132)
Caused by: java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:298)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:431)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at winstone.Launcher.<init>(Launcher.java:154)
... 7 more
Dec 26, 2017 1:27:43 PM winstone.Logger logInternal
SEVERE: Container startup failed
java.io.IOException: Failed to start Jetty
at winstone.Launcher.<init>(Launcher.java:156)
at winstone.Launcher.main(Launcher.java:354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at Main._main(Main.java:294)
at Main.main(Main.java:132)
Caused by: java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:298)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:431)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at winstone.Launcher.<init>(Launcher.java:154)
... 7 more
root@iZuf6ea6asxmy2epuhqiodZ:/usr/java/tomcat/apache-tomcat-7.0.82/webapps# ll
total 72588
drwxr-xr-x 8 root root 4096 Dec 26 13:22 ./
drwxr-xr-x 9 root root 4096 Dec 26 12:47 ../
drwxr-xr-x 14 root root 4096 Dec 26 12:47 docs/
drwxr-xr-x 7 root root 4096 Dec 26 12:47 examples/
drwxr-xr-x 5 root root 4096 Dec 26 12:47 host-manager/
drwxr-xr-x 10 root root 4096 Dec 26 13:22 jenkins/
-rw-r--r-- 1 root root 74294776 Dec 26 13:18 jenkins.war
drwxr-xr-x 5 root root 4096 Dec 26 12:47 manager/
drwxr-xr-x 3 root root 4096 Dec 26 12:47 ROOT/
打开浏览器,输入http://IP:8080/jenkins/ 进行访问,按提示找到Jenkins生成的密码并粘贴到框中,
输入密码进入:
这里选择把建议的插件全装上去
--设置用户名和密码
保存,进入主界面
==================================================
二、构建一个Maven项目
1、新建一个job
2、输入名称和项目类型
3、设置编译的版本号等信息
4、设置svn库地址:
输入项目托管的svn的地址,如果有出现要输入账号和密码的地方,输入即可。
如果有出现如下说明用户或密码不对:
点击进去重新设置用户和密码
5、配置jdk和maven
第一次打开出现如下,点击进去。要求提示设置JDK和Maven
点击系统管理-》系统设置,找到JDK和Maven的设置位置
选择本电脑的:
9、到这里已经可以构造这个项目了。
点击构建
构建输出的信息:
构建成功输出如下 :
10、验证
这时它已经自动把这个项目打包了一个war包,默认打包到了C:\Users\linbingwen\.jenkins\workspace\JavaWeb\JavaWeb\target
或者点击如下:
三、自动远程部署到tomcat
接下来要完成自动构建成war包后,将些war包上传到远程linux的tomcat的webapps目录,更新项目的war包,并重启tomcat.
1、安装插件
系统管理-》管理插件,在可选插件里找到下面这个,然后点击直接安装命令,安装成功后要重启jenkins
t笔者在安装插件时报错如下:
这应该是天朝的墙所导致的,所以笔者就使用了手动安装的方式。
解决方法:
手动安装
到https://wiki.jenkins-ci.org/display/JENKINS/Publish+Over+SSH+Plugin,下载hpi到本地电脑到
在系统管理--管理插件--高级--浏览-上传插件即可
然后它自动上传并安装:
红色的是笔者在线安装不成功的,蓝色的是笔者安装成功的了,之后重启jenkis即可。
2、配置ssh内容:
在系统管理-》系统设置里
找到Publish over SSH
然后输入:
3、配置Post Steps
这里还是接着上面的JavaWeb项目,这个配置得安装了上面的插件后才会显示!
其中,
Transfer SetSource files:表示要上传的本地的war包及路径,可到工作空间去看
Remove prefix:表示要上传时要去除的文件夹,即只上传war包
remote driectory:即表示执行时的路径,相当于把war包上传到这里了
exec commad:要执行的命令
要执行的脚本的内容:
#!/bin/sh #defined TOMCAT_HOME="/usr/java/tomcat/apache-tomcat-7.0.67/" ID=`ps -ef | grep java | grep tomcat|awk '{print $2}'` echo $ID echo "kill tomcat" kill -9 $ID echo "remover war file" cd "$TOMCAT_HOME"/webapps rm -rf JavaWeb-0.0.1-SNAPSHOT rm -rf JavaWeb-0.0.1-SNAPSHOT.war echo "copy war to webapp" cd /home/lin cp JavaWeb-0.0.1-SNAPSHOT.war "$TOMCAT_HOME"/webapps cd "$TOMCAT_HOME"/bin echo "start tomcat" ./startup.sh
步骤:
先停掉tomcat
删除webapp下对应的war包
复制war到webapps
重启tomcat
4、构建部署
点击项目的构建按钮,最终出现如下:
在linux上打开浏览器,输入http://localhost:8080/JavaWeb-0.0.1-SNAPSHOT/
注意:这里配置的ssh用户:lin要有root的权限,要不可以会报错没有权限执行kill 或rm 命令
本文使用的Linux:Ubuntu14.04
其中JDK、Tomcat、SVN服务器请看这里Ubuntu安装配置JDK、Tomcat、SVN服务器
转自:http://m.blog.csdn.net/article/details?id=50518959