{"id":112,"date":"2009-01-28T07:10:15","date_gmt":"2009-01-28T07:10:15","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2009\/01\/28\/apache-ant-learn-apache-ant-from-ibm-experts\/"},"modified":"2017-12-26T00:49:20","modified_gmt":"2017-12-26T00:49:20","slug":"apache-ant-learn-apache-ant-from-ibm-experts","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/apache-ant-learn-apache-ant-from-ibm-experts\/","title":{"rendered":"Apache Ant: Learn Apache Ant from IBM Experts"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3994 aligncenter\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2009\/01\/learn-apache-ant.png\" alt=\"learn-apache-ant-from-experts\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2009\/01\/learn-apache-ant.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2009\/01\/learn-apache-ant-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Apache Ant: Learn Apache Ant from IBM Experts <\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong><u><span style=\"font-size: 16px; color: #808000;\">Section 1. Getting started\u00a0<\/span><\/u><\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>\u00a0\u00a0<\/strong><br \/>\n<strong>What is this tutorial about?<\/strong><br \/>\nIn this tutorial, you&#8217;ll learn about Ant, a build tool for Java TM projects. Ant has quickly become popular among Java developers because of its flexibility and ease of use, so you it owe it to yourself to find out more about it. You don&#8217;t need any prior experience with or knowledge of Ant before you proceed. We&#8217;ll first see the basic structure of Ant build files, and learn how to invoke the tool. We&#8217;ll walk through the steps involved in writing a build file for a simple Java project, and then look at some of Ant&#8217;s other useful functions, including filesystem operations and pattern matching. We&#8217;ll finish by writing our own Java class that extends Ant&#8217;s functionality. In the course of the tutorial, we&#8217;ll show you how to run Ant both from the command line and from within the open source Eclipse IDE. You don&#8217;t need both environments.<\/span><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 2. Ant basics<\/strong><\/span><\/u><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Ant basics introduction<\/strong><br \/>\nIn this section, we&#8217;ll give you an overview of Ant&#8217;s capabilities and strengths, and discuss its history and rise in popularity. We then delve straight into the fundamentals of Ant by examining the basic structure of a minimal build file. We&#8217;ll also introduce the concepts of properties and dependencies. What is Ant? Apache Ant is a Java-based build tool. According to its original author, James<br \/>\nDuncan Davidson, the name is an acronym for another neat tool. Build tools are used in software development to transform source code and other input files into an executable form (and maybe into installable product images as well). As an application&#8217;s build process becomes more complex, it becomes increasingly important to ensure that precisely the same build steps are carried out<br \/>\nduring each build, with as much automation as possible, in order to produce consistent builds in a timely manner. Traditional projects in C or C++ often use the make tool for this purpose, where the build tasks are performed by invoking shell commands, and dependencies are defined between each of the build tasks so that they are always performed in the necessary order.<br \/>\nAnt is similar to make in that it defines dependencies between build tasks; however, instead of implementing build tasks using platform-specific shell commands, it uses cross-platform Java classes. With Ant, you can write a single build file that operates consistently on any Java platform (as Ant itself is implemented in the Java language); this is Ant&#8217;s greatest strength.<br \/>\nAnt&#8217;s other key strengths are its powerful simplicity and its ability to be seamlessly extended with custom capabilities. Hopefully, you will appreciate these strengths after completing the rest of this tutorial.<br \/>\n<strong>A little history<\/strong><br \/>\nAnt started out as an internal component of Tomcat, the servlet container that is used in the reference implementation for the Java Servlet and JavaServer Pages (JSP) technologies. The Tomcat codebase was donated to the Apache Software Foundation; there, it became part of the Apache Jakarta project, which has a mission to produce open source, server-side solutions for the Java platform. The<br \/>\nusefulness of Ant was quickly recognized and its usage spread throughout the other Jakarta subprojects. Thus, it was made into a Jakarta subproject of its own, with the first independent release being made in July of 2000. Since then, Ant&#8217;s popularity has grown and grown. It has won numerous industry awards and become the de facto standard for building open source Java projects. In<br \/>\nNovember of 2002, this success was acknowledged by Ant&#8217;s promotion to a top-level Apache project. At the time of writing, the current stable release of Ant is 1.5.4, which supports all<br \/>\nJDK versions from 1.1 onwards. Beta versions of the next release, 1.6, are also available; these require JDK 1.2 or later. Planning is also underway for a future 2.0 release, which will involve a major rearchitecture. Ant 2.0 will feature improved consistency and enhanced functionality, whilst still retaining Ant&#8217;s core goals of simplicity, ease of understanding, and extensibility.<br \/>\n<strong>Anatomy of an Ant build file<\/strong><br \/>\nAnt does not define its own custom syntax; instead, its build files are written in XML (see Resources ). There are a defined set of XML elements that Ant understands, and, as you&#8217;ll see in a later section, new elements can be defined to extend Ant&#8217;s capabilities. Every build file consists of a single top-level project element, which in turn contains one or more target elements. A target is a defined step in a build that performs any number of operations, such as compiling a set of source files. The operations themselves are performed by other specialized task tags, as we&#8217;ll see later. These tasks are then grouped together into target elements, as desired. All of the operations required for a build could be placed into a single target element, but that would reduce flexibility. It is usually preferable to split the operations into logical build steps, with each step contained in its own target element. This allows you to perform one individual part of the overall build without necessarily performing other parts. For instance, by only invoking certain targets, you could compile your project&#8217;s source code without creating an installable project image. The top-level project element needs to contain a default attribute that specifies the target to be executed if none is specified when Ant is invoked. Then the target itself needs to be defined, using the target element. Here is a minimal build file:<\/span><\/p>\n<p>Note that this is well-formed XML, with an XML declaration specifying the version of XML being used (this is not currently required by Ant, but it is good practice), and with every element being properly closed. It is also possible to open and close an element in one go. So, instead of the separate start and end tags for the target element above, we could have written it as follows:<\/p>\n<p><span style=\"font-family: verdana, geneva;\">This more concise form can be clearer when an element doesn&#8217;t have any enclosed content.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Adding descriptions<\/strong><br \/>\nThe build file we saw in the previous section is nice and concise, but it doesn&#8217;t say much about the actual project being built. There are a number of ways to make it more descriptive without changing the functionality. Here&#8217;s an example:<\/span><\/p>\n<p>A simple project introducing the use of descriptive tags in Ant build files.<\/p>\n<p>As you can see, XML comments can be used throughout build files to improve clarity. Also, Ant defines its own description element and description attribute, which can be used to provide more structured annotations.<\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Properties<\/strong><br \/>\nProperties in Ant are like programming language variables in that they have a name and a value. However, unlike normal variables, properties in Ant cannot be changed once they have been set; they are immutable, like String objects in the Java language. This might seem restrictive at first, but it is in keeping with Ant&#8217;s philosophy of simplicity: after all, it is a build tool, not a programming language. If<br \/>\nyou do attempt to give an existing property a new value, this isn&#8217;t treated as an error, but the property does retain its existing value. (This behavior can be useful, as we&#8217;ll see.)<br \/>\nBased on the descriptive names of the elements and attributes seen so far, it should come as no surprise that the mechanism for setting properties in Ant looks like this:<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">To refer to this property in other parts of the build file, you would use this syntax: ${metal} For example, to use the value of one property as part of the value of another, you would write a tag that looks like this:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">There are number of predefined properties in Ant. Firstly, all of the system properties set by the Java environment used to run Ant are available as Ant properties, such as ${user.home}. In addition to these, Ant defines a small set of its own properties, including ${ant.version}, which contains the version of Ant, and ${basedir}, which is the absolute path of the project&#8217;s directory (as defined by the directory containing the build file, or by the optional basedir attribute of the project element). Properties are often used to refer to files and directories on the filesystem, but this could cause problems across different platforms with different path separator characters ( \/ versus \\, for example). Ant&#8217;s location attribute is specifically designed to hold filesystem paths in a platform-neutral way. You would use location in place of value like so:<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">The path separator characters used for the location attribute are converted to the correct ones for the current platform; and, as the filename is relative, it is taken to be relative to the project&#8217;s base directory. We could just as easily have written this instead:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Both versions of this tag will behave in the same fashion across different platforms. The only thing to avoid if portability is required is DOS-style drive letters in<br \/>\nfilenames. It is also more flexible to use relative path names instead of absolute ones where possible.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Defining dependencies<\/strong><br \/>\nBuilding a project generally requires a number of steps &#8212; first compiling source code and then packaging it into Java Archive Files (JARs) files, for example. (See Resources for more information on using JARs.) Many of these steps have a clearly defined order &#8212; for instance, you can&#8217;t package the classfiles until they have been generated by the compiler from the source code. Instead of specifying the targets in sequence order, Ant takes the more flexible approach of defining dependencies, just as make and similar build tools do. Each target is defined in terms of all the other<br \/>\ntargets that need to be completed before it can be performed. This is done using the depends attribute of the target element. For example:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">This approach allows you to request a build of any stage of the project; Ant will execute the defined prerequisite stages first. In the example above, if you ask Ant to developerWorks\u00ae ibm.com\/developerWorks complete the compile step, it determines that the targets init and preprocess need to be executed first. The init target doesn&#8217;t depend on anything, so that will<br \/>\nbe executed first. Then Ant will look at the preprocess target and find that it depends on the init target; because it has already run that, it doesn&#8217;t do so again, and executes the preprocess target. Finally, the compile task itself can be executed. Note that the order in which the targets appear in the build file is not important: it is only the depends attributes that determine execution order. <\/span><\/p>\n<p><!--nextpage--><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 3. Running Ant<\/strong><\/span><\/u><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Running Ant introduction<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The Apache Ant tool can be invoked in various ways. On its own, Ant comes in command-line form, and is usually invoked from a UNIX or Linux shell prompt or a Windows command prompt, with the build files being written using your text editor of choice. This is fine if the project to be built is being developed in this way, but many find IDEs more convenient. Most of these offer some level of support for Ant, so at a minimum Ant builds can be invoked without the hassle of having to leave the IDE to perform command-line operations.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">In this section, we&#8217;ll see how to use Ant from the command line, and learn some useful command-line options. Then we&#8217;ll take a brief tour of the Ant support offered by the open source Eclipse platform. (You should be at least passingly familiar with Eclipse in order to get the most out of those panels.)<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Running Ant from the command line<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Invoking Ant from your command prompt can be as simple as just typing ant on its own. If you do so, Ant uses the default build file; the default target specified in that build file is the one that Ant attempts to build. It is also possible to specify a number of command-line options, followed by any number of build targets, and Ant will build each of these targets in order, resolving any dependencies along the way.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Here is some typical output from a simple Ant build executed from the command line:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Buildfile: build.xml<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">init:<br \/>\n[mkdir] Created dir: E:\\tutorials\\ant\\example\\build<br \/>\n[mkdir] Created dir: E:\\tutorials\\ant\\example\\dist<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">compile:<br \/>\n[javac] Compiling 8 source files to E:\\tutorials\\ant\\example\\build<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">dist:<br \/>\n[jar] Building jar: E:\\tutorials\\ant\\example\\dist\\example.jar<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">BUILD SUCCESSFUL<br \/>\nTotal time: 2 seconds<\/span><\/p>\n<p>We&#8217;ll see just what all that means as we move on through the tutorial.<\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Command-line options<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Just as the make tool looks for a build file with the name makefile by default, Ant looks for a file with the name build.xml. Therefore, if your build file uses this name, you don&#8217;t need to specify it on the command line. Naturally, it is sometimes convenient to have build files with other names, in which case you need to use the -buildfile arguments to Ant ( -f is the abbreviated version).<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Another useful option is -D, which is used to set properties that can then be used in the build file. This is very useful for configuring the build you want to initiate in some way. For example, to set the name property to a particular value, you would use an option that looks something like this:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">-Dmetal=beryllium<br \/>\nThis capability can be used to override initial property settings in the build file. As noted earlier, you cannot change the value of a property once it has been set. The -D flag sets a property before any information in the build file is read; because the assignment in the build file comes after this initial assignment, it therefore doesn&#8217;t change the value.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>IDE integration<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Due to Ant&#8217;s popularity, most modern IDEs now have integrated support for it, and many others offer support for it in plugins. The list of supported environments includes the JEdit and Jext editors, Borland JBuilder, IntelliJ IDEA, the Java Development Environment for Emacs (JDEE), NetBeans IDE, Eclipse, and WebSphere (R) Studio Application Developer.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Eclipse support for Ant<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The open source Eclipse project offers a great deal of support for Ant. The core of this support is Eclipse&#8217;s Ant editor, which features syntax highlighting. The editor is illustrated below:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<img border=\"0\" \/>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">This editor provides content assistance &#8212; for example, typing tag, and a short explanation of that task. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">You can also see an outline view that shows the structure of the build file and provides a quick way of navigating through the file. There is also an Ant view that allows targets to be built from a number of different Ant files.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Running Ant builds from within Eclipse<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Files named build.xml are identified and decorated in Eclipse&#8217;s navigator view with an Ant icon. (See File associations in Eclipse if your build file has a different filename.) Right-clicking these files provides a Run Ant&#8230; menu option that opens a dialog like the one shown below:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<img border=\"0\" \/>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">All of the targets from the build file are shown, with the defaults selected. After you&#8217;ve decided whether or not to change the default targets, press the Run button to launch Ant. Eclipse will switch to the Console view to show the output, as illustrated below. Errors are shown in a different color, and you can click on task names in the output to jump to the corresponding invocation in the build file.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<img border=\"0\" \/>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>File associations in Eclipse<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">By default, Eclipse only uses the Ant editor on files named build.xml, but you can easily configure the editor to recognize files with other names. Select Window=&gt;Preferences from the menu, then expand the Workbench group, and select the File Associations preference page. Then add a new file type for the desired filenames. You could, for example, add a new file type for all files named mybuild.xml. You could even use *.xml if you want to do the same for all files with an .xml suffix (except for specific filenames, such as plugin.xml, which in Eclipse override the wildcard specifications). Finally, add an associated editor for this new file type, and select Ant editor from the editor selection list, as shown below:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<img border=\"0\" \/>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<\/span><\/p>\n<p><!--nextpage--><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 4. Building a simple Java project<\/strong><\/span><\/u><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Building a simple Java project introduction<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Now that we have seen the format of Ant build files, and learned how to define properties and dependencies and how to run Ant, we are ready to construct a build environment for a basic Java project. This will involve learning the Ant tasks for compiling source code and assembling JAR files.<br \/>\n<strong>Compiling source code<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">As Ant is primarily aimed at building Java applications, it should come as no surprise that it has excellent built-in support for invoking the javac compiler and other Java-related tasks. Here&#8217;s how to write a task that compiles some Java code:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>This tag looks for all files ending in .java in the src directory and invokes the javac compiler on them, generating the classfiles in the same directory. Of course, it is usually cleaner to put the classfiles under a separate directory structure; you can have Ant do this by adding a destdir attribute. Other useful attributes:<\/p>\n<p><span style=\"font-family: verdana, geneva;\">classpath: Equivalent to the -classpath option on javac.<br \/>\ndebug=&#8221;true&#8221;: Indicates to the compiler that the source files should be compiled with debug information.<br \/>\nAn important characteristic of the javac task is that it will only compile those source files that it believes it needs to. If a classfile already exists, and that classfile&#8217;s corresponding source file hasn&#8217;t been changed since the classfile was generated, then that source file won&#8217;t be recompiled. The output of the javac task shows the number of source files that were actually compiled. It is good practice to write a clean target that removes any generated classfiles from that target directory. This can then be used if you want to make sure that all of the source files are compiled. This behavior characterizes many of Ant&#8217;s tasks: If the task can determine that the requested operation doesn&#8217;t need to be performed, then it will be skipped.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Like Ant, the javac compiler is itself implemented in the Java language. This is used to good advantage by the javac task in Ant, because it usually invokes the compiler classes in the same Java virtual machine (JVM) in which Ant itself is running in. Other build tools would typically need to launch a new javac process each time Java code needed to be compiled, which would in turn require a new JVM instance. But with Ant, only a single JVM instance is required, both to run Ant itself and to perform all of the required compilation tasks (along with other related tasks, such as manipulating JAR files). This is a far more efficient use of resources and can lead to greatly improved build times.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Compiler options<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">As we saw in the previous section, the default behavior of the Ant javac task is to invoke the standard compiler of whichever JVM is being used to run Ant itself, and to invoke that compiler in-process (that is, without launching a separate JVM). However, there may be times when you want the compiler to be invoked separately &#8212; when you wish to specify certain memory options to the compiler, for instance, or when you need to use a different level of the compiler. To achieve this, simply set javac &#8216;s fork attribute to true, like this:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>And if you want to specify a different javac executable, and pass a maximum memory setting to it, you would do something like this:<\/p>\n<p><span style=\"font-family: verdana, geneva;\">memoryMaximumSize=&#8221;128m&#8221;\/&gt;<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">You can even configure Ant to use a different compiler altogether. Supported compilers include the open source Jikes compiler and the GCJ compiler from the GNU Compiler Collection (GCC). (See Resources for information on these two compilers.) These can be specified in two ways: either by setting the build.compiler property, which will apply to all uses of the javac task, or by setting the compiler attribute in each javac task as required.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Creating a JAR file<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">After Java source files have been compiled, the resulting classfiles are typically packaged into a JAR file, which is similar to a zip archive. Every JAR file contains a manifest file that can specify properties of the JAR file.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Here&#8217;s a simple use of the jar task in Ant:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>This creates a new JAR file called package.jar and adds all the files in the classes directory to it (JAR files can contain any type of file, not just classfiles). In this case, no manifest file was specified, so Ant will provide a basic one.<\/p>\n<p><span style=\"font-family: verdana, geneva;\">The manifest attribute allows you to specify a file to use as the manifest for the JAR file. The contents of a manifest file could also be specified within the build file using the manifest task. This task can write a manifest file to the filesystem, or can actually be nested inside the jar task so that the manifest file and JAR file are created all in one go. For example:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Time-stamping builds<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">It is often desirable in a build environment to use the current time and date to mark the output of a build in some way, in order to record when it was built. This might involve editing a file to insert a string to indicate the date and time, or incorporating this information into the filename of the JAR or zip file.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">This need is addressed by the simple but very useful tstamp task. This task is typically called at the start of a build, such as in an init target. No attributes are required, so just is sufficient is many cases.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The tstamp task doesn&#8217;t produce any output; instead, it sets three Ant properties based on the current system time and date. Here are the properties that tstamp sets, an explanation for each, and examples of what they might be set to:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Property Explanation Example<br \/>\nDSTAMP This is set to the current date, the default format being yyyymmdd 20031217<br \/>\nTSTAMP This is set to the current time, the default format being hhmm 1603<br \/>\nTODAY This is set to a string describing the current date, with the month written in full December 17 2003 <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">For example, in the previous section we created a JAR file as follows:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>After having called the tstamp task, we could name the JAR file according to the date, like this:<\/p>\n<p>&nbsp;<\/p>\n<p>So, if this task were invoked on December 17, 2003, the JAR file would be named package-20031217.jar.<\/p>\n<p><span style=\"font-family: verdana, geneva;\">The tstamp task can also be configured to set different properties, apply an offset before or after the current time, or format the strings differently. All of this is done using a nested format element, as follows:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><br \/>\npattern=&#8221;HH:mm:ss&#8221;<br \/>\noffset=&#8221;10&#8243; unit=&#8221;minute&#8221;\/&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">The listing above sets the OFFSET_TIME property to a time in hours, minutes, and seconds that is 10 minutes after the current time.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The characters used to define the format strings are the same as those defined by the java.text.SimpleDateFormat class. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Putting it all together<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The previous sections have given us enough knowledge to build a simple Java project. We will now assemble the code snippets into a complete build file that compiles all the source code under a src directory, puts the resulting classfiles under a build directory, and then packages everything up into a JAR file in a dist directory, ready for distribution. In order to try this out for yourself, all you need is a src directory containing one or more Java source code files &#8212; anything from a simple &#8220;Hello World&#8221; program to a large number of source files from an existing project. If you need to add JAR files or anything else to the Java classpath in order to successfully compile your source code, simply add a classpath attribute for it in the javac task.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The build file looks like this:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>A simple Java project<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>And here is some sample output from a build that was run with that file (yours may vary, depending on the contents of your src directory):<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Buildfile: build.xml<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">init:<br \/>\n[mkdir] Created dir: E:\\tutorial\\javaexample\\build<br \/>\n[mkdir] Created dir: E:\\tutorial\\javaexample\\dist<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">compile:<br \/>\n[javac] Compiling 10 source files to E:\\tutorial\\javaexample\\build<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">dist:<br \/>\n[jar] Building jar: E:\\tutorial\\javaexample\\dist\\package-20031217.jar<br \/>\n[jar] Building jar: E:\\tutorial\\javaexample\\dist\\package-src-20031217.jar<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">BUILD SUCCESSFULs<br \/>\nTotal time: 5 seconds<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Notice that the JAR file is named according to the current date, and that a manifest entry is set for the main class of the application, so that it can be launched directly with a command along the lines of java -jar package-20031217.jar. We also create a JAR file consisting of just the source code of the project.<\/span><\/p>\n<p><!--nextpage--><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 5. Filesystem operations<\/strong><\/span><\/u><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Filesystem operations introduction<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">We&#8217;ve learned enough about Ant now to be able to build a basic Java project, but naturally real-world projects are rarely as simple as our example. In the next few sections, we&#8217;ll look some of Ant&#8217;s numerous additional capabilities and the situations in which they can be used.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">In this section, we&#8217;ll see how to perform common file operations, such as creating directories and unpacking files. One of the good features of Ant is that the tasks to perform these operations generally behave the same on all platforms. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Creating and deleting directories<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">One of the most basic filesystem operations is the creation of a directory or folder. The Ant task for this is called mkdir, and it is unsurprisingly very similar to the Windows and UNIX\/Linux commands of the same name:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Note first the use of \/ as the directory separator, which is the convention for UNIX and Linux platforms. You might think that this isn&#8217;t very platform neutral, but Ant knows how to handle it, and will do the right thing for whichever platform it is running on, in the same way as we saw earlier when defining location-based properties. We could just as easily have used \\ instead, regardless of platform &#8212; Ant handles either form, or even a mixture of both.<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Another useful feature of the mkdir task is its ability to create parent directories if they don&#8217;t exist already. Consider the listing above, and imagine that the archive directory exists, but not the metals directory. If you were using the underlying platform&#8217;s mkdir commands, you would need to first explicitly create the metals directory, and then create the zinc directory using a second invocation of the mkdir command. But the Ant task is smarter than this, and will create both directories in one go. Similarly, if the target directory already exists, the mkdir task will not complain, but will just assume that its work is already done, and do nothing.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Deleting directories is just as easy:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><br \/>\nThis will delete the specified directory, along with any files and sub-directories it contains. Use the file attribute instead of dir to specify a single file to be deleted.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Copying and moving files and directories<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Making a copy of a file is simple in Ant. For example:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>You can also use move to perform a rename operation instead of copying the file:<\/p>\n<p>&nbsp;<\/p>\n<p>Another common filesystem operation is to copy or move a file into another directory. The Ant syntax for this is just as straightforward:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>By default, Ant only outputs a summary of the move and copy operations it performs, including such information as the number of files it has moved or copied. If you wish to see more detailed information, including the names of the files involved, you can set the verbose attribute to true.<br \/>\n<strong>Creating and unpacking zip and tar files<\/strong><\/p>\n<p><span style=\"font-family: verdana, geneva;\">In the previous section, we saw how to create a JAR file. The process is almost exactly the same for creating other archive files. Here is an Ant task that creates a zip file instead:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The same syntax can also be used to create tar files. Files can also be compressed using the GZip and BZip2 tasks. For example:<\/p>\n<p>&nbsp;<\/p>\n<p>Uncompressing and extracting files is just as straightforward:<\/p>\n<p>&nbsp;<\/p>\n<p>The overwrite attribute can also be included to control the overwrite behavior. The default is to overwrite any existing files with matching entries from the archive being extracted. The related task names are untar, unjar, gunzip, and bunzip2.<\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Replacing tokens in files<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The final filesystem operation we will look at in this section is the replace task, which performs search and replace operations within files. The token attribute specifies the string to be searched for, and the value attribute specifies the new string that all occurrences of the token string should be replaced with. For example:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">\u00a0<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The replacement occurs in place, within the file itself. To provide more detailed output, set the summary attribute to true. This causes the task to output the number of occurrences of the token string that were found and replaced.<\/span><\/p>\n<p><!--nextpage--><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 6. Extending Ant with custom tasks<\/strong><\/span><\/u><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Extending Ant with custom tasks introduction<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">As we&#8217;ve seen throughout the previous sections, Ant is very powerful, with a number of core tasks covering a broad set of functionality. There are a number of additional core tasks not covered here, plus a number of optional tasks providing a wide variety of additional functionality, as well as other tasks that are available as part of the Ant-Contrib project; finally, there are even more externally available tasks listed on the Apache Ant home page. Given all of this, it might seem unlikely that you&#8217;d ever need any other tasks, but the real power of Ant lies in its ease of extensibility. In fact, it is precisely this extensibility that has resulted in such a large number of additional tasks being developed.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">There can be occasions when creating your own custom task is a good idea. For example, imagine that you have created a command-line tool to perform a particular operation; this tool might be a good candidate to be made available as an Ant task (particularly if the tool was written in the Java language, although this doesn&#8217;t have to be the case). Instead of having Ant call the tool externally using the exec task (which introduces dependencies and makes it harder to use your build file across different environments), you can incorporate it directly into the build file. The regular fileset and wildcard matching capabilities of Ant can also be made available to your custom task.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">In this section, we&#8217;ll take a look at the construction of a simple custom task. This task will perform a sort operation on the lines of a file, and write the sorted set of lines out to a new file. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Creating a custom task<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">To implement a simple custom task, all we need to do is extend the org.apache.tools.ant.Task class and override the execute() method. So, as a skeleton of our file sorter custom task, we have the following:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">import org.apache.tools.ant.BuildException;<br \/>\nimport org.apache.tools.ant.Task;<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">public class FileSorter extends Task {<br \/>\n\/\/ The method executing the task<br \/>\npublic void execute() throws BuildException {}<br \/>\n}<br \/>\nNotice that the execute() method is declared to throw a BuildException. If anything goes wrong with our task, we throw this to signal the failure back to Ant.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Most tasks, core and custom alike, make use of attributes to control their behavior. For our simple task, we need an attribute to specify the file to be sorted, and another to specify the output file for the sorted contents. Let&#8217;s call these attributes file and tofile, respectively.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Ant makes it very easy to support attributes in custom tasks. We do this simply by implementing a method with a specifically formatted name that Ant can call with the values of the corresponding attribute specified in the build file. The name of the method needs to be set plus the name of the attribute, so in our case we need methods called setFile() and setTofile(). When Ant encounters an attribute setting in the build file, it looks for a method of the appropriate name, known as a setter method, in the associated task.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Attributes in the build file are specified as strings, so the argument to our setter methods can be a string. In such a case, Ant will call our method with the string value of the attribute, after expanding any properties referenced by the value. But sometimes we want to treat the value of the attribute as a different type. This is true for our sample task, where the attribute values refer to files on the filesystem rather than just arbitrary strings. We can do this very easily by declaring our method argument to be of type java.io.File. Ant will take the string value of the attribute and interpret it as a file, and then pass this to our method. If the file is specified with a relative path name, this will be converted into an absolute path name relative to the project&#8217;s base directory. Ant can perform similar conversions for other types, such as boolean s and int s. If you provide two methods with the same name but with different argument types, Ant will use the more specific one, so a file type will be preferred to a string type.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The two setter methods we need for our custom task look like this:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">\/\/ The setter for the &#8220;file&#8221; attribute<br \/>\npublic void setFile(File file) {}<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">\/\/ The setter for the &#8220;tofile&#8221; attribute<br \/>\npublic void setTofile(File tofile) {}<\/span><\/p>\n<p><strong>Implementing a custom task<\/strong><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Using the skeleton developed in the previous section, we can now complete the implementation of our simple file sorter task:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">import java.io.*;<br \/>\nimport java.util.*;<br \/>\nimport org.apache.tools.ant.BuildException;<br \/>\nimport org.apache.tools.ant.Task;<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">\/**<br \/>\n* A simple example task to sort a file<br \/>\n*\/<br \/>\npublic class FileSorter extends Task {<br \/>\nprivate File file, tofile;<\/span><\/p>\n<p>\/\/ The method executing the task<br \/>\npublic void execute() throws BuildException {<br \/>\nSystem.out.println(&#8220;Sorting file=&#8221;+file);<br \/>\ntry {<br \/>\nBufferedReader from =<br \/>\nnew BufferedReader(new FileReader(file));<br \/>\nBufferedWriter to =<br \/>\nnew BufferedWriter(new FileWriter(tofile));<br \/>\nList allLines = new ArrayList();<br \/>\n\/\/ read in the input file<br \/>\nString line = from.readLine();<br \/>\nwhile (line != null) {<br \/>\nallLines.add(line);<br \/>\nline = from.readLine();<br \/>\n}<br \/>\nfrom.close();<br \/>\n\/\/ sort the list<br \/>\nCollections.sort(allLines);<br \/>\n\/\/ write out the sorted list<br \/>\nfor (ListIterator i=allLines.listIterator(); i.hasNext(); ) {<br \/>\nString s = (String)i.next();<br \/>\nto.write(s); to.newLine();<br \/>\n}<br \/>\nto.close();<br \/>\n} catch (FileNotFoundException e) {<br \/>\nthrow new BuildException(e);<br \/>\n} catch (IOException e) {<br \/>\nthrow new BuildException(e);<br \/>\n}<br \/>\n}<\/p>\n<p><span style=\"font-family: verdana, geneva;\">\/\/ The setter for the &#8220;file&#8221; attribute<br \/>\npublic void setFile(File file) {<br \/>\nthis.file = file;<br \/>\n}<br \/>\n\/\/ The setter for the &#8220;tofile&#8221; attribute<br \/>\npublic void setTofile(File tofile) {<br \/>\nthis.tofile = tofile;<br \/>\n}<br \/>\n}<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">The two setter methods simply store the values of the attributes so they can be used in the execute() method. Here, the input file is read line by line into a list, which is then sorted and written out line by line to the output file. Note that to keep things simple we perform very little error checking &#8212; for example, we don&#8217;t even check that the required attributes have actually been set by the build file. We do at least catch any I\/O exceptions from the operations performed, and rethrow these as Ant BuildExceptions. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">We can now compile our custom task with the javac compiler, or from within an IDE. In order to resolve the Ant classes we have used, you need to add the location of the ant.jar file to your classpath. This is should be in the lib directory of your Ant installation.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Using a custom task<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Now that we&#8217;ve developed and compiled our custom task, we are in a position to make use of it from a build file.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Before we can call our custom task, we need to define it by giving it a name, and telling Ant the classfile that implements it and any classpath setting required to locate that classfile. This is done using the taskdef task, like so:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">classname=&#8221;FileSorter&#8221;<br \/>\nclasspath=&#8221;.&#8221;\/&gt;<\/span><\/p>\n<p>That&#8217;s it! The task can now be used in the same way as Ant&#8217;s core tasks. Here is a complete build file, showing the definition and use of our custom task:<\/p>\n<p>&nbsp;<\/p>\n<p>classname=&#8221;FileSorter&#8221;<br \/>\nclasspath=&#8221;.&#8221;\/&gt;<\/p>\n<p>Now, create an input.txt file in the present working directory to test the custom task. For example:<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Hello there<br \/>\nThis is a line<br \/>\nAnd here is another one<\/span><\/p>\n<p>Here is the console output after running the above build file:<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Buildfile: build.xml<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">main:<br \/>\n[filesorter] Sorting file=E:\\tutorial\\custom\\input.txt<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">BUILD SUCCESSFUL<br \/>\nTotal time: 0 seconds<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Notice that our relative pathname of input.txt gets converted into an absolute pathname in the current directory. This is because we specified the argument to the setter method to be of type java.io.File instead of java.lang.String. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Now let&#8217;s see if the task actually worked. A file called output.txt should have been created in the same directory with the following contents:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">And here is another one<br \/>\nHello there<br \/>\nThis is a line<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">You might try specifying an input file that doesn&#8217;t exist to see how a &#8220;file not found&#8221; exception is reported back to Ant.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Congratulations: you have now developed and used a custom Ant task! There are many additional aspects involved in creating more complex tasks, and Resources contains links to sources of further information in this area.<\/span><\/p>\n<p><!--nextpage--><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 7. Useful tasks and techniques<\/strong><\/span><\/u><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Useful tasks and techniques introduction<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Before looking at custom tasks, we&#8217;ll first cover some useful functionality that we haven&#8217;t already encountered. There is a great deal of functionality that comes standard with Ant, so we can only pick out a few of the most useful pieces here. Pattern matching and file selectors are powerful mechanisms that greatly enhance the capabilities of some of the tasks we&#8217;ve already seen, and chaining builds together and working with CVS repositories are two areas that have been found to be of great practical use.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Pattern matching<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">When we looked at filesystem tasks earlier, we used only individually named files and directories. However, it is often useful to perform those operations on a group of files at once &#8212; on all files in a given directory that end with . java, for instance. Just as the equivalent DOS and UNIX commands provide this sort of functionality, so does Ant. This is done using wildcard characters: *, which matches zero or more characters, and ? , which matches exactly one character. The pattern to match all files ending with .java would therefore be simply *.java. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Matching is also performed on directories. For example, the pattern src*\/*.java would match all Java files in any directories with a prefix of src. There is one additional pattern construct: **, which matches any number of directories. For example, the pattern **\/*.java would match all Java files under the current directory structure.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">You can make use of patterns with filesystem tasks in a fairly consistent way, such as with a nested fileset element. Previously, we copied a single file with this task:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>If we wanted to use a pattern instead, we could replace the file attribute with a fileset element, like this:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">The fileset would by default consist of all the files under the specified src directory, so to select just the Java files, we use the include element with a pattern. In a similar way, we could add an exclude element with another pattern, which would potentially remove matches from the include specification. We could even specify multiple include and exclude elements; this would result in a set of files and directories consisting of all the matches from the include patterns joined together, with all of the matches from the exclude elements removed.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Note that there is one feature of filesets that is usually very helpful, but occasionally leads to confusion for those who are not aware of it. This feature is known as default excludes: a built-in list of patterns that are automatically excluded from the contents of filesets. The list includes entries to match directories called CVS, and files ending with the ~ character, which may be backup files. You often don&#8217;t want to include these sorts of files and directories in filesystem operations, so that is the default behavior. However, if you really want to select all files and directories without exception, you can set the defaultexcludes attribute of your fileset to no.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Using selectors<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">As we&#8217;ve seen, a fileset is used to specify a group of files, and the contents of this group can be defined using include and exclude patterns. You can also use special elements called selectors in conjunction with the include and exclude to select files. Here is a list of the core selectors available with Ant:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">size: This is used to select files based on their size in bytes (unless the units attribute is used to specify a different unit). The when attribute is used to set the nature of the comparison &#8212; less, more, or equal &#8212; and the value attribute defines the target size against which the size of each file is compared.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">contains: Only files containing the given text string (specified by the text attribute) match this selector. By default, the search is case sensitive; adding casesensitive=&#8221;no&#8221; changes this.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">filename: The name attribute specifies the pattern to match filenames against. This is essentially the same as the include element, and the same as the exclude element when negate=&#8221;yes&#8221; is specified.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">present: Selects those files from the current directory structure that have the same name and relative directory structure as those in a specified targetdir directory.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">depend: This selector has the same effect as the present selector, except that the matching files are restricted to those that have been modified more recently than the corresponding files in the targetdir location.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">date: This selects files based on the date on which they were last modified. The when attribute specifies whether the comparison is before, after, or equal, and the datetime attribute specifies the date and time to compare against, given in a fixed format of MM\/DD\/YYYY HH:MM AM_or_PM. Note that on Windows platforms there is a built-in margin of 2 seconds either way to allow for inaccuracies in the underlying filesystem &#8212; this may cause more files to match than otherwise expected. The amount of leeway allowed can be changed with the granularity attribute (specified in milliseconds).<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">depth: This selector looks at the number of levels in the directory structure of each file. Attributes for min and\/or max are used to select files with a desired number of directory levels.<br \/>\nSelectors can also be combined together by nesting one or more selectors inside a selector container. The most common selector container, and, selects only those files that are selected by all of the selectors it encloses. Other selector containers include or, not, none, and majority. <\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Here is an example of a fileset that selects only those files that are larger than 512 bytes and also contain the string &#8220;hello&#8221;:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Chaining builds together<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">There are two different approaches to building large projects. One is to have a single monolithic build file that does everything; the other is to split the build up into a number of smaller pieces by having high-level build files that call out to other build files to perform specific tasks.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">It is easy to call one Ant build from another using the ant task. In a simple case, you can specify just the build file to use, using the antfile attribute, and Ant will build the default target in that build file. For example: <\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Any properties defined in the parent build file will by default be passed into the sub-build, although this can be avoided by specifying inheritAll=&#8221;false&#8221;. It is also possible to pass in explicit properties by using a nested property element &#8212; these still apply even if inheritAll is set to false. This facility is good for passing in parameters to sub-builds.<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Let&#8217;s consider an example. Here is a build file that we wish to call:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">(We haven&#8217;t come across the echo task before &#8212; it simply outputs the given message.)<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">And here is a second build file that calls out to the first, passing in the message property:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The output from running the second build file is as follows:<\/p>\n<p><span style=\"font-family: verdana, geneva;\">Buildfile: build.xml<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">callSub:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">showMessage:<br \/>\n[echo] Message=Hello from parent build<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">BUILD SUCCESSFUL<br \/>\nTotal time: 0 seconds<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\"><strong>Working with CVS repositories<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">CVS is an acronym for concurrent versions system. It is a source code control system that is designed to keep track of changes made by a number of different developers. It is extremely popular, particularly with open source projects. Ant provides close integration with CVS. This is very useful for automated build environments, as a single build file can extract one or more modules from the source code repository, build the project, and even generate patch files based on the changes that have been made since the previous build.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Note that in order to make use of the cvs task in Ant, you need to have the cvs command installed on your machine and available from the command line. This command is included in most Linux distributions; it is also available for Windows in several forms, &#8212; as part of the invaluable Cygwin environment, for instance. (See Resources for more on Cygwin.)<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Here is an example build file that extracts a module from a CVS repository:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>package=&#8221;org.eclipse.swt.examples&#8221;<br \/>\ndest=&#8221;${basedir}&#8221;\/&gt;<\/p>\n<p>The main attribute to the cvs task is cvsRoot, which is the complete reference to the CVS repository, including connection method and user details. The format of this parameter is:<\/p>\n<p><span style=\"font-family: verdana, geneva;\">[:method:][[user][:password]@]hostname[:[port]]\/path\/to\/repository<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana, geneva;\">In the above example, we connect to the central repository for the Eclipse project as an anonymous user. The other attributes then specify the module or package we wish to extract and the destination for the extracted files. Extraction is the default operation for the CVS task; other operations can be specified using the command attribute.<\/span><\/p>\n<p><!--nextpage--><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 8. Wrap-up<\/strong><\/span><\/u><\/p>\n<p><span style=\"font-family: verdana, geneva;\">We hope you have found this tour of Ant useful. Whilst Ant aims to be as simple and straightforward as possible, it provides a great deal of functionality through a large number of tasks, each with a number of options, which can be overwhelming at times. We haven&#8217;t been able to explore all of Ant&#8217;s functionality in this single tutorial, but we hope to have introduced all of the fundamental concepts and enough of the basic functionality to start you down the road of using Ant on real-world projects. Here is a summary of what we&#8217;ve covered: <\/span><\/p>\n<ul>\n<li><span style=\"font-family: verdana, geneva;\">How Ant build files are structured<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">How to run Ant from the command line and from Eclipse<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">How to build simple Java projects by compiling source code, creating JAR files, and time-stamping files to identify the output of each build<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">How to perform basic filesystem operations in Ant<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">The basic concepts of pattern matching and selectors, plus how to call one build from another, and how to perform CVS operations<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">How to extend Ant&#8217;s standard capabilities by writing Java classes <\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: verdana, geneva;\">To continue your Ant journey, follow some of the links on the next section, and familiarize yourself with the Ant manual, which is the definitive source of reference when writing Ant build files. Good luck!<\/span><\/p>\n<p><!--nextpage--><\/p>\n<p><u><span style=\"color: #808000; font-family: verdana, geneva;\"><strong style=\"font-size: 16px;\">Section 9. Resources<\/strong><\/span><\/u><\/p>\n<ul>\n<li><span style=\"font-family: verdana, geneva;\">Visit the <a href=\"http:\/\/ant.apache.org\/\" target=\"_blank\" rel=\"noopener\">Apache Ant home page<\/a>. It&#8217;s a great source of information, and it includes downloads, the Ant manual, and links to other resources.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">Find out more about Ant&#8217;s <a href=\"http:\/\/ant.apache.org\/external.html#IDE%20and%20Editor%20Integration\" target=\"_blank\" rel=\"noopener\">IDE and editor integration<\/a>.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">Learn more about <a href=\"http:\/\/www.eclipse.org\/\" target=\"_blank\" rel=\"noopener\">Eclipse<\/a>, a universal tool platform.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">The <a href=\"http:\/\/www.ibm.com\/developerworks\/opensource\/\" target=\"_blank\" rel=\"noopener\"><em>developerWorks<\/em> Open source projects zone<\/a> has a wealth of Eclipse-based content.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">If you&#8217;re new to XML, visit the <a href=\"http:\/\/www.ibm.com\/developerworks\/xml\/\" target=\"_blank\" rel=\"noopener\"><em>developerWorks<\/em> XML zone<\/a>.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\"><a href=\"http:\/\/www.cygwin.com\/\" target=\"_blank\" rel=\"noopener\">Cygwin<\/a> is a Linux-like environment for Windows.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\"><a href=\"http:\/\/www.cvshome.org\" target=\"_blank\" rel=\"noopener\">CVS<\/a> is the open standard for version control.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">Learn more about <a href=\"http:\/\/www-3.ibm.com\/software\/awdtools\/studioappdev\/\" target=\"_blank\" rel=\"noopener\">WebSphere Studio Application Developer<\/a>, which is based on the Eclipse platform.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">Find out more about WebSphere Application Server&#8217;s <a href=\"http:\/\/publib7b.boulder.ibm.com\/wasinfo1\/en\/info\/aes\/ae\/rovr_antapi.html\" target=\"_blank\" rel=\"noopener\">support for Ant tasks<\/a>.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">Explore the power of the JAR file format in &#8220;<a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-jar\/\" target=\"_blank\" rel=\"noopener\"> JAR files revealed<\/a> &#8221; (<em> developerWorks<\/em>, October 2003).<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\"><em>developerWorks<\/em> features a number of articles on Ant: <\/span>\n<ul>\n<li><span style=\"font-family: verdana, geneva;\">&#8220;<a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-ant\/\" target=\"_blank\" rel=\"noopener\"> Incremental development with Ant and JUnit<\/a>, &#8221; Malcolm Davis, (November 2000). <\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">&#8220;<a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-antbuild\/\" target=\"_blank\" rel=\"noopener\"> Extending Ant to support interactive builds<\/a>, &#8221; Anthony Young-Garner (November 2001). <\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">&#8220;<a href=\"http:\/\/www.ibm.com\/developerworks\/xml\/library\/x-antxsl\/\" target=\"_blank\" rel=\"noopener\"> Enhance Ant with XSL transformations<\/a>, &#8221; Jim Creasman (September 2003). <\/span><\/li>\n<\/ul>\n<\/li>\n<li><span style=\"font-family: verdana, geneva;\">Ant also supports the open source <a href=\"http:\/\/www-124.ibm.com\/developerworks\/projects\/jikes\/\" target=\"_blank\" rel=\"noopener\">Jikes compiler<\/a> and the <a href=\"http:\/\/gcc.gnu.org\/java\/\" target=\"_blank\" rel=\"noopener\">GNU Compiler for Java<\/a> (GCJ).<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">Find hundreds of articles about every aspect of Java programming in the <a href=\"http:\/\/www.ibm.com\/developerworks\/java\/\" target=\"_blank\" rel=\"noopener\"><em>developerWorks<\/em> Java technology zone<\/a>.<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva;\">Also see the <a href=\"http:\/\/www.ibm.com\/developerworks\/views\/java\/tutorials.jsp\" target=\"_blank\" rel=\"noopener\">Java technology zone tutorials page<\/a> for a complete listing of free Java-related tutorials from <em>developerWorks<\/em>. <\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: verdana, geneva;\"><strong>About the author<\/strong><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Matt Chapman is an advisory software engineer in the IBM Java Technology Centre in Hursley, U.K. He has spent the last seven years working with Java technology, including Java virtual machine implementations for a variety of platforms, the user interface toolkits Swing and AWT, and, more recently, tooling for the Eclipse platform. Matt has a degree in computer science and is also a Sun-certified Java programmer. You can contact him at mchapman@uk.ibm.com.<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva;\">Reference :<a href=\"http:\/\/www.ibm.com\/developerworks\/edu\/j-dw-java-apant-i.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.ibm.com\/developerworks\/edu\/j-dw-java-apant-i.html<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache Ant: Learn Apache Ant from IBM Experts Section 1. Getting started\u00a0 \u00a0\u00a0 What is this tutorial about? In this tutorial, you&#8217;ll learn about Ant, a build tool for Java TM projects. Ant has quickly become popular among Java developers because of its flexibility and ease of use, so you it owe it to yourself&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3994,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[8],"tags":[209,3005,1836,2997,1897,3006,3002,1837,1835,3001,3000,3003,2998,3004,100,97,2999,1682,1748,1673,3007,105,1749,98],"class_list":["post-112","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-training","tag-ant","tag-ant-coach","tag-ant-course","tag-ant-expert","tag-ant-guide","tag-ant-instructor","tag-ant-professional","tag-ant-trainer","tag-ant-training","tag-ant-training-by-expert-trainers","tag-ant-training-by-experts","tag-ant-training-by-professionals","tag-ant-tutorial","tag-apache-ant-ibm-expert","tag-hyderabad","tag-india","tag-learn-ant","tag-mumbai","tag-netherlands","tag-pune","tag-singapore","tag-trainer","tag-uk","tag-usa"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":3996,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions\/3996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3994"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}