Difference between revisions of "Ant"

From Suhrid.net Wiki
Jump to navigationJump to search
 
(One intermediate revision by the same user not shown)
Line 94: Line 94:
 
</project>
 
</project>
  
 +
</syntaxhighlight>
 +
 +
 +
* Myriad tasks exist. For e.g. various file system tasks available are : <mkdir>, <delete>, <copy>, <zip>, <unzip>, <replace> etc.
 +
 +
== Custom tasks ==
 +
 +
* Implement a class that extends ANT's Task class.
 +
* Associate a tag with the class name.
 +
* Custom task java class:
 +
 +
<syntaxhighlight lang="java5">
 +
package net.suhrid;
 +
 +
import org.apache.tools.ant.Task;
 +
import org.apache.tools.ant.BuildException;
 +
 +
public class MyAntTask extends Task {
 +
 +
public void execute() throws BuildException {
 +
System.out.println("Custom ANT Task executing");
 +
}
 +
}
 +
 +
</syntaxhighlight>
 +
 +
* Build file:
 +
 +
<syntaxhighlight lang="xml">
 +
 +
<project name="Custom Task Test" default="run">
 +
<taskdef name="custom_task"
 +
        classname="net.suhrid.MyAntTask"
 +
        classpath="bin"/>
 +
</project>
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 11:48, 4 June 2012

Intro

  • Ant is designed primarily for building Java projects, but that's not its only use.
  • It is helpful for other tasks, such as performing filesystem operations in a cross-platform way.
  • As an application's build process becomes more complex, it becomes increasingly important to ensure that precisely the same build steps are carried out during each build, with as much automation as possible, in order to produce consistent builds in a timely manner.
  • Ant 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's greatest strength.

Build File

  • Build files are written in XML.
  • Every build file contains a single top-level project element.
  • The project element consists of a number of 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. 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

  • Sample build file:
<?xml version="1.0"?>
<project default="init">
    <target name="init">
    </target>
</project>

Properties

  • Properties are like variables. They can be given a name and a value. However, they are immutable, the value cannot be changed once set.
  • Example:
<property name="db-driver" value="org.xyz.db.JDBCDriver"/>
<property name="db-conn" value="${db-driver}@host:port"/>

Dependencies

  • .Instead of specifying the targets in sequence order, Ant takes the more flexible approach of defining dependencies.
  • Each target is defined in terms of all the other targets that need to be completed before it can be performed. This is done using the depends attribute of the target element.
  • Depends means on "Depends-On"
  • Example:
<target name="init"/>
<target name="preprocess" depends="init"/>
<target name="compile" depends="init,preprocess"/>
<target name="package" depends="compile"/>

Tasks

  • Built in javac task. An important characteristic of the javac task is that it will only compile those source files that it believes it needs to.
  • Runs within the ANT JVM without need to spawn a new process.
  • Similar tasks for packaging and executing.
  • See sample build file below:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
     4 Jun 2012 15:50:06                                                        

     Eclipse Ant Test    
     Project to test Eclipse Ant Integration
                   
     suhridk                                                                
     ====================================================================== -->
<project name="Eclipse Ant Test" default="run">
    <description>
            Project to test Eclipse Ant Integration
    </description>

    <!-- ================================= 
          target: default              
         ================================= -->
    <target name="compile" description="Compile java classes">
    	    <javac srcdir="src" destdir="bin" />
    </target>

    <!-- - - - - - - - - - - - - - - - - - 
          target: package                      
         - - - - - - - - - - - - - - - - - -->

    <target name="package" depends="compile" description="Creates a jar">
			<jar destfile="package.jar" basedir="bin">
				<manifest>
					<attribute name="Main-class" value="net.suhrid.RecordSystem"/>
				</manifest>
			</jar>
	</target>

	<target name="run" depends="package" description="Executes the jar">
			<java jar="package.jar" fork="true"/>
	</target>

</project>


  • Myriad tasks exist. For e.g. various file system tasks available are : <mkdir>, <delete>, <copy>, <zip>, <unzip>, <replace> etc.

Custom tasks

  • Implement a class that extends ANT's Task class.
  • Associate a tag with the class name.
  • Custom task java class:
package net.suhrid;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;

public class MyAntTask extends Task {
	
	public void execute() throws BuildException {
		System.out.println("Custom ANT Task executing");
	}
}
  • Build file:
<project name="Custom Task Test" default="run">
	<taskdef name="custom_task"
        classname="net.suhrid.MyAntTask"
        classpath="bin"/>
</project>