Simple ant build script that supports src/ and tes

2019-06-26 07:13发布

Currently I use an IDE for all my builds and unit tests. Now I have a need to use ant. I found a few simple ant build.xml scripts but they didn't support a separate Junit test/ dir. My projects are structured as follows:

src/
  com/foo/
  com/bar/

test/ -- Mirror of src/, with all *Test.java files.
  com/foo/ 
  com/bar/

lib/  -- All Java libs, including junit 4.

How can a construct a small ant script that builds my src/ and test/ Java classes then runs all my JUnit tests?

标签: ant
2条回答
一纸荒年 Trace。
2楼-- · 2019-06-26 07:30

I don't understand the question. Are you asking how to set the default target? Select which target to run when executing or do you just not know how to write build.xml files? It's not that hard really. See http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html and http://ant.apache.org/manual/

查看更多
劫难
3楼-- · 2019-06-26 07:48

I define <path> elements for each target.

This is an excerpt from my build file, you'll have to adapt some paths and properties, but you can get the idea:

<path id="src.path">    
    <pathelement path="src/"/>
</path>

<path id="compile.path">
    <path refid="src.path"/>
    <fileset dir="lib/">
        <include name="**/*.jar"/>
    </fileset>
</path>

<path id="unit.test.path">
    <path refid="compile.path"/>
    <pathelement path="test/"/>
</path>

<target name="compile">
    <javac destdir="bin">
        <src path="src"/>
        <classpath refid="compile.path"/>
    </javac>
</target>

<target name="compileUnitTests" depends="compile">
    <javac srcdir="test/" destdir="bin">
        <classpath refid="unit.test.path"/>
    </javac>
</target>

<target name="runUnitTests" depends="compileUnitTests">
    <junit printsummary="yes" haltonfailure="no">
    <jvmarg value="-Dfile.encoding=UTF-8"/>
        <classpath refid="unit.test.path"/>

        <formatter type="xml"/>

        <batchtest fork="yes" todir="${this.report}">
            <fileset dir="test">
                <include name="${test.pattern}"/>
                <exclude name="**/AllTests.class"/>
                <exclude name="**/*$*.class"/>
            </fileset>
        </batchtest>
    </junit>
</target>

And if you need to refine this to your needs, as cotton.m says, go read the ant task docs. Using ant with your specific directory structure does require some knowledge of the tool, don't expect you'll easily find ready-made examples that just work with your exact requirements.

查看更多
登录 后发表回答