Ant – Building Projects ”; Previous Next Now that we have learnt about the data types in Ant, it is time to put that knowledge into practice. We will build a project in this chapter. The aim of this chapter is to build an Ant file that compiles the java classes and places them in the WEB-INFclasses folder. Consider the following project structure − The database scripts are stored in the db folder. The java source code is stored in the src folder. The images, js, META-INF, styles (css) are stored in the war folder. The Java Server Pages (JSPs) are stored in the jsp folder. The third party jar files are stored in the lib folder. The java class files are stored in the WEB-INFclasses folder. This project forms the Hello World Fax Application for the rest of this tutorial. C:workFaxWebApplication>tree Folder PATH listing Volume serial number is 00740061 EC1C:ADB1 C:. +—db +—src . +—faxapp . +—dao . +—entity . +—util . +—web +—war +—images +—js +—META-INF +—styles +—WEB-INF +—classes +—jsp +—lib Here is the build.xml required for this project. Let us consider it piece by piece. <?xml version=”1.0″?> <project name=”fax” basedir=”.” default=”build”> <property name=”src.dir” value=”src”/> <property name=”web.dir” value=”war”/> <property name=”build.dir” value=”${web.dir}/WEB-INF/classes”/> <property name=”name” value=”fax”/> <path id=”master-classpath”> <fileset dir=”${web.dir}/WEB-INF/lib”> <include name=”*.jar”/> </fileset> <pathelement path=”${build.dir}”/> </path> <target name=”build” description=”Compile source tree java files”> <mkdir dir=”${build.dir}”/> <javac destdir=”${build.dir}” source=”1.5″ target=”1.5″> <src path=”${src.dir}”/> <classpath refid=”master-classpath”/> </javac> </target> <target name=”clean” description=”Clean output directories”> <delete> <fileset dir=”${build.dir}”> <include name=”**/*.class”/> </fileset> </delete> </target> </project> First, let us declare some properties for the source, web, and build folders. <property name=”src.dir” value=”src”/> <property name=”web.dir” value=”war”/> <property name=”build.dir” value=”${web.dir}/WEB-INF/classes”/> In the above mentioned example − src.dir refers to the source folder of the project, where the java source files can be found. web.dir refers to the web source folder of the project, where you can find the JSPs,web.xml, css, javascript and other web related files build.dir refers to the output folder of the project compilation. Properties can refer to other properties. As shown in the above example, the build.dir property makes a reference to the web.dir property. In this example, the src.dir refers to the source folder of the project. The default target of our project is the compile target. But first, let us look at the clean target. The clean target, as the name suggests, deletes the files in the build folder. <target name=”clean” description=”Clean output directories”> <delete> <fileset dir=”${build.dir}”> <include name=”**/*.class”/> </fileset> </delete> </target> The master-classpath holds the classpath information. In this case, it includes the classes in the build folder and the jar files in the lib folder. <path id=”master-classpath”> <fileset dir=”${web.dir}/WEB-INF/lib”> <include name=”*.jar”/> </fileset> <pathelement path=”${build.dir}”/> </path> Finally, the build targets to build the files. First of all, we create the build directory, if it does not exist, then, we execute the javac command (specifying jdk1.5 as our target compilation). We supply the source folder and the classpath to the javac task and ask it to drop the class files in the build folder. <target name=”build” description=”Compile main source tree java files”> <mkdir dir=”${build.dir}”/> <javac destdir=”${build.dir}” source=”1.5″ target=”1.5″ debug=”true” deprecation=”false” optimize=”false” failonerror=”true”> <src path=”${src.dir}”/> <classpath refid=”master-classpath”/> </javac> </target> Executing Ant on this file compiles the java source files and places the classes in the build folder. The following outcome is the result of running the Ant file − C:>ant Buildfile: C:build.xml BUILD SUCCESSFUL Total time: 6.3 seconds The files are compiled and placed in the build.dir folder. Print Page Previous Next Advertisements ”;
Category: Ant
ANT – Custom Components
Ant – Custom Components ”; Previous Next Ant allows to create and use custom components very easily. Custom Components can be created by implementing Condition, Selector, Filter etc. interfaces. Once a class is ready, we can use typedef to create the component within build.xml to be used under any target. Syntax First define a class as Ant custom component say TextSelector.java then in build.xml, define a selector. <typedef name=”text-selector” classname=”TextSelector” classpath=”.”/> Then use that component within a target. <target name=”copy”> <copy todir=”${dest.dir}” filtering=”true”> <fileset dir=”${src.dir}”> <text-selector/> </fileset> </copy> </target> Example Create TextSelector.java with the following content and put the same in same place as build.xml − import java.io.File; import org.apache.tools.ant.types.selectors.FileSelector; public class TextFilter implements FileSelector { public boolean isSelected(File b, String filename, File f) { return filename.toLowerCase().endsWith(“.txt”); } } Create a text1.txt and a text2.java in src directory. Target is to be copy only .txt file to build directory. Create build.xml with the following content − <?xml version=”1.0″?> <project name=”sample” basedir=”.” default=”copy”> <property name=”src.dir” value=”src”/> <property name=”dest.dir” value=”build”/> <typedef name=”text-selector” classname=”TextSelector” classpath=”.”/> <target name=”copy”> <copy todir=”${dest.dir}” filtering=”true”> <fileset dir=”${src.dir}”> <text-selector/> </fileset> </copy> </target> </project> Output Running Ant on the above build file produces the following output − F:tutorialspointant>ant Buildfile: F:tutorialspointantbuild.xml copy: [copy] Copying 1 file to F:tutorialspointantbuild BUILD SUCCESSFUL Total time: 0 seconds Now only .txt file is copied. Print Page Previous Next Advertisements ”;