由于需要编译java,所以必须是jdk,oracle 官网的jdk下载地址如下,文件选择后缀名为tar.gz的jdk下载
http://www.oracle.com/technetwork/Java/javase/archive-139210.html
然后解压
tar -zxvf 压缩文件名.tar.gz
编辑环境变量
vim /etc/profile
最后面添加以下配置
export JAVA_HOME=/usr/local/jdk1.7.0_80/
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/rt.jar
激活环境变量
source /etc/profile
2、准备ant
wget https://www.apache.org/dist/ant/binaries/apache-ant-1.9.7-bin.zip
unzip apache-ant-1.9.7-bin.zip
3、准备jenkins
wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war
官网下载很慢,特放到网盘http://pan.baidu.com/s/1eSfS4SU,当前版本号是2.8
将jenkins放入tomcat并启动,然后访问并next next初始化
jenkins所在tomcat,和应用所在tomcat必须是两个独立的tomcat
(因为要通过jenkins启停web应用的tomcat,此时jenkins所在tomcat不能停)
4、准备项目build.xml,我放在了项目根目录
<?xml version="1.0" encoding="UTF-8"?>
<project name="ecp" default="deploy" basedir=".">
<!--
Must set these args in jenkins!!!
TOMCAT_HOME=/usr/local/apache/apache-tomcat-8.0.35
-->
<property name="webapp.name" value="ROOT" />
<property name="dist.dir" value="${basedir}/dist" />
<property name="ant.dir" value="/root/.jenkins/plugins/ant/WEB-INF/lib" />
<property name="webRoot.dir" value="${basedir}/ROOT" />
<!-- <property name="src.dir" value="${basedir}/src" /> -->
<property name="config.dir" value="${basedir}/config" />
<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
<property name="build.dir" value="${basedir}/build" />
<!-- 使用eclipse jdt进行编译,而不使用JDK编译
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />
-->
<!-- 初始化classpath -->
<path id="project.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<!-- 添加tomcat类路径 -->
<fileset dir="${TOMCAT_HOME}/lib">
<include name="*.jar" />
</fileset>
<!-- ant lib包 -->
<fileset dir="${ant.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- get the source compile classpath in a printable form -->
<pathconvert pathsep="${line.separator}| |-- " property="echo.path.compile" refid="project.classpath">
</pathconvert>
<!-- show classpath jars -->
<target name="print_classpath">
<echo message="|-- compile classpath and basedir"/>
<echo message="| |"/>
<echo message="| |-- ${echo.path.compile}"/>
<echo message="| |-- ${basedir}"/>
</target>
<tstamp prefix="my">
<format property="day" pattern="yyyy-MM-dd"/>
<format property="time" pattern="HH:mm:ss"/>
<format property="dt" pattern="yyyy-MM-dd_HH-mm-ss"/>
</tstamp>
<target name="checks">
<available file="${TOMCAT_HOME}/webapps/${webapp.name}.war" property="found.war"/>
</target>
<!-- 备份上次war包 -->
<target name="backup" depends="checks" description="备份旧文件" if="found.war">
<echo message="begin backup..." />
<move file="${TOMCAT_HOME}/webapps/${webapp.name}.war"
tofile="${TOMCAT_HOME}/webapps/${webapp.name}.war.${my.dt}"></move>
</target>
<!-- 删除之前的目录结构 -->
<target name="clear" depends="backup" description="清理旧文件">
<echo message="begin clear..." />
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
<!-- <delete file="${TOMCAT_HOME}/webapps/${webapp.name}.war" /> -->
<delete dir="${TOMCAT_HOME}/webapps/${webapp.name}" />
<delete dir="${TOMCAT_HOME}/work/Catalina/localhost/" />
</target>
<!-- 创建目录结构 -->
<target name="init" depends="clear" description="创建初始化目录结构">
<echo message="begin init..." />
<mkdir dir="${build.dir}/classes" />
<mkdir dir="${dist.dir}" />
</target>
<!-- 编译java -->
<target name="compile" depends="init" description="编译java文件">
<echo message="begin compile..." />
<javac destdir="${build.dir}/classes" includeantruntime="false" nowarn="on"
source="1.7" target="1.7" deprecation="true" debug="true"
encoding="UTF-8" classpathref="project.classpath" >
<src path="config" />
<src path="core" />
<src path="crm" />
<src path="em" />
<src path="fa" />
<src path="hr" />
<src path="oa" />
<src path="scm" />
<src path="src" />
<src path="sso" />
<src path="test" />
<src path="thirdParty" />
<src path="wf" />
<compilerarg line="-Xlint:unchecked" />
<!-- <classpath refid="project.classpath" /> -->
</javac>
<copy todir="${build.dir}/classes">
<fileset dir="${config.dir}">
<include name="**/*.xml" />
<include name="**/*.properties" />
<include name="**/i18n/*.properties" />
</fileset>
</copy>
<echo message="end compile..." />
</target>
<!-- 将class文件打成 jar包 -->
<!--
<target name="pack" depends="compile">
<jar jarfile="${build.dir}/${webapp.name}.jar">
<fileset dir="${build.dir}/classes">
<include name="**/*.class"/>
</fileset>
</jar>
</target>
-->
<!-- 打成war包, 名称默认为 项目名 -->
<target name="war" depends="compile" description="将工程打成war包">
<echo message="begin war...${webRoot.dir}..." />
<war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}" webxml="${webRoot.dir}/WEB-INF/web.xml">
<lib dir="${lib.dir}" />
<classes dir="${build.dir}/classes" />
<fileset dir="${webRoot.dir}">
<include name="***.*" />
</fileset>
</war>
<echo message="end war..." />
</target>
<!-- copy war包 tomcat的deploy目录 -->
<target name="deploy" depends="war" description="部署项目">
<echo message="begin deploy..." />
<copy file="${dist.dir}/${webapp.name}.war" todir="${TOMCAT_HOME}/webapps" />
<echo message="end deploy..." />
</target>
</project>
6、配置jekins的全局设置,关联前面准备的jdk和ant
7、jenkins 新建自由风格的job
(1)配置“源码管理”,把项目的svn地址帐号密码配置进去
(2)配置“构建触发器”->“Poll SCM”,输入*/60 * * * *,代表每60分钟构建一次,如果代码未更新则不构建,此处为cron表达式,不懂的可以百度,不会的可以用在线工具生成,http://cron.qqe2.com/
(3)配置“构建”,新建“Execute Shell”,录入如下内容,关闭tomcat服务:
export TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-8.0.35
echo "[Deploy] Shutting down tomcat"
if [ `ps auxwwww|grep $TOMCAT_HOME|grep -v grep|wc -l` -gt 0 ]
thenfor pid in `ps auxwww|grep $TOMCAT_HOME|grep -v grep|tr -s ' '|cut -d ' ' -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
4)配置“构建”,新建“Invote Ant”,并且点“高级”:
AntVersion 选择刚才配置的ant
BuildFile 输入:build.xml(如果此文件不在项目根目录,则应该写上其路径)
Properties输入:TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-8.0.35,这个参数是在build.xml中调用的
(5)配置“构建”,新建“Execute Shell”,录入如下内容,启动tomcat服务:
echo "[Deploy] Starting up tomcat"
export BUILD_ID=XXXXXX
/usr/local/tomcat/apache-tomcat-8.0.35/bin/startup.sh
8、启动吧!注意查看Console Output。