Search code examples
javajunitantjavac

Compiling 1 file with Javac / Ant also compiles imported file/class in specified file


The task at hand is to separately compile Java classes and their associated JUnit 'Test' classes using Ant-script. The regular classes are stored in 'src', while the test classes are stored in 'test/src'. Regular classes should be compiled to 'bin' and test classes to 'test/bin'. Both are in the same package.

My Ant script looks as follows:

<javac
     includeantruntime="false"
     classpathref="master-classpath"
     destdir="${test.class.build.dir}" 
                                        >
     <src path="${src.dir}"/>
     <src path="${test.class.dir}"/>
     <include name="**/*Test*.java"/>
</javac>

And running the script shows me only one file will be compiled:

    [javac] Compiling 1 source file to C:\Users\AK_Flex\Desktop\HW01\test\bin

However, the test class as well as the regular class it imports (already compiled in 'bin') are being compiled and outputted to the 'test/bin' folder. The regular classes do not import the test classes, so 'bin' looks as desired. (code not depicted) Is there any way to circumvent this behavior of the compiler?


Solution

  • Since you want compiled classes in two different folders, you need two compilation steps.

    <javac includeantruntime="false"
           srcdir="src"
           destdir="bin"
           classpathref="master-classpath">
    </javac>
    <javac includeantruntime="false"
           srcdir="test/src"
           destdir="test/bin">
        <classpath>
            <pathelement location="bin"/>
            <path refid="master-classpath"/>
        </classpath>
    </javac>