除某些文件外,Ant脚本排除文件夹

时间:2012-07-25 13:29:17

标签: java ant

我想写一个排除整个文件夹的Ant脚本,除了一些文件。

我有一个文件夹,其中包含数千个Java文件。现在我想要排除该文件夹,我想要包含两个Java文件。我怎么能这样做?

以下代码对我不起作用。

<target  name="compile" >
    <javac srcdir="src" destdir="./classes"
        <exclude name="com/corporate/modes/**"/>
        <include name="com/corporate/modes/UpdatePersonalDetail.java"/>

1 个答案:

答案 0 :(得分:1)

要仅包含javac编译任务中文件夹中的特定文件,请使用<include>元素指定文件。指定<include>元素时,只有命名文件(及其项目依赖项)才会包含在编译中。

示例项目

项目目录: / home / project
源目录: / home / project / src
构建目录: / home / project / build


build.xml(位于/ home / project中)

<?xml version="1.0" encoding="UTF-8"?>
<project name="compile_test" basedir="." default="compile_class1">

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

  <target name="init" description="Initialize the build directory.">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${classes.dir}" />
  </target>

  <target name="clean" description="Delete all files created by this script.">
    <delete dir="${build.dir}" />
  </target>

  <target name="compile_class1" depends="init">
    <javac srcdir="${src.dir}" destdir="${classes.dir}"
        includeantruntime="false">
      <include name="com/mypackage/Class1.java" />
    </javac>
  </target>

  <target name="compile_class2" depends="init">
    <javac srcdir="${src.dir}" destdir="${classes.dir}"
        includeantruntime="false">
      <include name="com/mypackage/Class2.java" />
    </javac>
  </target>
</project>

Java源文件

Class1.java

package com.mypackage;

public class Class1 {
  public static void main(String[] args){
    System.out.println("Class1");
  }
}

Class2.java

package com.mypackage;

public class Class2 {
  public static void main(String[] args){
    Class3 class3 = new Class3();
    System.out.println(class3.getMessage());
  }
}

Class3.java

package com.mypackage;

public class Class3 {
  public String getMessage() {
    return "The answer is 42.";
  }
}

Ant输出

Ant目标compile_class1

$ ant clean compile_class1

Buildfile: /home/project/build.xml

clean:
   [delete] Deleting directory /home/project/build

init:
    [mkdir] Created dir: /home/project/build
    [mkdir] Created dir: /home/project/build/classes

compile_class1:
    [javac] Compiling 1 source file to /home/project/build/classes

BUILD SUCCESSFUL
Total time: 1 second

请注意,虽然有三个Java源文件,但只编译了<include>元素指定的文件。


Ant目标compile_class2

$ ant clean compile_class2

Buildfile: /home/project/build.xml

clean:
   [delete] Deleting directory /home/project/build

init:
    [mkdir] Created dir: /home/project/build
    [mkdir] Created dir: /home/project/build/classes

compile_class2:
    [javac] Compiling 1 source file to /home/project/build/classes

BUILD SUCCESSFUL
Total time: 1 second

在这种情况下,尽管Ant目标compile_class2仅在嵌套的<include>元素中指定了一个文件,但Class2.java和Class3.java都已编译,因为Class2.java依赖于Class3.java。如果编译中未包含Class2.java的依赖项,那么在尝试执行Class2时,您将收到无法找到com.mypackage.Class3的错误。