Ant
From BC$ MobileTV Wiki
Ant is a build tool for Java application development which uses XML configuration files to define how a project gets built (i.e. generate .class files from .java), deployed (i.e. to Tomcat or Jetty server), run (i.e. auto-start, insert run-time arguments, etc) and managed over its lifecycle (i.e. startup and shutdown routines, default data, testing, etc).
According to Ant's original author, James Duncan Davidson, the name is an acronym for "Another Neat Tool", but later took on a deeper meaning related to the ability of ants to carry many times their actual weight and perform a large number of tasks.[1]
Specification
- Ant 2.x Tasks: http://ant.apache.org/manual/tasklist.html
Build File
The following is a sample ANT build.xml file:
<project name="PROJECT-NAME" default="all" basedir="."> <property file="${basedir}/build.properties"/> <property name="project.version" value="1.0"/> <property name="project.implementation.title" value="PROJECT TITLE"/> <property name="project.specification.title" value="PROJECT SPECIFICATION TITLE"/> <property name="project.main.class" value="PACKAGE.CmdLine"/> <property name="global.vendor.name" value="COMPANY"/> <property name="project.structure.dist" value="${basedir}/dist"/> <property name="project.structure.src" value="${basedir}/src"/> <property name="project.structure.conf" value="${basedir}/src/conf"/> <property name="project.structure.test" value="${basedir}/test"/> <property name="project.structure.build" value="${basedir}/build"/> <property name="project.structure.classes.src" value="${project.structure.build}/classes/src"/> <property name="project.structure.classes.test" value="${project.structure.build}/classes/test"/> <property name="project.structure.temp" value="${project.structure.build}/temp"/> <property name="version.java.jvm" value="1.5"/> <property name="project.jar" value="${ant.project.name}-${project.version}.jar"/> <property name="project.structure.lib" value="${basedir}/lib"/> <property name="commons-cli.jar" value="commons-cli-1.1.jar"/> <property name="one-jar-ant-task.jar" value="one-jar-ant-task-0.96.jar"/> <echo message="project.version = ${project.version}"/> <path id="javac.classpath"> <fileset dir="${project.structure.lib}"> <include name="${commons-cli.jar}"/> </fileset> </path> <target name="clean" description="Clean build directory"> <echo message="common.clean"/> <delete dir="${project.structure.build}"/> <delete dir="${project.structure.dist}"/> </target> <target name="init" depends="clean" description="init"> <echo message="common.init"/> <delete dir="${project.structure.temp}"/> <mkdir dir="${project.structure.dist}"/> <mkdir dir="${project.structure.build}"/> <mkdir dir="${project.structure.classes.src}"/> <mkdir dir="${project.structure.classes.test}"/> <mkdir dir="${project.structure.temp}"/> <tstamp> <format property="build.timestamp" pattern="yyyy-MM-dd'T'HH:mm:ss" /> </tstamp> <echoproperties destfile="${project.structure.temp}/build.properties"/> </target> <target name="compile" depends="init" description="Compile project"> <echo message="common.compile"/> <javac classpathref="javac.classpath" srcdir="${project.structure.src}" destdir="${project.structure.build}" debug="true" failonerror="true" deprecation="true" target="${version.java.jvm}" /> </target> <target name="create-manifest" depends="init"> <delete file="${project.structure.temp}/MANIFEST.MF"/> <manifest file="${project.structure.temp}/MANIFEST.MF"> <attribute name="Built-By" value="${user.name}"/> <attribute name="Built-On" value="${os.name}"/> <section name="${ant.project.name}"> <attribute name="Specification-Title" value="${project.specification.title}"/> <attribute name="Specification-Version" value="${project.version}"/> <attribute name="Specification-Vendor" value="${global.vendor.name}"/> <attribute name="Implementation-Title" value="${project.implementation.title}"/> <attribute name="Implementation-Version" value="${build.timestamp}"/> <attribute name="Implementation-Vendor" value="${global.vendor.name}"/> </section> </manifest> </target> <target name="manifest-add-main" if="project.main.class"> <manifest file="${project.structure.temp}/MANIFEST.MF" mode="update"> <attribute name="Main-Class" value="${project.main.class}"/> </manifest> </target> <target name="jar" depends="compile, create-manifest, manifest-add-main"> <echo message="Building jar ${project.structure.dist}/${project.jar}"/> <delete file="${project.structure.dist}/${project.jar}"/> <jar jarfile="${project.structure.dist}/${project.jar}" basedir="${project.structure.build}" manifest="${project.structure.temp}/MANIFEST.MF" excludes="**/*Test.class, **/*Test$*.class" /> </target> <taskdef name="one-jar" classname="com.simontuffs.onejar.ant.OneJarTask" classpath="${project.structure.lib}/${one-jar-ant-task.jar}" onerror="report"/> <target name="one-jar" depends="jar" description="package sitebuilder to run as a standalone jar"> <one-jar destfile="${project.structure.dist}/${ant.project.name}-cmdline-${project.version}.jar" manifest="${project.structure.conf}/${ant.project.name}.mf"> <main> <fileset dir="${project.structure.build}"/> </main> <lib> <fileset dir="${project.structure.lib}"> <include name="${commons-cli.jar}"/> </fileset> </lib> </one-jar> </target> <target name="all" depends="package"/> <target name="package" depends="one-jar"> <zip destfile="${basedir}/dist/${ant.project.name}-${project.version}.zip" basedir="${basedir}/.." includes="${ant.project.name}/**" /> <tar destfile="${basedir}/dist/${ant.project.name}-${project.version}.tar.gz" compression="gzip" basedir="${basedir}/.." includes="${ant.project.name}/**" /> </target> <pathconvert pathsep="${line.separator}| |-- " property="echo.path.compile" refid="javac.classpath"> </pathconvert> <target name="debug-classpath" description="Pretty print classpath for debugging" > <echo message="|-- compile classpath"/> <echo message="| |"/> <echo message="| |-- ${echo.path.compile}"/> </target> <target name="dp" description="Debug Print"> <echo message="${ant.project.name}"/> <echo message="${project.structure.lib}"/> <echo message="${commons-cli.jar}" /> <echo message="${one-jar-ant-task.jar}"/> </target> </project>
Ivy
- Apache - Ivy: https://ant.apache.org/ivy/ (now officially a sub-project of ANT, billed as the "Agile" Dependency Management add-on for ANT)
- wikipedia: Ivy
Resources
- Apache 2.x Manual: http://ant.apache.org/manual/
- Passing Command-Line Arguments into ANT: http://ant.apache.org/faq.html#passing-cli-args
- ANT Hello World: http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
- "javac" task: http://ant.apache.org/manual/Tasks/javac.html (compile project)
- "java" task: http://ant.apache.org/manual/Tasks/java.html (run a class)
- "jar" task: http://ant.apache.org/manual/Tasks/jar.html (package project as JAR)
- "manifestclasspath" task: http://ant.apache.org/manual/Tasks/manifestclasspath.html (convert classpath to MANIFEST.mf format)
- "manifest" task: http://ant.apache.org/manual/Tasks/manifest.html (create )
- "mkdir" task: http://ant.apache.org/manual/Tasks/mkdir.html (make a directory)
- "move" task: http://ant.apache.org/manual/Tasks/move.html (move file between directories, removing original file/location)
- "copy" task: http://ant.apache.org/manual/Tasks/copy.html (copy file from one directory to another, leaving original intact)
- "delete" task: http://ant.apache.org/manual/Tasks/delete.html (delete single file or a directory and all its files/sub-directories)
- "junit" task: http://ant.apache.org/manual/Tasks/junit.html (run unit tests)
- "condition" task: http://ant.apache.org/manual/Tasks/condition.html (perform a call within the build script if a condition is met, or not)
- "input" task: http://ant.apache.org/manual/Tasks/input.html (request user input at runtime, halt build/target execution until received)
- "property" task: http://ant.apache.org/manual/Tasks/property.html (sets a property or group of properties so that it/they can be accessed using ${property.name} notation)
- "loadProperties" task: https://ant.apache.org/manual/Tasks/loadproperties.html (loads a properties file or JNDI resource, same as <property file|resource="..."/>)
- "replace" task: http://ant.apache.org/manual/Tasks/replace.html (find and replace text/key in a file or set of files with desired text/key-value)
- "replaceregexp" task: http://ant.apache.org/manual/Tasks/replaceregexp.html (find and replace text in a file or set of files with desired text using Regular Expressions)
- "antcall" task: http://ant.apache.org/manual/Tasks/antcall.html (call another Ant target within the same buildfile)
- "ant" task: http://ant.apache.org/manual/Tasks/ant.html (run Ant on a specified buildfile)
- "cvs" task: http://ant.apache.org/manual/Tasks/cvs.html (checkout files from CVS)
- "svn-ant" plugin and task: http://subclipse.tigris.org/svnant.html (checkout files from SVN)[2]
- JGIT plugin for Ant: http://aniszczyk.org/2011/05/12/git-ant-tasks-via-jgit/ (checkout files from GIT)[3][4][5]
- "exec" task: http://ant.apache.org/manual/Tasks/exec.html (executes a system command in command-line terminal, potentially not platform-independent)
Tutorials
- Using Apache Ant: http://ant.apache.org/manual/using.html
- Apache Ant - Tutorial: http://www.vogella.com/articles/ApacheAnt/article.html
- ANT Tutorial: http://supportweb.cs.bham.ac.uk/docs/tutorials/docsystem/build/tutorials/ant/ant.html
- How To Use Ant: http://www.factorypattern.com/how-to-use-ant/
- Creating a Distribution Package of Your Java Application: http://web.archive.org/web/20070430115222/http://www.javazing.com/2007/04/05/creating-a-distribution-package-of-your-java-application/
- Creating a Java build specification: http://www.klocwork.com/products/documentation/current/Creating_a_Java_build_specification
- Use Ant for build scripts: http://www.javapractices.com/topic/TopicAction.do?Id=135
- Writing Tasks for ANT Build scripts: http://ant.apache.org/manual/tutorial-writing-tasks.html
- Project Inter-Dependencies Using Ant: http://www.exubero.com/ant/dependencies.html
- Packaging external jars in Ant: http://www.knowledgetip.com/index.php/home/software-development/1-java/42-externaljarsant
- Make ANT easy with Eclipse: http://www.ibm.com/developerworks/opensource/tutorials/os-ecl-easyant/
- ant, jar files, and Class-Path oh my: http://stackoverflow.com/questions/1190646/ant-jar-files-and-class-path-oh-my
- Classpath settings in ANT task: http://middlewaremagic.com/weblogic/?p=698
- Ant - Debugging Classpaths: http://www.javalobby.org/java/forums/t71033.html
- How to use fileset for classpath javac argument in ANT?: http://www.coderanch.com/t/106927/tools/fileset-classpath-javac-argument-ANT
- Creating a jar File in Command Prompt or Shell Terminal: http://www.skylit.com/javamethods/faqs/createjar.html
- How to create a runnable JAR in Ant? The classpath Problem: http://thinkitdifferently.wordpress.com/2009/02/18/jar-classpath/
- Setting the current working directory for an Ant Java task (won't set for all Ant tasks, just a specific one): http://shaneosullivan.wordpress.com/2007/01/03/setting-the-working-directory-for-an-ant-java-task/
- Include all jars in the classpath definition: http://www.rgagnon.com/javadetails/java-0587.html
External Links
- wikipedia: Apache Ant
- Apache ANT: http://ant.apache.org/ | DOWNLOAD
- wikipedia: Apache Ant
- 10 Minutes Guide to Ant: http://www.roseindia.net/jboss/10_minutes_guide_to_ant.shtml
- Automate your build process using Java and Ant: http://www.javaworld.com/jw-10-2000/jw-1020-ant.html
- NetBeans - deploying all in one ".jar": https://stackoverflow.com/questions/2212913/netbeans-deploying-all-in-one-jar
- How to Add Version Information to a Jar File with Netbeans: www.javaxt.com/Tutorials/Netbeans/How_to_Add_Version_Information_to_a_Jar_File_with_Netbeans
- What Is Ant, Really?: https://dzone.com/articles/what-is-ant-really?edition=702399
References
- ↑ Why do you call it ANT?: http://ant.apache.org/faq.html#ant-name
- ↑ How to Use SVN Tasks with ANT: http://java.dzone.com/articles/how-use-svn-tasks-with-ant
- ↑ Define your own Ant Tasks for GIT: http://tlrobinson.net/blog/2008/11/ant-tasks-for-git/
- ↑ JGit - GIT Ant Tasks defined so far: http://wiki.eclipse.org/JGit/User_Guide#Ant_Tasks
- ↑ How to lookup the latest git commit hash from an ant build script: http://stackoverflow.com/questions/2974106/how-to-lookup-the-latest-git-commit-hash-from-an-ant-build-script