1

I have an Ant build that is compiling my main classes correctly, but failing when it goes to compile my test classes, because it cannot find the symbols for any of my main classes:

Project directory structure:

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

(Although this has the structure of a Maven project, it's not. It's an Ant-based build. I just like the way Maven organizes projects.)

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>

When I run compile-main I get a BUILD SUCCESSFUL message (it builds just fine). But when I run compile-test, I got 100+ errors like so:

[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 is a main class (enum) and has an Always value.

I have checked and rechecked my paths. For the life of me I can't figure out why my test.compile.path cannot find my main code. Thanks in advance.

  • The classpath for compile-test should include the classes generated by compile-main, I suggest. – Aubin Feb 11 '13 at 19:47

1 Answers1

1

The problem is that test.compile.path refers to the source directory for the main classes, not the target directory where the generated classes go.

If you change the javac task for the test compile to include the generated classes, it should work, as shown below:

    <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>
Dave DeCaprio
  • 2,051
  • 17
  • 31