Saturday, March 1, 2014

HelloWorld.war, Ant, build.xml and Jenkins

Ant, build.xml with Jenkins

It was 15th September 2005, I joined my first company NexGen Software Solutions Ltd as Project Trainee. The first day I join, I was aware of OOPS Concepts and was able to explain by writing simple Java Programs.  And I was not aware of that "How the big internet applications are running, how to code for that and how to run it". But as a little ant in big ocean, I was learning Java, and able to run HelloWorld.java by compiling using javac(Java Compiler), it will be compiled into byte code,that will be later converted in executable code by JRE when running that using java.
And I was not aware of How Database works, How the Internet websites are developed using java.
Then my Lead gave me the Book: Apache Ant, and asked to learn it quickly with in a week, as it required urgently for that project. But I took 2 weeks and learned something. Then I created a small Applet program and converted it as jar using the ant by build.xml. That time I was thinking that I learned lot, and appreciated myself.  This is how I got introduced with Ant, with in two weeks of my Java Career started. 

Now a days we are not running the ant or maven either in eclipse or in command prompt. Instead we configure it with any of the CI(Continuous Integration Tool).

The CI tools (Jenkins or Hudson) does check-out code from svn repository , compiles them, runs unit testing, packages them and deploys the war/tar.

CI tools will take input what to compile, what to package, where to deploy from either ant scripts or from maven.
Here we will see
Section 1: Coding of sample a sample web application.
Section 2. How to write build.xml(ANT Script).
Section 3. How to install and run Jenkins.
Section 4. How to configure the application in the Jenkins.
Section 5. How to build through the Jenkins.
-----Write Sample Application
1. Open eclipse and create "Dynamic Web Project"
2. Create a sample HelloWorld web application.
3. Run it on Tomcat and confirm whether it running successfully.
4. Please confirm the project folder structure as below:

HelloWorld
|+src
|  + test.hello.web
|        + HelloWorldController.java
|+WebContent
|  + META-INF
        + MANIFEST.MF
|  + WEB-INF
|          + helloworld-servlet.xml
        + web.xml
        + jsp
            + helloworld.jsp
|        + lib
            + commons-logging.jar
            + spring-mvc.jar
            + spring-core.jar
            + spring-web.jar
            + spring-beans.jar
            + spring-webmvc.jar
            + spring-context.jar
            + spring-mvc.jar

|+build.xml

-----Write Ant Build Scripts
1. RightClick the project and create XML file.
2. Name it as build.xml. The folder structure, where to creat the build.xml is given above.
build file should like the below.

<?xml version="1.0" encoding="UTF-8"?>
<project name="buildwar" default="deploy" basedir=".">
    <description> Compile, Package and Deploy AppajiMaamaa</description>
    <!-- set global properties for this build -->
    <property name="src" location="./src" />
    <property name="classes" location="./build/classes" />
    <property name="deployDest" location="D:\Test\apache-tomcat-6.0.37\webapps" />

    <path id="project-classpath">
        <!-- Libraries inside project lib folder like, Spring, Logging, Hibernate -->
        <fileset dir="./WebContent/WEB-INF/lib" includes="*.jar" />
        <!-- Libraries inside server lib folder like, Servlet, JSP -->
        <fileset dir="D:\Test\apache-tomcat-6.0.37\lib" includes="*.jar" />
    </path>

    <target name="clean">
        <delete dir="./build/classes">
        </delete>
        <echo> Cleaned old class files !</echo>
        <delete>
            <fileset dir="./build">
                <include name="*.war" />
            </fileset>
        </delete>
        <echo> Cleaned old war file in build folder !</echo>
    </target>

    <target name="cleanDeploy">
        <delete>
            <fileset dir="${deployDest}">
                <include name="*.war" />
                <exclude name="*/*.*" />
            </fileset>
        </delete>
        <echo> Removed war files inside Server!</echo>
        <delete dir="${deployDest}/appajimama">
        </delete>
        <echo> Removed Previous Deployment inside Server!</echo>
    </target>
    <target name="compile" depends="clean" description="compile the source ">
        <mkdir dir="./build/classes" />
        <javac includeantruntime="false" srcdir="${src}" destdir="${classes}" classpathref="project-classpath" />
        <echo> Java file compiled and copied to classes folder !</echo>
    </target>
    <target name="buildwar" description="Building War File" depends="compile">
        <war destfile="./build/appajimama.war" webxml="./WebContent/WEB-INF/web.xml">
            <fileset dir="./WebContent">
                <include name="**/*.jsp" />
                <include name="*/*.xml" />
                <include name="**/*.jar" />
            </fileset>
            <lib dir="./WebContent/WEB-INF/lib">
            </lib>
            <classes dir="./build/classes" />
        </war>
    </target>
    <target name="deploy" depends="buildwar,cleanDeploy">
        <copy file="./build/appajimama.war" todir="${deployDest}" overwrite="true">
        </copy>
    </target>

</project>

3. Run the above script and check the war is build and copied to tomcat\webapps.
4. Start the tomcat server and verify that there is no error.
5. Move the application into SVNRepository. If you want to know How to setup repository please read my previous posts.
http://chennai-java.blogspot.in/2014/02/configure-wandiscosubversion-188-with.html
------------Install jenkins --------------------
1. Download Jenkins.war from www.Jenkins.com
2. Copy inside the tomcat\webapps
3. Start the tomcat and open the browser with the URL http:\\localhost:8080\jenkins
4. If you are able the see the jenkins page, then it seems Jenkins server is installed successfully.
5. Configure Jenkins with Ant.
-----------Install Ant------------------------
1. Download Ant, and extract the zip.
2. Add the ANT_HOME to System Environment variable. Also add the ANT_HOME\bin to PATH variable.
3. Verify ant installation by typing "ant -version". If you get version details. Then Ant is installed successfully. Else please check the Environment path details.
-----------Configure ant inside the Jenkins---------
1. Open the Jenkins http:\\localhost:8080\jenkins
2. Go to Manage Jenkins -> Configure -> Configure System
3. Add JDK details with as like this Name : JDK 7, JAVA_HOME : C:\Program Files\JDK\1.7.0_45
4. Add JDK details with as like this Name : ANT , ANT_HOME : C:\Program Files\apache-ant-1.9.3
5. Save, Apply changes.
6. Stop the Jenkins Tomcat server and restart.
-----------Configure the HelloWorld into Jenkins------------------------
1. We were able to build and deploy using the build.xml from eclipse, now here we will see how to configure the application into Jenkins.
2. Click on New Item menu.
3. Project Name : HelloWorld
4. Source Code Management : Subversion: We need to give the subversion repository Subversion : http://localhost:7070/svn/spring_samples/branches/AppajiMaamaa1.0
   If we have been setup user access for the SVNRepository, please give the username and password.
6. Build : Ant Version : Default, Targets : deploy (What ever the target we gave inside the build.xml need to be mentioned here).
7. Save, Apply
----------- Build and Deploy -----------------------
1. Now Click on the "Build Now" button.
2. To see the console, click the link inside the "Build History". It will open the another screen.
3. Now click on the "Console Output". If build is success then it will show "BUILD SUCCESSFUL, Total time: 2 seconds, Finished: SUCCESS".
4. Failure Reason : Ant path is not correct, Subversion Code Repository Path may wrong. So please verify them.
         

1 comment:

Unknown said...

Hi,
How to deploy war file in other server which is in same network