Ant - How to exclude Java test files?

I want to copy Java code from the source folder to the destination folder and compile it, but some test code needs to be excluded. How can I copy non-test code? For example, I want to enable bb/app/lite2 and exclude bb/app/test1 , bb/app/cg , ...

I know that I can use the exclude element, but I do not want to write so many exclusion patterns. So I can just use:

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

+4
source share
1 answer

Given the directory structure pointed out in your question, it looks like all your non-test code is in the bb/app/lite2 . In this case, you can write the <copy> task as follows until bb/app/lite2 appears under 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, accepting a naming convention for your test files, such as <ClassName>Test.java , allows you 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 the includes and excludes , so instead of copying the source files to a new one, you can directly select non-test files.

Use the <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" /> 

Like David W. , mentioned in his commentary, another convention (which can be used in conjunction with a file naming convention) is to place the test code in a separate directory. For instance,

  • src/java
  • test/src/java

Or after the maven convention:

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

Then compiling your non-test sources is simple, since you do not need to specify include / exclude 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" /> 

Stackoverflow Related Questions

+5
source

Source: https://habr.com/ru/post/1433973/


All Articles