Jenkins搭建全流程

一、jenkins的安装与安装配置

  1. 在Jenkins官网下载页——点击左侧windows下载(左侧为季度稳定版,右边为开发版)
  2. 一步一步安装完*.msi的文件后,会自动在安装目录(*\jenkins\secrets\initialAdminPassword)下创建管理员秘钥文件,将它复制粘贴到安装完后自动打开的8080端口页内(localhost:8080;注意去除空格),保存本机上的秘钥,有提示插件安装分常用安装(左)自定义安装(右),也可以不选,可自行选择。初步安装到这里结束了。

 

二、ant、tomcat、jdk的安装与配置

1、ant版本选择1.10+,tomcat选择7+,jdk选择1.7+

2、安装、配置环境变量后没有什么特殊的地方,建议安装在jenkins同一个父级目录下。然后在ant的lib目录下粘贴一个ant-contrib-1.0b3.jar包(我也没有测是不是放tomcat里)

 

三、jenkins持续集成配置

1、首先打开jenkins的页面,默认是localhost:8080,左侧系统管理—》管理插件—》可选插件—》右上角过滤中搜索。我的项目只需要从SVN拉取代码、编译、打包、推送到容器,所以只安装了“ant”、“subversion”、“deploy”。(安装后要重新启动jenkins服务,页面响应有延迟的)。

2、配置全局工具配置:左侧系统管理—》全局工具配置中,配置jdk、ant,填好_home。

以上各种配置需要点击应用和保存。

3、新建项目:左侧新建—》构建一个自由风格项目—》填好项目名(随意写个)—》确定,之后会打开配置页面,如果现在不配置,可稍后点击左上角jenkins回到首页,右侧列表即项目列表,可在项目名称旁的下拉箭头选择配置。

4、源码管理:勾选subversion填写版本库项目地址,credentials处填写版本库账号密码(通过右侧jenkins账户编辑、add,这样的账户会有很多,最好加上description)。

5、构建触发器:poll SCM按照crontab格式时间进行调整。

6、构建:选择invoke ant,我这里填的不是default,targets处填写的是

war -buildfile $JENKINS_HOME/workspace/PROJECT_NAME/build.xml

7、构建后操作:选择Deploy war/ear to a container,与项目中build.xml(见acxoa项目根目录下build.xml)中有关,

WAR/EAR files:war包相对地址。

Context path:war包路径。

Containers:选择tomcat的版本,按照之前SVN账户一样新建个jenkins的tomcat账户。TomcatURL:填写项目启动后访问地址

8、tomcat配置:刚刚上面提到了tomcat账户,tomcat账户默认是被注释的,需要在Catalina_HOME/conf/ tomcat-users.xml加上<role rolename="tomcat" />

<role rolename="admin"/> <role rolename="admin-gui"/>

<role rolename="manager"/> <role rolename="manager-gui"/>

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

 

注意事项:

  1. 从运行后的打印结果中我们可以看到 major version : 52 这样的字样,这个表明这个 class 文件是 JDK 1.8 (或称为 8.0) 生成的!
  2. Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.意思在Eclipse中自带的Ant环境下缺少一个jar包,该jar包中包含net/sf/antcontrib/antlib.xml,为ant-contrib-1.0b3.jar(我选用的ant是1.70的),eclipse版本是3.4.2,eclipse中自带的ant也是1.70,但是不包含ant-contrib-1.0b3.jar,因此只要下载ant,然后将其中的ant-contrib-1.0b3.jar引入到eclipse的ant中即可
  3. D:\JV\Jenkins\workspace\BOSS\src\main\java\cc\aicx\util\ImageHelper.java:24: 错误: 程序包com.sun.image.codec.jpeg不存在  [javac] import com.sun.image.codec.jpeg.JPEGCodec;的原因是sun公司的被Oracle收购了,旗下的jar包等资源在JDK版本迭代中已被移除,例如:

Java代码  

  1. FileOutputStream out = new FileOutputStream(dstName);  
  2. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  3. encoder.encode(dstImage);  

Java代码  

  1. String formatName = dstName.substring(dstName.lastIndexOf(".") + 1);  
  2. //FileOutputStream out = new FileOutputStream(dstName);  
  3. //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  4. //encoder.encode(dstImage);  
  5. ImageIO.write(dstImage, /*"GIF"*/ formatName /* format desired */ , new File(dstName) /* target */ );  
  1. 中文乱码的时候:应该改成这样的Java代码: 进入【系统管理】->【系统设置】->全局属性:KEY: LANG; VALUE:zh.CH.UTF-8;修改Jenkins.xml文件     在Jenkins安装目录下找到jenkins.xml文件,(1)找到<arguments> ……</arguments>,在中间添加-Difile.encoding=utf-8 
  2. 几个地方要注意的有:
  1. 源码管理的SVN拉取地址
  2. Tomcat的地址
  3. Build.xml中的各种tomcat地址
  4. Tomcat的webapp和work下面的几个原始文件夹不要删掉,会出现无法将War部署到容器的错误
  5. 注意ant的版本与jdk的版本问题,ant-1.9以下,跟jdk1.7以下适配

Build.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="acxoa" default="total">

         <tstamp>

                  <format property="build.latest.dir" pattern="yyyyMMddHHmmss" />

                  <format property="build.latest.date" pattern="yyyy-MM-dd" />

                  <format property="build.latest.time" pattern="HH:mm:ss" />

         </tstamp>

         <property name="src.dir" location="src" />

         <property name="web.dir" location="WebContent" />

         <property name="webinf.dir" location="${web.dir}/WEB-INF" />

         <property name="lib.dir" location="${webinf.dir}/lib" />

         <property name="build.dir" location="build/classes" />

         <property name="tomcat.home" value="D:/JV/apache-tomcat-7.0.472" />

         <!-- Output -->

         <property name="build.output" location="output" />

         <property name="build.output.dir" location="${build.output}/diary" />

         <taskdef resource="net/sf/antcontrib/antlib.xml">

                  <!--

                  Path类型

                  (1).location 表示一个文件或目录。Ant在内部将此扩展为一个绝对路径。

                  (2).refid 是对当前构建文件中某处定义的一个path的引用。 -->

                  <classpath>

                          <pathelement location="${tomcat.home}/lib/ant-contrib-1.0b3.jar" />

                  </classpath>

         </taskdef>

 

         <!-- 类路径 -->

         <path id="compile.classpath">

                  <fileset dir="${lib.dir}">

                          <include name="**/*.jar" />

                  </fileset>

                  <!-- 添加tomcat类路径 -->

                  <!-- <fileset dir="D:\OATesting\apache-tomcat-7.0.82\lib">  -->

                  <fileset dir="D:\JV\apache-tomcat-7.0.472\lib"> 

            <include name="*.jar" /> 

        </fileset> 

                  <!--  <fileset dir="/opt/tomcatoa/lib/">

                          <include name="*.jar" />

                  </fileset>

                  -->

         </path>

        

<!--

         <target name="linux.stop.tomcat" description="starts tomcat in the current console window">

                  <if>

                          <socket server="localhost" port="8081" />

                          <then>

                                   <echo message="stop tomcat..." />

                                   <exec executable="${tomcat.home}/bin/shutdown.sh" failοnerrοr="false">

                                            <env key="CATALINA_HOME" value="${tomcat.home}" />

                                   </exec>

                                   <echo message="${tomcat.home}/bin/shutdown.sh" />

                                   <waitfor maxwait="3" maxwaitunit="minute" checkevery="5" checkeveryunit="second">

                                            <not>

                                                     <socket server="localhost" port="8081" />

                                            </not>

                                   </waitfor>

                          </then>

                  </if>

         </target>

-->

 

<!-- <target name="total" depends="deploy" description="构建完成">

                  <echo>${build.output.log.datetime}构建完成!</echo>

         </target>-->

 

         <target name="total" depends="war" description="构建完成">

                  <echo>${build.output.log.datetime}构建完成!</echo>

         </target>

 

<!--

         <target name="deploy" depends="war,linux.stop.tomcat" description="部署项目">

                  <echo>${build.time}部署项目</echo>

                  <delete file="/opt/tomcatoa/webapps/acxoa.war" failοnerrοr="false" />

                  <delete dir="/opt/tomcatoa/webapps/acxoa" failοnerrοr="false" />

                  <copy file="/root/.jenkins/workspace/acxoa/output/diary/acxoa.war" todir="/opt/tomcatoa/webapps" />

         </target>

-->

 

         <target name="war" depends="init,compilesrc" description="生成War">

                  <war destfile="${build.output.dir}/acxoa.war" webxml="${webinf.dir}/web.xml" >

                          <!--Fileset 数据类型定义了一组文件,并通常表示为<fileset>元素。不过,许多ant任务构建成了

                                   隐式的fileset,这说明他们支持所有的fileset属性和嵌套元素。以下为fileset 的属性列表。

                                   (1).dir表示fileset 的基目录。

                                   (2).casesensitive的值如果为false,那么匹配文件名时,fileset不是区分大小写的,其默认

                                   值为true.

                                   (3).defaultexcludes 用来确定是否使用默认的排除模式,默认为true。

                                   (4).excludes 是用逗号分隔的需要派出的文件模式列表。

                                   (5).excludesfile 表示每行包含一个排除模式的文件的文件名。

                                   (6).includes 是用逗号分隔的,需要包含的文件模式列表。

                                   (7).includesfile 表示每行包括一个包含模式的文件名。 -->

                          <fileset file="${web.dir}/WebContent">

                                   <include name="**/**.*" />

                                   <exclude name="**/*.jar" />

                                   <exclude name="**/*.class" />

                          </fileset>

                          <lib dir="${lib.dir}">

                          </lib>

                          <classes dir="${build.dir}">

                          </classes>

                  </war>

                  <echo>${build.output.log.datetime}生成War完成!</echo>

         </target>

 

         <target name="compilesrc" description="编译源代码文件">

                  <!--(1).file 表示源文件。

                          (2).tofile 表示目标文件。

                          (3).todir 表示目标目录。

                          (4).overwrite 表示指定是否覆盖目标文件,默认值是不覆盖。

                          (5).includeEmptyDirs 表示制定是否拷贝空目录,默认值为拷贝。

                          (6).failonerror 表示指定如目标没有发现是否自动停止,默认值是停止。

                          (7).verbose 表示制定是否显示详细信息,默认值不显示。 -->

                  <copy todir="${build.dir}">

                          <fileset dir="${src.dir}/main/modeler">

                          </fileset>

                          <fileset dir="${src.dir}/main/resources">

                          </fileset>

                          <fileset dir="${src.dir}/main/workflow">

                          </fileset>

                  </copy>

                  <!-- (1).srcdir表示源程序的目录。

                           (2).destdir表示class文件的输出目录。

                           (3).include表示被编译的文件的模式。

                           (4).excludes表示被排除的文件的模式。

                           (5).classpath表示所使用的类路径。

                           (6).debug表示包含的调试信息。

                           (7).optimize表示是否使用优化。

                           (8).verbose 表示提供详细的输出信息。

                           (9).fileonerror表示当碰到错误就自动停止。-->

                  <javac srcdir="${src.dir}" destdir="${build.dir}" includeAntRuntime="false" encoding="UTF-8" debug="true">

                          <include name="**/*.java" />

                          <classpath refid="compile.classpath" />

                  </javac>

                  <echo>${build.output.log.datetime}编译源代码文件完成!</echo>

         </target>

 

         <target name="init" depends="clean,cleanoutput" description="创建输出目录">

                  <mkdir dir="${build.dir}" />

                  <mkdir dir="${build.output.dir}" />

                  <echo>${build.output.log.datetime}输出目录创建完成!</echo>

         </target>

 

         <target name="clean" description="清理编译目录">

                  <!-- (1)/file表示要删除的文件。

                           (2).dir表示要删除的目录。

                           (3).includeEmptyDirs 表示指定是否要删除空目录,默认值是删除。

                           (4).failonerror 表示指定当碰到错误是否停止,默认值是自动停止。

                           (5).verbose表示指定是否列出所删除的文件,默认值为不列出。-->

                  <delete includeemptydirs="true" failοnerrοr="false">

                          <fileset dir="${build.dir}">

                                   <include name="**/*" />

                          </fileset>

                  </delete>

                  <echo>编译目录清理完成!</echo>

         </target>

 

         <target name="cleanoutput" description="清理输出目录">

                  <delete includeemptydirs="true" failοnerrοr="false">

                          <fileset dir="${build.output}">

                                   <exclude name="output*.log" />

                          </fileset>

                  </delete>

                  <echo>输出目录清理完成!</echo>

         </target>

</project>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值