Ant

From BC$ MobileTV Wiki
(Redirected from Ivy)
Jump to: navigation, search
Apache Ant project logo

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


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




Resources


Tutorials


External Links


References

  1. Why do you call it ANT?: http://ant.apache.org/faq.html#ant-name
  2. How to Use SVN Tasks with ANT: http://java.dzone.com/articles/how-use-svn-tasks-with-ant
  3. Define your own Ant Tasks for GIT: http://tlrobinson.net/blog/2008/11/ant-tasks-for-git/
  4. JGit - GIT Ant Tasks defined so far: http://wiki.eclipse.org/JGit/User_Guide#Ant_Tasks
  5. 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

See Also

Maven | Java