4

I want to copy Java code from a source folder to a target folder and compile it, but need to exclude some testing code. How can I only copy the non-test code? For example, I want to include bb/app/lite2 and exclude bb/app/test1, bb/app/cg,...

I know I can use the exclude element, but I don't want to write so many exclude patterns. So can I just use:

<exclude name="bb/app/**"> and then use <include name="bb/app/lite2">?

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
terry
  • 131
  • 1
  • 5

1 Answers1

5

Given the directory structure mentioned in your question, it sounds like all of your non-test code is in the directory bb/app/lite2. In that case, you could write the <copy> task as follows as long as bb/app/lite2 appears below the specified source directory:

<property name="source.dir" location="${basedir}/src" />
<property name="target.dir" location="${basedir}/target" />

<copy todir="${target.dir}" overwrite="true">
  <fileset dir="${source.dir}" includes="bb/app/lite2/**/*.java" />
</copy>

However, adopting a naming convention for your test files such as <ClassName>Test.java makes it possible to write includes/excludes patterns as follows.

Copy all sources excluding tests

<property name="source.dir" location="${basedir}/src" />
<property name="target.dir" location="${basedir}/target" />

<copy todir="${target.dir}" overwrite="true">
  <fileset dir="${source.dir}" 
      includes="**/*.java" excludes="**/*Test.java" />
</copy>

The <javac> Ant task supports includes and excludes attributes, so rather than copy source files to a new directory, you can select the non-test files directly.

Use <javac> Ant task includes and excludes attributes

<property name="source.dir" location="${basedir}/src" />
<property name="classes.dir" location="${basedir}/build/classes" />

<javac srcdir="${source.dir}" destdir="${classes.dir}"
    includes="**/*.java" excludes="**/*Test.java"
    classpathref="my.classpath" debug="on" deprecation="on" />

As David W. mentioned in his comment, another convention (that may used in conjunction with a file naming convention) is to place test code in a separate directory. For example,

  • src/java
  • test/src/java

Or following the maven convention:

  • src/main/java
  • src/test/java

Then compiling your non-test sources is simple since you do not have to specify includes/excludes patterns:

<property name="source.dir" location="${basedir}/src/java" />
<property name="classes.dir" location="${basedir}/build/classes" />

<javac srcdir="${source.dir}" destdir="${classes.dir}"
    classpathref="my.classpath" debug="on" deprecation="on" />

Related stackoverflow questions

Community
  • 1
  • 1
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • Actually, a better way of doing this is to follow Maven conventions (even if you're not using Maven) and put your test code and your regular code in separate directories. In Maven, one is in `src/main/java` while the other is in `src/test/java` (I'll leave it to the reader to figure out which is which). You can also put resources that are required in jars, but are not java files into `src/main/resources`. Makes it much easier to do your work if it isn't all in a single directory. Otherwise, use `` and `` subtasks for the `` command. – David W. Sep 13 '12 at 14:38