Ant – Using Token Filter ”; Previous Next Ant Filter allows to set a token filter for current project. A token is seperated by @ symbol and can be read using properties file as well. Steps Step 1 − Define a token using @@. This is a sample text written in @year@. Step 2 − Set the filter. <filter token=”year” value=”2021″/> Step 3 − Use the filter. All tasks will replace the occurence of @year@ with 2021. <copy todir=”${dest.dir}” filtering=”true”> <fileset dir=”${src.dir}”/> </copy> Filter Task Properties Following are the key attributes − Sr.No Attribute & Description 1 token the token string without the separator chars (@) 2 value the string that should be put to replace the token when the file is copied. 3 filtersfile The file from which the filters must be read. This file must be a formatted as a property file. Either token and value to be provided or filtersfile to Filter task to work properly. Example Create a src folder with text1.txt file with following contents − This is a sample text written in @year@. 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”/> <target name=”copy”> <filter token=”year” value=”2021″/> <copy todir=”${dest.dir}” filtering=”true”> <fileset dir=”${src.dir}”/> </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: 1 second F:tutorialspointant> Verify the content of copied file to build folder. This is a sample text written in 2021. Print Page Previous Next Advertisements ”;
Category: Ant
ANT – Property Files
Ant – Property Files ”; Previous Next Setting properties directly in the build file is fine, if you are working with a handful of properties. However, for a large project, it makes sense to store the properties in a separate property file. Benefits Storing the properties in a separate file offers the following benefits − It allows you to reuse the same build file, with different property settings for different execution environment. For example, build properties file can be maintained separately for DEV, TEST, and PROD environments. It is useful, when you do not know the values for a property (in a particular environment) up-front. This allows you to perform the build in other environments, where the property value is known. There is no hard and fast rule, but typically the property file is named as build.properties and is placed along-side the build.xml file. You could create multiple build properties files based on the deployment environments – such as build.properties.dev and build.properties.test. The contents of the build property file are similar to the normal java property file. They contain one property per line. Each property is represented by a name and a value pair. The name and value pairs are separated by an equals (=) sign. It is highly recommended that the properties are annotated with proper comments. Comments are listed using the hash (#) character. The following example shows a build.xml file and its associated build.properties file − build.xml Given below is an example for build.xml file. <?xml version=”1.0″?> <project name=”Hello World Project” default=”info”> <property file=”build.properties”/> <target name=”info”> <echo>Apache Ant version is ${ant.version} – You are at ${sitename} </echo> </target> </project> build.properties An example for build.properties file is mentioned below − # The Site Name sitename=www.tutorialspoint.com buildversion=3.3.2 In the above example, sitename is a custom property which is mapped to the website name. You can declare any number of custom properties in this fashion. Another custom property listed in the above example is the buildversion, which, in this instance, refers to the version of the build. In addition to the above, Ant comes with a number of predefined build properties, which are listed in the previous section, but is given below once again for your reference. 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. The example presented in this chapter uses the ant.version built-in property. Print Page Previous Next Advertisements ”;
ANT – Executing Java code
Ant – Executing Java code ”; Previous Next You can use Ant to execute the Java code. In the following example, the java class takes in an argument (administrator”s email address) and send out an email. public class NotifyAdministrator { public static void main(String[] args) { String email = args[0]; notifyAdministratorviaEmail(email); System.out.println(“Administrator “+email+” has been notified”); } public static void notifyAdministratorviaEmail(String email { //…… } } Here is a simple build that executes this java class. <?xml version=”1.0″?> <project name=”sample” basedir=”.” default=”notify”> <target name=”notify”> <java fork=”true” failonerror=”yes” classname=”NotifyAdministrator”> <arg line=”[email protected]”/> </java> </target> </project> When the build is executed, it produces the following outcome − C:>ant Buildfile: C:build.xml notify: [java] Administrator [email protected] has been notified BUILD SUCCESSFUL Total time: 1 second In this example, the java code does a simple thing which is, to send an email. We could have used the built in the Ant task to do that. However, now that you have got the idea, you can extend your build file to call the java code that performs complicated things. For example: encrypts your source code. Print Page Previous Next Advertisements ”;
ANT – Eclipse Integration
Ant – Eclipse Integration ”; Previous Next If you have downloaded and installed Eclipse already, you have very little to do to get started. Eclipse comes pre bundled with the Ant plugin, ready to use. Follow the simple steps, to integrate Ant into Eclipse. Make sure that the build.xml is a part of your java project, and does not reside at a location that is external to the project. Enable Ant View by following Window → Show View → Other → Ant → Ant. Open Project Explorer, drag the build.xml into the Ant View. Your Ant view looks similar to the one given below − Clicking on the targets, build / clean / usage will run Ant with the target. Clicking “fax” will execute the default target – usage. The Ant Eclipse plugin also comes with a good editor for editing build.xml files. The editor is aware of the build.xml schema and can assist you with code completion. To use the Ant editor, right click your build.xml (from the Project Explorer) and select Open with > Ant Editor. The Ant editor should look something similar to− The Ant editor lists the targets on the right hand side. The target list serves as a bookmark that allows you to jump straight into editing a particular target. Print Page Previous Next Advertisements ”;
ANT – Deploying Applications
Ant – Deploying Applications ”; Previous Next In the previous chapter, we have learnt how to package an application and deploy it to a folder. In this chapter, we are going to deploy the web application directly to the application server deploy folder and then, we are going to add a few Ant targets to start and stop the services. Let us continue with the Hello World fax web application. This is a continuation of the previous chapter; the new components are highlighted in bold. build.properties The file for build.properties is given below − # Ant properties for building the springapp appserver.home=c:\install\apache-tomcat-7.0.19 # for Tomcat 5 use $appserver.home}/server/lib # for Tomcat 6 use $appserver.home}/lib appserver.lib=${appserver.home}/lib deploy.path=${appserver.home}/webapps tomcat.manager.url=https://www.tutorialspoint.com:8080/manager tomcat.manager.username=tutorialspoint tomcat.manager.password=secret build.xml The file for build.xml is as follows − <?xml version=”1.0″?> <project name=”fax” basedir=”.” default=”usage”> <property file=”build.properties”/> <property name=”src.dir” value=”src”/> <property name=”web.dir” value=”war”/> <property name=”javadoc.dir” value=”doc”/> <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=”javadoc”> <javadoc packagenames=”faxapp.*” sourcepath=”${src.dir}” destdir=”doc” version=”true” windowtitle=”Fax Application”> <doctitle><![CDATA[<h1>= Fax Application=</h1>]]></doctitle> <bottom><![CDATA[Copyright © 2011. All Rights Reserved.]]></bottom> <group title=”util packages” packages=”faxapp.util.*”/> <group title=”web packages” packages=”faxapp.web.*”/> <group title=”data packages” packages=”faxapp.entity.*:faxapp.dao.*”/> </javadoc> </target> <target name=”usage”> <echo message=””/> <echo message=”${name} build file”/> <echo message=”———————————–“/> <echo message=””/> <echo message=”Available targets are:”/> <echo message=””/> <echo message=”deploy –> Deploy application as directory”/> <echo message=”deploywar –> Deploy application as a WAR file”/> <echo message=””/> </target> <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> <target name=”deploy” depends=”build” description=”Deploy application”> <copy todir=”${deploy.path}/${name}” preservelastmodified=”true”> <fileset dir=”${web.dir}”> <include name=”**/*.*”/> </fileset> </copy> </target> <target name=”deploywar” depends=”build” description=”Deploy application as a WAR file”> <war destfile=”${name}.war” webxml=”${web.dir}/WEB-INF/web.xml”> <fileset dir=”${web.dir}”> <include name=”**/*.*”/> </fileset> </war> <copy todir=”${deploy.path}” preservelastmodified=”true”> <fileset dir=”.”> <include name=”*.war”/> </fileset> </copy> </target> <target name=”clean” description=”Clean output directories”> <delete> <fileset dir=”${build.dir}”> <include name=”**/*.class”/> </fileset> </delete> </target> <!– ============================================================ –> <!– Tomcat tasks –> <!– ============================================================ –> <path id=”catalina-ant-classpath”> <!– We need the Catalina jars for Tomcat –> <!– * for other app servers – check the docs –> <fileset dir=”${appserver.lib}”> <include name=”catalina-ant.jar”/> </fileset> </path> <taskdef name=”install” classname=”org.apache.catalina.ant.InstallTask”> <classpath refid=”catalina-ant-classpath”/> </taskdef> <taskdef name=”reload” classname=”org.apache.catalina.ant.ReloadTask”> <classpath refid=”catalina-ant-classpath”/> </taskdef> <taskdef name=”list” classname=”org.apache.catalina.ant.ListTask”> <classpath refid=”catalina-ant-classpath”/> </taskdef> <taskdef name=”start” classname=”org.apache.catalina.ant.StartTask”> <classpath refid=”catalina-ant-classpath”/> </taskdef> <taskdef name=”stop” classname=”org.apache.catalina.ant.StopTask”> <classpath refid=”catalina-ant-classpath”/> </taskdef> <target name=”reload” description=”Reload application in Tomcat”> <reload url=”${tomcat.manager.url}”username=”${tomcat.manager.username}” password=”${tomcat.manager.password}” path=”/${name}”/> </target> </project> In this example, we have used Tomcat as our application server. First, in the build properties file, we have defined some additional properties which are explained below − The appserver.home points to the installation path to the Tomcat application server. The appserver.lib points to the library files in the Tomcat installation folder. The deploy.path variable now points to the webapp folder in Tomcat. Applications in Tomcat can be stopped and started using the Tomcat manager application. The URL for the manager application, username and password are also specified in the build.properties file. Next, we declare a new CLASSPATH that contains the catalina-ant.jar. This jar file is required to execute Tomcat tasks through Apache Ant. Tasks The catalina-ant.jar provides the following tasks − Sr.No Properties & Description 1 InstallTask Installs a web application. Class Name: org.apache.catalina.ant.InstallTask 2 ReloadTask Reload a web application. Class Name: org.apache.catalina.ant.ReloadTask 3 ListTask Lists all web applications. Class Name: org.apache.catalina.ant.ListTask 4 StartTask1 Starts a web application. Class Name: org.apache.catalina.ant.StartTask 5 StopTask Stops a web application. Class Name: org.apache.catalina.ant.StopTask 6 ReloadTask Reloads a web application without stopping. Class Name: org.apache.catalina.ant.ReloadTask The reload task requires the additional parameters which are as follows − URL to the manager application. Username to restart the web application. Password to restart the web application. Name of the web application to be restarted. Let us issue the deploy-war command to copy the webapp to the Tomcat webapps folder and then, let us reload the Fax Web application. The following outcome is the result of running the Ant file − C:>ant deploy-war Buildfile: C:build.xml BUILD SUCCESSFUL Total time: 6.3 seconds C:>ant reload Buildfile: C:build.xml BUILD SUCCESSFUL Total time: 3.1 seconds Once the above task is run, the web application is deployed and the web application is reloaded. Print Page Previous Next Advertisements ”;
ANT – Data Types
Ant – Data Types ”; Previous Next Ant provides a number of predefined data types. Do not confuse the term “data types” with those that are available in the programming language. Instead, consider them as a set of services that are built into the product already. Data Types in Ant The following data types are provided by Apache Ant. Fileset The fileset data type represents a collection of files. It is used as a filter to include or exclude files that match a particular pattern. For example, refer the following code. Here, the src attribute points to the source folder of the project. <fileset dir=”${src}” casesensitive=”yes”> <include name=”**/*.java”/> <exclude name=”**/*Stub*”/> </fileset> The fileset selects all .java files in the source folder except those contain the word ”Stub”.The case-sensitive filter is applied to the fileset, which means a file with the name Samplestub.java will not be excluded from the fileset. Pattern set A pattern set is a pattern that allows to filter files or folders easily based on certain patterns. The patterns can be created using the following meta characters − ? – Matches one character only. – Matches zero or many characters. ** – Matches zero or many directories recursively. The following example depicts the usage of a pattern set. <patternset id=”java.files.without.stubs”> <include name=”src/**/*.java”/> <exclude name=”src/**/*Stub*”/> </patternset> The patternset can then be reused with a fileset as follows − <fileset dir=”${src}” casesensitive=”yes”> <patternset refid=”java.files.without.stubs”/> </fileset> File list The filelist data type is similar to the file set except the following differences − It contains explicitly named lists of files and it does not support wild cards. This data type can be applied for existing or non-existing files. Let us see the following example of the filelist data type. Here, the attribute webapp.src.folder points to the web application source folder of the project. <filelist id=”config.files” dir=”${webapp.src.folder}”> <file name=”applicationConfig.xml”/> <file name=”faces-config.xml”/> <file name=”web.xml”/> <file name=”portlet.xml”/> </filelist> Filter set By using a filterset data type along with the copy task, you can replace certain text in all the files that matches the pattern with a replacement value. A common example is to append the version number to the release notes file, as shown in the following code. <copy todir=”${output.dir}”> <fileset dir=”${releasenotes.dir}” includes=”**/*.txt”/> <filterset> <filter token=”VERSION” value=”${current.version}”/> </filterset> </copy> In the above mentioned code − The attribute output.dir points to the output folder of the project. The attribute releasenotes.dir points to the release notes folder of the project. The attribute current.version points to the current version folder of the project. The copy task, as the name suggests, is used to copy files from one location to another. Path The path data type is commonly used to represent a class-path. Entries in the path are separated using semicolons or colons. However, these characters are replaced at the runtime by the executing system”s path separator character. The classpath is set to the list of jar files and classes in the project, as shown in the example below. <path id=”build.classpath.jar”> <pathelement path=”${env.J2EE_HOME}/${j2ee.jar}”/> <fileset dir=”lib”> <include name=”**/*.jar”/> </fileset> </path> In the code given above − The attribute env.J2EE_HOME points to the environment variable J2EE_HOME. The attribute j2ee.jar points to the name of the J2EE jar file in the J2EE base folder. 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 ”;
ANT – Home
Apache ANT Tutorial PDF Version Quick Guide Resources Job Search Discussion Apache ANT is a Java based build tool from Apache Software Foundation. Apache ANT’s build files are written in XML and they take advantage of being open standard, portable and easy to understand. This tutorial will teach you how to use Apache ANT to automate the build and deployment process in simple and easy steps. After completing this tutorial, you will find yourself at a moderate level of expertise in using Apache ANT from where, you can take yourself to next levels. Audience This tutorial is prepared for the beginners to help them understand basic functionality of Apache ANT tool to automate the build and deployment process. Prerequisites We assume that you have knowledge about the software development using any programming language, especially Java, and are aware about the software build and deployment process. Print Page Previous Next Advertisements ”;
ANT – Build Documentation
Ant – Build Documentation ”; Previous Next Documentation is a must in any project. Documentation plays a great role in the maintenance of a project. Java makes documentation easier by the use of the inbuilt javadoc tool. Ant makes it even easier by generating the documentation on demand. As you know, the javadoc tool is highly flexible and allows a number of configuration options. Ant exposes these configuration options via the javadoc task. If you are unfamiliar with javadocs, we suggest that you start with this Java Documentation Tutorial. The following section lists the most commonly used javadoc options that are used in Ant. Attributes Source can be specified using sourcepath, sourcepathref or sourcefiles. sourcepath is used to point to the folder of the source files (e.g. src folder). sourcepathref is used to refer a path that is referenced by the path attribute (e.g, delegates.src.dir). sourcefiles is used when you want to specify the individual files as a comma separated list. Destination path is specified using the destdir folder (e.g build.dir). You could filter the javadoc task by specifying the package names which are to be included. This is achieved by using the packagenames attribute, a comma separated list of package files. You could filter the javadoc process to show only the public, private, package, or protected classes and members. This is achieved by using the private, public, package and protected attributes. You could also tell the javadoc task to include the author and version information by using the respective attributes. You could also group the packages together using the group attribute, so that it becomes easy to navigate. Putting it all together Let us continue our theme of the Hello world Fax application and add a documentation target to our Fax application project. Given below is an example javadoc task used in our project. In this example, we have specified the javadoc to use the src.dir as the source directory, and doc as the target. We have also customised the window title, the header, and the footer information that appear on the java documentation pages. Also, we have created three groups − one for the utility classes in our source folder, one for the user interfaces classes, and one for the database related classes. You may notice that the data package group has two packages -– faxapp.entity and faxapp.dao. <target name=”generate-javadoc”> <javadoc packagenames=”faxapp.*” sourcepath=”${src.dir}” destdir=”doc” version=”true” windowtitle=”Fax Application”> <doctitle><![CDATA[= Fax Application =]]></doctitle> <bottom> <![CDATA[Copyright © 2011. All Rights Reserved.]]> </bottom> <group title=”util packages” packages=”faxapp.util.*”/> <group title=”web packages” packages=”faxapp.web.*”/> <group title=”data packages” packages=”faxapp.entity.*:faxapp.dao.*”/> </javadoc> <echo message=”java doc has been generated!” /> </target> Let us execute the javadoc Ant task. It generates and places the java documentation files in the doc folder. When the javadoc target is executed, it produces the following outcome − C:>ant generate-javadoc Buildfile: C:build.xml java doc has been generated! BUILD SUCCESSFUL Total time: 10.63 second The java documentation files are now present in the doc folder. Typically, the javadoc files are generated as a part of the release or package targets. Print Page Previous Next Advertisements ”;
ANT – Creating JAR files
Ant – Creating JAR files ”; Previous Next The next logical step after compiling your java source files, is to build the java archive, i.e., the Java Archive (JAR) file. Creating JAR files with Ant is quite easy with the jar task. Attributes The commonly used attributes of the jar task are as follows − Sr.No Attributes & Description 1 basedir The base directory for the output JAR file. By default, this is set to the base directory of the project. 2 compress Advises Ant to compress the file as it creates the JAR file. 3 keepcompression While the compress attribute is applicable to the individual files, the keepcompression attribute does the same thing, but it applies to the entire archive. 4 destfile The name of the output JAR file. 5 duplicate Advises Ant on what to do when duplicate files are found. You could add, preserve, or fail the duplicate files. 6 excludes Advises Ant to not include these comma separated list of files in the package. 7 excludesfile Same as above, except the exclude files are specified using a pattern. 8 inlcudes Inverse of excludes. 9 includesfile Inverse of excludesfile. 10 update Advises Ant to overwrite files in the already built JAR file. Continuing our Hello World Fax Application project, let us add a new target to produce the jar files. But before that, let us consider the jar task given below. <jar destfile=”${web.dir}/lib/util.jar” basedir=”${build.dir}/classes” includes=”faxapp/util/**” excludes=”**/Test.class” /> Here, the web.dir property points to the path of the web source files. In our case, this is where the util.jar will be placed. The build.dir property in this example, points to the build folder, where the class files for the util.jar can be found. In this example, we create a jar file called util.jar using the classes from the faxapp.util.* package. However, we are excluding the classes that end with the name Test. The output jar file will be placed in the web application lib folder. If we want to make the util.jar an executable jar file, we need to add the manifest with the Main-Class meta attribute. Therefore, the above example will be updated as follows − <jar destfile=”${web.dir}/lib/util.jar” basedir=”${build.dir}/classes” includes=”faxapp/util/**” excludes=”**/Test.class” class=”ts” <manifest class=”ts” <attribute name=”Main-Class” value=”com.tutorialspoint.util.FaxUtil”/> </manifest class=”ts” </jar class=”ts” To execute the jar task, wrap it inside a target, most commonly, the build or package target, and execute them. <target name=”build-jar” class=”ts” <jar destfile=”${web.dir}/lib/util.jar” basedir=”${build.dir}/classes” includes=”faxapp/util/**” excludes=”**/Test.class” class=”ts” <manifest class=”ts” <attribute name=”Main-Class” value=”com.tutorialspoint.util.FaxUtil”/> </manifest class=”ts” </jar class=”ts” </target class=”ts” Running Ant on this file creates the util.jar file for us. The following outcome is the result of running the Ant file − C: class=”ts”ant build-jar Buildfile: C:build.xml BUILD SUCCESSFUL Total time: 1.3 seconds The util.jar file is now placed in the output folder. Print Page Previous Next Advertisements ”;