1

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"/> 
1
  • 2
    <exclude> patterns override all <include> patterns. Just <include> the exact files you need and don't provide an <exclude>. Commented Jul 25, 2012 at 13:41

1 Answer 1

1

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.

Example Project

Project Directory: /home/project
Source Directory: /home/project/src
Build Directory: /home/project/build


build.xml (located in /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 Source Files

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 Output

Ant target 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 

Notice that although there were three Java source files, only the file specified by the <include> element was compiled.


Ant target 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 

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I want to exclude the whole folder except one file, the how can this be achived ?
By specifying a file (or files) using <include> elements, you are indicating that only that file (and its dependencies) should be compiled. All other files and directories are automatically excluded (hence, you are excluding the whole folder except the file specified).
But what is I want to take only one file of that folder and exclude others ?
Answer updated to provide a complete project example illustrating that when a file is specified with the nested <include> element, only that file (and its project dependencies) will be compiled.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.