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">
?
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.
<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.
<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" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.