Reputation: 6657
I want to write the Ant script that excludes the complete folder except some of the files.
I have one folder where thousands of Java files are located. Now I want to exclude that folder and I want to include two Java files out of that. How can I do that?
The below code doesn't work for me.
<target name="compile" >
<javac srcdir="src" destdir="./classes"
<exclude name="com/corporate/modes/**"/>
<include name="com/corporate/modes/UpdatePersonalDetail.java"/>
Upvotes: 1
Views: 5219
Reputation: 24194
To include only specific files from a folder in your javac
compilation task, specify the files using the <include>
element. When an <include>
element is specified, only the named file (and its project dependencies) will be included in the compilation.
Project Directory: /home/project
Source Directory: /home/project/src
Build Directory: /home/project/build
<?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>
package com.mypackage;
public class Class1 {
public static void main(String[] args){
System.out.println("Class1");
}
}
package com.mypackage;
public class Class2 {
public static void main(String[] args){
Class3 class3 = new Class3();
System.out.println(class3.getMessage());
}
}
package com.mypackage;
public class Class3 {
public String getMessage() {
return "The answer is 42.";
}
}
$ 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
Notice that although there were three Java source files, only the file specified by the <include>
element was compiled.
$ 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
In this case, although the Ant target compile_class2
only specified one file in the nested <include>
element, both Class2.java and Class3.java were compiled since Class2.java depends on Class3.java. If the dependencies of Class2.java were not included in the compilation, then when attempting to execute Class2, you would receive an error that com.mypackage.Class3 could not be located.
Upvotes: 1