天天看点

Auto-Build, Deploy and Notify with Jenkins

Jenkins Setup

  1. Download Jenkins

http://jenkins-ci.org/

  1. Create new job
  1. 3.  Setup Source Code Management
  1. Setup Build Triggers

Set “Poll SCM” to check the SCM internally. If there is new commit in SCM, the build task will be triggered.

  1. Setup Build Task
  1. Setup Editable Email Notification

The Email-ext plugin must be installed.

Tomcat Setup in test server

  1. Tomcat must be installed as service, because we need to start tomcat by using net start [Tomcat Service Name], whose benefit is that tomcat is started successfully when net start command returns.
  2. Setup tomcat-user for remote deploy
  1. Find the tomcat-user.xml under conf folder
  2. Add

<role rolename="manager-script"/>

<user username="tomcat" password="tomcat" roles=" manager-script"/>

                The step can also be configured in the setup process.

Copssh Setup in test server

Copssh is set up for Jenkins script remotely invoking net start [Tomcat Service Name] and net stop [Tomcat Service Name].

You’d better anticipate a windows account, because the windows account setting may have some restriction, such the length, special characters and so on.

Auto-Build Ant Script

The external lib for ant is depends

jsch.jar sshexec and scp http://www.jcraft.com/jsch/index.html
catalina-ant.jar tomcat-coyote.jar tomcat-util.jar tomcat-juli.jar Tomcat7deploy tasks http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html

The other difference between Tomcat 7 and Tomcat 6 is server path.

For Tomcat 7: http://{host}:{port}/manager/text/

For Tomcat 6: http://{host}:{port}/manager/

Build script for Tomcat 7

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

<project>

    <property environment="env"/>

    <property file="build.properties"/>

    <property name="web.name" value="timesheet"/>

   <property name="web.dir" value="WebRoot"/>

   <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>

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

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

    <property name="buildlib.dir" value="lib"/>

    <property name="tomcat.dir" value="${env.CATALINA_HOME}"/>

   <property name="drop.dir" value="drop"/>

    <path id="tomcat.ant.classpath">

    <pathelement location="${tomcat.dir}/lib/catalina-ant.jar"/>

     <pathelement location="${tomcat.dir}/lib/tomcat-coyote.jar"/>

     <pathelement location="${tomcat.dir}/lib/tomcat-util.jar"/>

     <pathelement location="${tomcat.dir}/bin/tomcat-juli.jar"/>

      </path>

              <taskdef name="start" classname="org.apache.catalina.ant.StartTask" classpathref="tomcat.ant.classpath" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" />

                <taskdef name="stop" classname="org.apache.catalina.ant.StopTask" classpathref="tomcat.ant.classpath" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" />

                <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="tomcat.ant.classpath" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" />

                <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="tomcat.ant.classpath" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" />

    <path id="classpath">

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

            <include name="*.jar"/>

        </fileset>

        <pathelement path="${build.dir}/classes"/>

     </path>

     <path id="buildclasspath">

                <path refid="classpath"/>

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

                                         <include name="annotations-api.jar"/>

                                                <include name="el-api.jar"/>

                                                <include name="jasper-api.jar"/>

                                                <include name="jsp-api.jar"/>

                                                <include name="servlet-api.jar"/>

                                </fileset>

                  </path>

    </path>

                <!-- =================================================================== -->

                <!-- The "help" target is used to display the helping message.           -->

                <!-- =================================================================== -->

<target name="help">

        <echo message=""/>

        <echo message="${webapp.name} build file"/>

        <echo message="-----------------------------------"/>

        <echo message=""/>

        <echo message="Available targets are:"/>

        <echo message=""/>

        <echo message="help      --> Print this screen"/>

        <echo message="compile   --> Compile all Java files"/>

                                <echo message="war       --> Generate war package"/>

                                <echo message="clean     --> Clean output directory"/>

                                <echo message="deploy     --> Deploy to server"/>

                                <echo message="undeploy     --> UnDeploy the app"/>

                </target>

                <target name="check-java-home" unless="env.JAVA_HOME">

                  <fail message="JAVA environment variable JAVA_HOME must be set!"/>

                </target>

                <target name="check-tomcat-home" unless="env.CATALINA_HOME">

                  <fail message="Tomcat environment variable CATALINA_HOME must be set!"/>

                </target>

                <!-- =================================================================== -->

                <!-- The "init" target is used to initialize for building                -->

                <!-- =================================================================== -->

                <target name="init"

                  description="generate war package" depends="check-java-home, check-tomcat-home, clean">

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

                </target>

                <!-- =================================================================== -->

                <!-- The "compile" target is used to compile project                     -->

                <!-- =================================================================== -->

<target name="compile" description="compile the java source" depends="init">            

        <javac destdir="${build.dir}" debug="yes" encoding="utf-8">

            <src path="${src.dir}"/>

            <classpath refid="buildclasspath"/>

        </javac>

</target>

                <!-- =================================================================== -->

                <!-- The "war" target is used to generate war package                    -->

                <!-- =================================================================== -->

                <target name="war"

                                description="generate war package" depends="compile">

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

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

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

                                   </fileset>

                                </copy>

                                <war basedir="${web.dir}" warfile="${web.name}.war" webxml="${web.dir}/WEB-INF/web.xml"> 

        </war> 

                </target>

                <!-- =================================================================== -->

                <!-- The "stoptomcat" target is used to stop tomcat with net stop        -->

                <!-- =================================================================== -->

                <target name="stoptomcat"

                                description="stoptomcat">

                                <echo message="stoptomcat"/>

                                <sshexec host="${server.address}"

                                         username="${server.user}"

                                                                 password="${server.password}"

                                                                 command="net stop &quot;${tomcat.service}&quot;"

                                                                 failοnerrοr="No"

                                                                 trust="Yes"/>                    

                </target>

                <!-- =================================================================== -->

                <!-- The "removeapp" target is used to remove timesheet under webapps    -->

                <!-- =================================================================== -->

                <target name="removeapp"

                                description="removeapp">

                                <echo message="removeapp"/>

                                <sshexec host="${server.address}"

                                         username="${server.user}"

                                                                 password="${server.password}"

                                                                 command="rm &quot;${tomcat.webapps}/${web.name}.war&quot; -f"

                                                                 failοnerrοr="No"

                                                                 trust="Yes"/>    

                                <sshexec host="${server.address}"

                                         username="${server.user}"

                                                                 password="${server.password}"

                                                                 command="rm &quot;${tomcat.webapps}/${web.name}&quot; -r -f"

                                                                 failοnerrοr="No"

                                                                 trust="Yes"/>                    

                </target>

                <!-- =================================================================== -->

                <!-- The "starttomcat" target is used to start tomcat                    -->

                <!-- =================================================================== -->

                <target name="starttomcat"

                                description="starttomcat">

                                <echo message="starttomcat"/>

                                <sshexec host="${server.address}"

                                         username="${server.user}"

                                                                 password="${server.password}"

                                                                 command="net start &quot;${tomcat.service}&quot;"

                                                                 trust="Yes"/>                    

                </target>

                <!-- =================================================================== -->

                <!-- The "undeploy" target is used to undeploy war package to tomcat         -->

                <!-- =================================================================== -->

                <target name="undeploy"

                                description="undeploy war package to tomcat" depends="stoptomcat,removeapp">

                                <echo message="tomcat.manager: ${tomcat.manager}"/>

                                <!--<undeploy

                                    failοnerrοr="no"

                                    url="${tomcat.manager}"

            username="${tomcat.user}"

            password="123"

            path="/${web.name}"/>-->

                </target>

                <!-- =================================================================== -->

                <!-- The "deploy" target is used to deploy war package to tomcat         -->

                <!-- =================================================================== -->

                <target name="deploy"

                                description="deploy war package to tomcat" depends="war,undeploy,starttomcat">

                                <echo message="tomcat.manager: ${tomcat.manager}"/>

                                <deploy url="${tomcat.manager}"

            username="${tomcat.user}"

            password="${tomcat.password}"

            path="/${web.name}"

            update="true"

            war="${web.name}.war" />

                   <!-- <start url="${tomcat.manager}" username="${tomcat.user}"

                                                password="${tomcat.password}" path="/${web.name}" /> -->

                </target>

                <!-- =================================================================== -->

                <!-- The "clean" target is used to clean output directories              -->

                <!-- =================================================================== -->

    <target name="clean" description="Clean output directories">

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

                                <delete dir="${web.name}.war"/>

    </target>         

</project>