Ant – Property Task ”; Previous Next Ant build files are written in XML, which does not allow declaring variables as you do in your favorite programming language. However, as you may have imagined, it would be useful if Ant allowed declaring variables such as project name, project source directory, etc. Ant uses the property element which allows you to specify the properties. This allows the properties to be changed from one build to another or from one environment to another. Ant Properties By default, Ant provides the following pre-defined properties that can be used in the build file − Sr.No Properties & Description 1 ant.file The full location of the build file. 2 ant.version The version of the Apache Ant installation. 3 basedir The basedir of the build, as specified in the basedir attribute of the project element. 4 ant.java.version The version of the JDK that is used by Ant. 5 ant.project.name The name of the project, as specified in the name attribute of the project element. 6 ant.project.default-target The default target of the current project. 7 ant.project.invoked-targets Comma separated list of the targets that were invoked in the current project. 8 ant.core.lib The full location of the Ant jar file. 9 ant.home The home directory of Ant installation. 10 ant.library.dir The home directory for Ant library files – typically ANT_HOME/lib folder. Ant also makes the system properties (Example: file.separator) available to the build file. In addition to the above, the user can define additional properties using the property element. The following example shows how to define a property called sitename − <?xml version=”1.0″?> <project name=”Hello World Project” default=”info”> <property name=”sitename” value=”www.tutorialspoint.com”/> <target name=”info”> <echo>Apache Ant version is ${ant.version} – You are at ${sitename} </echo> </target> </project> Output Running Ant on the above build file produces the following output − C:>ant Buildfile: C:build.xml info: [echo] Apache Ant version is Apache Ant(TM) version 1.10.12 compiled on October 13 2021 – You are at www.tutorialspoint.com BUILD SUCCESSFUL Total time: 0 seconds C:> Print Page Previous Next Advertisements ”;
Category: Ant
ANT – Build Files
Ant – Build Files ”; Previous Next Typically, Ant”s build file, called build.xml should reside in the base directory of the project. However, there is no restriction on the file name or its location. You are free to use other file names or save the build file in some other location. For this exercise, create a file called build.xml anywhere in your computer with the following contents − <?xml version=”1.0″?> <project name=”Hello World Project” default=”info”> <target name=”info”> <echo>Hello World – Welcome to Apache Ant!</echo> </target> </project> Note that there should be no blank line(s) or whitespace(s) before the xml declaration. If you allow them, the following error message occurs while executing the ant build − The processing instruction target matching “[xX][mM][lL]” is not allowed. All build files require the project element and at least one target element. The XML element project has three attributes which are as follows − Sr.No Attributes & Description 1 name The Name of the project. (Optional) 2 default The default target for the build script. A project may contain any number of targets. This attribute specifies which target should be considered as the default. (Mandatory) 3 basedir The base directory (or) the root folder for the project. (Optional) A target is a collection of tasks that you want to run as one unit. In our example, we have a simple target to provide an informational message to the user. Targets can have dependencies on other targets. For example, a deploy target may have a dependency on the package target, the package target may have a dependency on the compile target and so forth. Dependencies are denoted using the depends attribute. For example − <target name=”deploy” depends=”package”> …. </target> <target name=”package” depends=”clean,compile”> …. </target> <target name=”clean” > …. </target> <target name=”compile” > …. </target> The target element has the following attributes − Sr.No Attributes & Description 1 name The name of the target (Required) 2 depends Comma separated list of all targets that this target depends on. (Optional) 3 description A short description of the target. (optional) 4 if Allows the execution of a target based on the trueness of a conditional attribute. (optional) 5 unless Adds the target to the dependency list of the specified Extension Point. An Extension Point is similar to a target, but it does not have any tasks. (Optional) The echo task in the above example is a trivial task that prints a message. In our example,it prints the message Hello World. To run the ant build file, open up command prompt and navigate to the folder, where the build.xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file. You should see the following output − C:>ant Buildfile: C:build.xml info: [echo] Hello World – Welcome to Apache Ant! BUILD SUCCESSFUL Total time: 0 seconds C:> Print Page Previous Next Advertisements ”;
ANT – Environment Setup
Ant – Environment Setup ”; Previous Next Apache Ant is distributed under the Apache Software License which is a fully-fledged open source license certified by the open source initiative. The latest Apache Ant version, including its full-source code, class files, and documentation can be found at https://ant.apache.org. Installing Apache Ant It is assumed that you have already downloaded and installed Java Development Kit (JDK) on your computer. If not, please follow the instructions available at file:///C:/java/java_environment_setup.htm Ensure that the JAVA_HOME environment variable is set to the folder, where your JDK is installed. Download the binaries from https://ant.apache.org Unzip the zip file to a convenient location c:folder by using Winzip, winRAR, 7-zip or similar tools. Create a new environment variable called ANT_HOME that points to the Ant installation folder. In this case, it is c:apache-ant-1.10.12-bin folder. Append the path to the Apache Ant batch file to the PATH environment variable. In our case, this would be the c:apache-ant-1.10.12-binbin folder. Verifying the Installation To verify the successful installation of Apache Ant on your computer, type ant on your command prompt. You should see an output as given below − C:>ant -version Apache Ant(TM) version 1.10.12 compiled on October 13 2021 If you do not see the above output, then please verify that you have followed the installation steps properly. Installing Eclipse This tutorial also covers integration of Ant with Eclipse integrated development environment (IDE). Hence, if you have not installed Eclipse, please download and install Eclipse. Steps to install Eclipse Download the latest Eclipse binaries from www.eclipse.org Unzip the Eclipse binaries to a convenient location, say c:folder. Run Eclipse from c:eclipseeclipse.exe. Print Page Previous Next Advertisements ”;