Ant测试编译无法找到主类

时间:2013-02-11 19:41:39

标签: java ant classpath javac

我有一个正确编译我的主类的Ant构建,但是当它编译我的测试类时失败,因为它无法找到我的任何主类的符号:

项目目录结构:

MyProj/
    src/
        main/
            java/
            resources/
        test/
            java/
            resources/
    build/
        build.properties
        build.xml
    gen/
        lib/
            main/
            test/
        bin/
            main/
            test/

(虽然这有Maven项目的结构,但事实并非如此。这是一个基于Ant的构建。我就像Maven组织项目一样。)

build.properties

src.main.java.dir=src/main/java
src.test.java.dir=src/test/java
gen.dir=gen
gen.bin.dir=${gen.dir}/bin
gen.bin.main.dir=${gen.bin.dir}/main
gen.bin.test.dir=${gen.bin.dir}/test
gen.lib.dir=${gen.dir}/lib
gen.lib.main.dir=${gen.lib.dir}/main
gen.lib.test.dir=${gen.lib.dir}/test

build.xml

<project name="MyProj" default="deploy" basedir="..">
    <property environment="env" />
    <property file="build/build.properties" />

    <!-- Sourcepath for main Java source. -->
    <path id="src.main.path">
        <fileset dir="${src.main.java.dir}">
            <include name="**.*java"/>
        </fileset>
    </path>

    <!-- Sourcepath for test Java source. -->
    <path id="src.test.path">
        <fileset dir="${src.test.java.dir}">
            <include name="**.*java"/>
        </fileset>
    </path>

    <!-- Path for main Java dependencies. -->
    <path id="lib.main.path">
        <fileset dir="${gen.lib.main.dir}">
            <include name="**.*jar"/>
        </fileset>
    </path>

    <!-- Path for test Java dependencies. -->
    <path id="lib.test.path">
        <fileset dir="${gen.lib.test.dir}">
            <include name="**.*jar"/>
        </fileset>
    </path>

    <!-- Main compile path. -->
    <path id="main.compile.path">
        <path refid="src.main.path"/>
        <path refid="lib.main.path"/>
    </path>

    <!-- Test compile path. -->
    <path id="test.compile.path">
        <path refid="src.test.path"/>
        <path refid="src.main.path"/>
        <path refid="lib.test.path"/>
        <path refid="lib.main.path"/>
    </path>

    <!-- Compile main Java sources. -->
    <target name="compile-main">
        <echo message="Compiling main sources." />
        <javac includeantruntime="false" srcdir="${src.main.java.dir}" destdir="${gen.bin.main.dir}">
            <classpath refid="main.compile.path"/>
        </javac>
    </target>

    <!-- Compile test Java sources. -->
    <target name="compile-test" depends="compile-main">
        <echo message="Compiling test sources." />
        <javac includeantruntime="false" srcdir="${src.test.java.dir}" destdir="${gen.bin.test.dir}">
            <classpath refid="test.compile.path"/>
        </javac>
    </target>
</project>

当我运行compile-main时,我收到一条BUILD SUCCESSFUL消息(它构建得很好)。但是当我运行compile-test时,我得到了100多个错误:

[javac] /home/myUser/sandbox/workbench/eclipse/workspace/MyProj/src/test/java/com/myproj/server/SomeTest.java:79: cannot find symbol
[javac] symbol  : variable Widget
[javac] location: class com.myproj.server.SomeTest
[javac]                 Widget.Always,
[javac]                 ^
[javac] 100 errors

BUILD FAILED
/home/myUser/sandbox/workbench/eclipse/workspace/MyProj/build/build-local.xml:163: Compile failed; see the compiler error output for details.

Widget是一个主类(枚举),其值为Always

我检查并重新检查了我的路径。对于我的生活,我无法弄清楚为什么我的test.compile.path无法找到我的主要代码。提前谢谢。

1 个答案:

答案 0 :(得分:0)

问题是test.compile.path引用了主类的源目录,而不是生成的类所在的目标目录。

如果更改测试编译的javac任务以包含生成的类,它应该可以工作,如下所示:

    <javac includeantruntime="false" srcdir="${src.test.java.dir}" destdir="${gen.bin.test.dir}">
        <classpath refid="test.compile.path"/>
        <classpath location="${gen.bin.main.dir}" />
    </javac>