0xbe5077ed
0xbe5077ed

Reputation: 4755

Ant -- Exclude my JUnit test classes from the Jar

My Java project has two directories, src/ and tests/, which contain the production code and the test code, respectively.

The package structure is mirrored in each directory — if src/ has package a.b.c, then tests/ also has an equivalent package a.b.c. The source files from both src/ and tests/ get compiled into the directory bin/. Because of the mirrored package structure, the test classes for package a.b.c get mingled in with the production classes for a.b.c.

I want an Ant target to build a jar, from the classes in bin/, but excluding all of the test files. I want to do this by mapping every filename tests/a/b/c/D.java ==> bin/a/b/c/D.class and excluding the mapped filename from the jar.

Something like this:

<target ...>
    <!-- Build a jar that contains the class files which came from -->
    <!-- source files under src/, but not those which came from -->
    <!-- source files under tests/ -->
    <jar ...>
        <fileset dir="bin">
            <!-- EXCLUDE LOGIC -->
        </fileset>
    </jar>
</target>

Upvotes: 0

Views: 1914

Answers (1)

Eric Jablow
Eric Jablow

Reputation: 7889

The best solution is to compile your src files to a bin directory in a compile target, and to compile the test files to a testbin directory in a test-compile target. You set up the classpath for the <javac> in the test-compile target to include the bin directory and the JUnit and mocking jars. You need to copy resources to the bin directory first so you can use them and override them with test versions.

Upvotes: 2

Related Questions