2018년 9월 10일 월요일

[Automate Build] 6. Fix My Build.xml (Apache Ant)


At the previous blog, I got error message ' "SomeDirectory/clz" not found'.
My build.xml is working well at Eclipse but, it is not enough to run in Jenkins.
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestSample">

    <!-- set system properties -->
    <property name="home" value=".." />
    <property name="java6.home" value="C:/Program Files/Java/jdk1.6.0_45" />
    
    <!-- set proeject path properties -->
    <property name="project_01" value="${home}/project_01" />
    <property name="project_02" value="${home}/project_02" />
    
    <!-- set library path properties -->
    <property name="lib_01" value="${someProject}/lib/lib_01" />
    <property name="lib_01" value="${someProject}/lib/lib_02" />

    <!-- set build target -->
    <target name="Build All Group" depends="Group_01, Group_02" />
    <target name="Group_01" depends="build_project_01" />
    <target name="Group_02" depends="build_project_02" />

    <target name="build_project_01">
        <delete dir="${someProject}">
            <include name="project_01.jar" />
        </delete>
        <jar destfile="${someProject}/build_project_01.jar" basedir="${project_01}/clz" />
    </target>

    <target name="build_project_02">
        <delete dir="${someProject}">
            <include name="project_02.jar" />
        </delete>
        <jar destfile="${someProject}/build_project_02.jar" basedir="${project_02}/clz" />
    </target>

</project>


I will modify to compile with keeping original structure.
(This series' purpose is introduce auto-building system with little learning curve to my co-workers)

First, I need to make directory 'clz' instead of Eclipse.
<target name="build_project_01">

    <!-- add mkdir -->
    <delete dir="${project_01}/clz"/>
    <mkdir dir="${project_01}/clz"/>
</target>


Adding compile command.
<property name="java6.javac" location="${java6.home}/bin/javac"/>

<target name="build_project_01">
    
    <!-- add javac for compile -->
    <javac executable="${java6.javac}" fork="yes" includeantruntime="false" encoding="UTF-8"
            debug="true" debuglevel="lines,vars,source" srcdir="${project_01}/src" destdir="${project_01}/clz">
        <classpath>
            <fileset dir="${lib_01}" includes="*.jar"/>
            <fileset dir="${lib_02}" includes="*.jar"/>
        </classpath>
    </javac>

</target>


Copy files witout '.java' such as '.hbm', '.jpg' and '.js'
<target name="build_project_01">

    <!-- add copy command -->
    <copydir src="${project_01}/src" dest="${project_01}/clz" excludes="**/*.java"/>

</target>


Finally, change java.home to run build.xml in Jenkins.

<!-- java6.home path for using in eclipse -->
<!-- property name="java6.home" value="C:/Program Files/Java/jdk1.6.0_45" /-->

<!-- java6.home path for using in Jenkins -->
<property name="java6.home" value="${java.home}/.." />

<property name="java6.javac" location="${java6.home}/bin/javac"/>


This is full sample build.xml file in my case
Actual my build.xml is longer than this, but basically repeating this structure.
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestSample">

    <!-- set system properties -->
    <property name="home" value=".." />
    <property name="java6.home" value="C:/Program Files/Java/jdk1.6.0_45" />
    
    <!-- set proeject path properties -->
    <property name="project_01" value="${home}/project_01" />
    <property name="project_02" value="${home}/project_02" />
    
    <!-- set library path properties -->
    <property name="lib_01" value="${someProject}/lib/lib_01" />
    <property name="lib_01" value="${someProject}/lib/lib_02" />

    <!-- set build target -->
    <target name="Build All Group" depends="Group_01, Group_02" />
    <target name="Group_01" depends="build_project_01" />
    <target name="Group_02" depends="build_project_02" />

    <target name="build_project_01">
        <delete dir="${project_01}/clz"/>
        <mkdir dir="${project_01}/clz"/>

        <javac executable="${java6.javac}" fork="yes" includeantruntime="false" encoding="UTF-8"
                debug="true" debuglevel="lines,vars,source" srcdir="${project_01}/src" destdir="${project_01}/clz">
            <classpath>
                <fileset dir="${lib_01}" includes="*.jar"/>
                <fileset dir="${lib_02}" includes="*.jar"/>
            </classpath>
        </javac>

        <copydir src="${project_01}/src" dest="${project_01}/clz" excludes="**/*.java"/>

        <delete dir="${someProject}">
            <include name="project_01.jar" />
        </delete>
        <jar destfile="${someProject}/build_project_01.jar" basedir="${project_01}/clz" />
    </target>

    <target name="build_project_02">
        <delete dir="${project_02}/clz"/>
        <mkdir dir="${project_02}/clz"/>

        <javac executable="${java6.javac}" fork="yes" includeantruntime="false" encoding="UTF-8"
                debug="true" debuglevel="lines,vars,source" srcdir="${project_02}/src" destdir="${project_02}/clz">
            <classpath>
                <fileset dir="${lib_01}" includes="*.jar"/>
                <fileset dir="${lib_02}" includes="*.jar"/>
            </classpath>
        </javac>

        <copydir src="${project_02}/src" dest="${project_02}/clz" excludes="**/*.java"/>

        <delete dir="${someProject}">
            <include name="project_02.jar" />
        </delete>
        <jar destfile="${someProject}/build_project_02.jar" basedir="${project_02}/clz" />
    </target>

</project>


Set modified build.xml (build_in_jenkins.xml)

It is result running build.xml

Success!!