Difference between revisions of "User:Minooz/RepoSyncProj/Ant"

From CDOT Wiki
Jump to: navigation, search
(Tutorials)
(Tutorials)
Line 57: Line 57:
 
::This could be used in an attribute like this: ${builddir}/classes
 
::This could be used in an attribute like this: ${builddir}/classes
 
::This is resolved at run-time as c:/test/classes
 
::This is resolved at run-time as c:/test/classes
 +
<source lang=java>
 +
 +
<project name="MyProject" default=“init" basedir=".">
 +
<description> simple example build file </description>
 +
<!-- set global properties for this build -->
 +
<property name="src" location="src"/>
 +
<property name="build" location="build"/>
 +
<target name="init">
 +
<!-- Create the build directory structure used by compile -->
 +
<mkdir dir="${build}"/>
 +
</target>
 +
<target name="compile" depends="init" description="comp-source ">
 +
<!-- Compile the java code from ${src} into ${build} -->
 +
<javac srcdir="${src}" destdir="${build}"/>
 +
</target>
 +
<target name="clean" description="clean up" >
 +
<!-- Delete the ${build} directory tree -->
 +
<delete dir="${build}"/>
 +
</target>
 +
</project>
 +
</source>
  
 
==Tips==
 
==Tips==

Revision as of 15:31, 14 October 2010

ANT

Apache Website

Manual
Index of Manual/Tasks
JUnit Task

Tutorials

Ant is a Java-based build tool. Instead of a model where it is extended with shell-based commands, ant is extended using Java classes
Instead of writing shell commands, the configuration files are XML-based, calling out a target tree where various tasks get executed.
Installing Ant :
Add the bin directory to your path. Set the ANT_HOME environment variable to the directory where you installed Ant. Assume ant is installed in c:\ant\. The following sets up the environment:
set ANT_HOME=c:\ant
set JAVA_HOME=...
set PATH=%PATH%;%ANT_HOME%\bin
ant buildfiles are written in XML – build.xml. Each buildfile contains one project and at least one (default) target.
<?xml version="1.0"?>
<!-- compile java files from all subdirectories -->
  <project name="First" default="build" basedir=".">
    <target name="build" >
      <javac srcdir="."
        debug="false"
        optimize="false"
        includes="**/*.java"
      />
    </target>
  </project>
A project has three attributes: name: the name of the project. default: the default target to use when no target is supplied. basedir: the base directory from which all path calculations are done
Each project defines one or more targets which are a set of task elements you want to execute
When starting ant, you can select which target(s) you want to have executed
A target has the following attributes: name: the name of the target. depends: a comma-separated list of names of targets. if: the name of the property that must be set in order for this target to execute
It should be noted, however, that Ant's depends attribute only specifies the order in which targets should be executed - it does not affect whether the target that specifies the dependency(s) gets executed if the dependent target(s) did not (need to) run.
Note: The if and unless attributes only enable or disable the target to which they are attached. They do not control whether or not targets that a conditional target depends upon get executed. In fact, they do not even get evaluated until the target is about to be executed, and all its predecessors have already run. (See code below)
<target name="myTarget" depends="myTarget.check" if="myTarget.run">
    <echo>Files foo.txt and bar.txt are present.</echo>
</target>

<target name="myTarget.check">
    <condition property="myTarget.run">
        <and>
            <available file="foo.txt"/>
            <available file="bar.txt"/>
        </and>
    </condition>
</target>
A project can have a set of properties
A property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes. This is done by placing the property name between "${" and "}" in the attribute value. Example:
If "builddir" property with the value “c:/test",
This could be used in an attribute like this: ${builddir}/classes
This is resolved at run-time as c:/test/classes
<project name="MyProject" default=“init" basedir=".">
<description> simple example build file </description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="comp-source ">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="clean" description="clean up" >
<!-- Delete the ${build} directory tree -->
<delete dir="${build}"/>
</target>
</project>

Tips

In Cygwin in the build folder. Just typing ant, will build the default target of the project. But typing ant assign1.test for example, will start from mentioned target.
Also, option -f will build any files that's not named build.xml e.g. ant -f buildHudson.xml
We can create a new ant script e.g. buildHudson.xml that triggers the target(assign1.test) of main build file(build.xml) of the project. See below:
<project name="assign1" basedir="." default="myTarget">
  <target name="assign1.build.call">
    <!-- Call the target that does everything -->
    <ant antfile="build.xml" target="assign1.test"/>
   </target>
  <target name="myTarget.check" depends="assign1.build.call">
    <echo>The assign1.build was called!</echo>
  </target>
</project>