2011年11月4日 星期五

Apache Ant - HelloWorldWithAnt Tutorial Problems

http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html is a great tutorial. However, I meet some problems in the tutorial. Here I summarize well-known solutions here.



Problem 1:
PROJECT_HOME\build.xml:24: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

Solution:
Change <javac> tag to <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false"/>.



Problem 2:
compile:
    [mkdir] Created dir: PROJECT_HOME\build\classes
    [javac] Compiling 2 source files to PROJECT_HOME\build\classes
    [javac] PROJECT_HOME\src\HelloWorldTest.java:1: package junit.framework does not exist
    [javac] public class HelloWorldTest extends junit.framework.TestCase {
    [javac]                                                    ^
    [javac] PROJECT_HOME\src\HelloWorldTest.java:7: cannot find symbol
    [javac] symbol  : method fail(java.lang.String)
    [javac] location: class HelloWorldTest
    [javac]         fail("An error message");
    [javac]         ^
    [javac] 2 errors

BUILD FAILED

Solution:
Download JUnit and put junit-4.10.jar (for example) in PROJECT_HOME\lib directory.



Problem 3:
When we execute ant junit command, we may fail.
BUILD FAILED
PROJECT_HOME\build.xml:49: Reference application not found.

Solution:
To fix it, we need to isolate application path:
    <path id="application" location="${jar.dir}/${ant.project.name}.jar"/>

    <target name="run" depends="jar">
        <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath"/>
                <path refid="application"/>
            </classpath>
        </java>
    </target>
    
    <target name="junit" depends="jar">
        <junit printsummary="yes">
            <classpath>
                <path refid="classpath"/>
                <path refid="application"/>
            </classpath>
            
            <batchtest fork="yes">
                <fileset dir="${src.dir}" includes="*Test.java"/>
            </batchtest>
        </junit>
    </target>



So, the whole PROJECT_HOME\build.xml is
<project name="AntHelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>
    <property name="lib.dir"     value="lib"/>
    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="demo.HelloWorld"/>

    
    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>
    <path id="application" location="${jar.dir}/${ant.project.name}.jar"/>

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

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false"/>
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath"/>
                <path refid="application"/>
            </classpath>
        </java>
    </target>
    
    <target name="junit" depends="jar">
        <junit printsummary="yes">
            <classpath>
                <path refid="classpath"/>
                <path refid="application"/>
            </classpath>
            
            <batchtest fork="yes">
                <fileset dir="${src.dir}" includes="*Test.java"/>
            </batchtest>
        </junit>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>
(Reference: https://gist.github.com/1117003)

Enjoy!

(PS. To avoid import error, we should set ANT_HOME correctly in the environment variable.)

沒有留言:

張貼留言