简体   繁体   English

蚂蚁-如何排除Java测试文件?

[英]Ant - How to exclude Java test files?

I want to copy Java code from a source folder to a target folder and compile it, but need to exclude some testing code. 我想将Java代码从源文件夹复制到目标文件夹并进行编译,但是需要排除一些测试代码。 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 ,... 例如,我要包含bb/app/lite2并排除bb/app/test1bb/app/cg ,...

I know I can use the exclude element, but I don't want to write so many exclude patterns. 我知道我可以使用exclude元素,但是我不想编写那么多的exclude模式。 So can I just use: 所以我可以使用:

<exclude name="bb/app/**"> and then use <include name="bb/app/lite2"> ? <exclude name="bb/app/**"> ,然后使用<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 . 给定问题中提到的目录结构,听起来所有非测试代码都在目录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: 在这种情况下,只要bb/app/lite2出现在指定的源目录下,就可以编写如下的<copy>任务:

<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. 但是,为测试文件采用诸如<ClassName>Test.java类的命名约定,可以如下编写包含/排除模式。

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. <javac> Ant任务支持includesexcludes属性,因此您可以直接选择非测试文件,而不是将源文件复制到新目录中。

Use <javac> Ant task includes and excludes attributes 使用<javac> Ant任务includesexcludes属性

<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. 正如David W.在其评论中提到的,另一种约定(可以与文件命名约定一起使用)是将测试代码放置在单独的目录中。 For example, 例如,

  • src/java
  • test/src/java

Or following the maven convention: 或遵循Maven约定:

  • 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 相关的stackoverflow问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM