为依赖于其他项目的项目运行ant脚本

时间:2013-12-05 19:25:09

标签: java ant junit

我是apache ant的新手。现在,我正在尝试为两个项目运行一个蚂蚁。让我们看下面......

我有一个名为'Multiply'的项目。在那个项目中,我编写了一个名为'Multiply'的java类和一个名为'multiply'的函数,它将两个输入整数和返回结果相乘。

然后我创建了另一个名为'Multiply-Test'的项目。在构建路径配置中,我将“Multiply”项目添加到其中进行测试。然后我编写了一个名为'MultiplyTest'的Test类和一个测试Multiply类的Multiply类的乘法函数的返回值的测试用例。

然后我为'Multiply-Test'项目写了一个ant脚本(build.xml)文件。我的xml文件是....

<!-- Sets variables which can later be used. -->
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<property name="test.report.dir" location="test-result" />

<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="junit.class.path">
    <pathelement location="lib/junit.jar" />
    <pathelement location="${build.dir}" />
</path>

<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${test.report.dir}" />
</target>

<!-- Creates the  build, docs and dist directory-->
<target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${test.report.dir}" />
</target>

<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}/com/dat/multiply" destdir="${build.dir}">
        <classpath refid="junit.class.path" />
    </javac>
</target>

<!-- Run the JUnit Tests -->
<target name="junit" depends="compile">
    <junit printsummary="on" fork="true" haltonfailure="yes">
        <classpath refid="junit.class.path" />
        <formatter type="xml" />
        <batchtest todir="${test.report.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*Test.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

<target name="main" depends="compile, junit">
    <description>Main target</description>
</target>

然后我运行ant脚本。我在控制台中发现了以下错误....

 Buildfile: C:\Eclipse Kepler\workspace\Multiply-Test\build.xml 
 clean:  
    [delete] Deleting directory C:\Eclipse Kepler\workspace\Multiply-Test\bin 
    [delete] Deleting directory C:\Eclipse Kepler\workspace\Multiply-Test\test-tesult 
 makedir: 
    [mkdir] Created dir: C:\Eclipse Kepler\workspace\Multiply-Test\bin
    [mkdir] Created dir: C:\Eclipse Kepler\workspace\Multiply-Test\test-result
 compile:
    [javac] C:\Eclipse Kepler\workspace\Multiply-Test\build.xml:29: 
    warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
junit:
    [junit] Running com.dat.test.MultiplyTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

    BUILD FAILED
    C:\Eclipse Kepler\workspace\Multiply-Test\build.xml:36: Test com.dat.test.MultiplyTest failed

    Total time: 1 second

并发现以下junit输出错误......

   java.lang.ClassNotFoundException: com.dat.test.MultiplyTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)

但我以另一种方式测试。然后,我清理并构建两个项目。然后右键单击class - &gt;运行'MultiplyTest'类跑来跑去。 - &GT; JUnit测试。哦,真的很有用。而Junit的结果确实如此。

现在,我不知道如何从ant脚本中测试类的工作。我需要解决,但我不知道。 有人帮帮我。谢谢..!

1 个答案:

答案 0 :(得分:1)

这看起来不对:

<javac srcdir="${src.dir}/com/dat/multiply" destdir="${build.dir}">
    <classpath refid="junit.class.path" />
</javac>

srcdir的{​​{1}}不应包含<javac>部分。 <javac> documentation解释说:

  

不要在srcdir属性中包含部分包结构

相反,它应该是:

/com/dat/multiply
相关问题