Top Banner
Live and Learn – ant
21

Live and Learn – ant. Resources Ant User Manual Skip the book, most of the material is right here Apache.

Dec 13, 2015

Download

Documents

Juniper James
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Live and Learn – ant

Page 2: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Resources Ant User Manual

http://ant.apache.org/manual/index.html Skip the book, most of the material is right here

Apache Ant 101 – DeveloperWorks http://www-128.ibm.com/developerworks/edu/j-d

w-java-apant-i.html Totally righteous tutorial

Page 3: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Questions from Live and Learn1. What is the basic structure of an Ant file? 2. What's required and what's optional? 3. What is the difference between build.xml and

build.properties? 4. What is the advantages/disadvantages of defining

properties in build.properties instead of build.xml? 5. How do I create and destroy directories? 6. How do I define projects to run using Ant? 7. Other hints and suggestions from an expert? 8. How to do junit tasks? 9. How to do task dependencies (don't compile unless

fetch got stuff)

Page 4: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Basic structure Entities:

Project Targets Tasks Types

Project contains tasks, which contain targets and types

Implicit target contains all “top-level” targets and types is executed each time ant is invoked, even for

-projecthelp

Page 5: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Minimal file<?xml version="1.0"?><project default="doItAll"> <target name="doItAll"> <mkdir dir="build"/> <mkdir dir="dist"/> <javac srcdir="src" destdir="build"/> <jar destfile="dist/package-src.jar" basedir="src"/> <jar destfile="dist/package.jar" basedir="build"> </target>

<target name="clean"> <delete dir="build"/> <delete dir="dist"/> </target> </project>

Page 6: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Sample file (unreadable)<?xml version="1.0"?><project default="dist" name="Project Argon"> <description>A simple Java project</description>

<property name="srcDir" location="src"/> <property name="buildDir" location="build"/> <property name="distDir" location="dist"/>

<target name="init"> <tstamp/> <mkdir dir="${buildDir}"/> <mkdir dir="${distDir}"/> </target>

<target name="compile" depends="init"> <javac srcdir="${srcDir}" destdir="${buildDir}"/> </target>

<target name="dist" depends="compile"> <jar destfile="${distDir}/package-${DSTAMP}.jar" basedir="${buildDir}"> <manifest> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="pkg.Main"/> </manifest> </jar> <jar destfile="${distDir}/package-src-${DSTAMP}.jar" basedir="${srcDir}"/> </target>

<target name="clean"> <delete dir="${buildDir}"/> <delete dir="${distDir}"/> </target> </project>

Page 7: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Sample file (part 1)<?xml version="1.0"?><project default="dist" name="Project Argon"> <description>A simple Java project</description>

<property name="srcDir" location="src"/> <property name="buildDir" location="build"/> <property name="distDir" location="dist"/>

<target name="init"> <tstamp/> <mkdir dir="${buildDir}"/> <mkdir dir="${distDir}"/> </target>

<target name="compile" depends="init"> <javac srcdir="${srcDir}" destdir="${buildDir}"/> </target>

Page 8: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Sample file (part 2) <target name="dist" depends="compile"> <jar destfile="${distDir}/package-${DSTAMP}.jar" basedir="${buildDir}"> <manifest> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="pkg.Main"/> </manifest> </jar> <jar destfile="${distDir}/package-src-${DSTAMP}.jar“ basedir="${srcDir}"/> </target>

<target name="clean"> <delete dir="${buildDir}"/> <delete dir="${distDir}"/> </target> </project>

Page 9: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Properties Write-once! From command line:

ant -D key=value Built-in:

basedir, ant.file, ant.version, ant.project.name, ant.java.version

Java System properties http://java.sun.com/j2se/1.3/docs/api/java/lang/S

ystem.html#getProperties() From the Property task

Page 10: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Property task explicitly stated:

<property name="foo.dist" value="dist"/> <property name="foo.dist" location="dist"/>

pulled from a file: Java-style properties file syntax.

<property file="${user.home}/my.properties"/>

pulled from the environment:<property environment="env"/><echo message="ANT_HOME is $

{env.ANT_HOME}"/> also URL or Resource

Page 11: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Example from ExpertVoices<target name="init"> <property environment="env"/> <fail unless="env.EV_CONFIG" message="Environment variable 'EV_CONFIG' is not set"/> <property name="config_property_file“ location="properties/${env.EV_CONFIG}.build.properties"/> <available file="${config_property_file}" type="file“ property="config_property_file_present"/> <fail unless="config_property_file_present“ message="Property file '${config_property_file}'

missing"/> <property file="${config_property_file}"/>

<property name="common_property_file“ location="properties/_common.build.properties"/> <property file="${common_property_file}"/></target>

Page 12: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

File Set Inline:

<copy todir="${build_wpmu_dir}"> <fileset dir="${wpmu_fixes_dir}"> <exclude name="**/CVS/*" /> <exclude name="**/EV_ReadMe" /> </fileset></copy>

By reference:<fileset id="these_files" dir="${wpmu_fixes_dir}"> <exclude name="**/CVS/*" /> <exclude name="**/EV_ReadMe" /></fileset>

<copy todir="${build_wpmu_dir}"> <fileset refid="these_files" /></copy>

Page 13: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Pattern Set Inline or by reference

<patternset id="skip_unwanted_files"> <exclude name="**/CVS/*" /> <exclude name="**/EV_ReadMe" /></patternset>

<copy todir="${build_wpmu_dir}"> <fileset dir="${wpmu_fixes_dir}"> <patternset refid="skip_unwanted_files"/> </fileset></copy>

Page 14: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Path-like structures -simple<classpath>

<pathelement path="${classpath}"/>

<pathelement location="lib/helper.jar"/>

</classpath>

Page 15: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Path-like structures - complex<path id="base.path"> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <pathelement location="classes"/></path>

<path id="tests.path"> <path refid="base.path"/> <pathelement location="testclasses"/></path>

Page 16: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Other types Selectors

Selectors are a mechanism whereby the files that make up a fileset can be selected based on criteria other than filename.

Filtersets<copy file="${build.dir}/version.txt"

toFile="${dist.dir}/version.txt">

<filterset>

<filter token="DATE" value="${TODAY}"/>

</filterset>

</copy>

Page 17: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Optional tasks

Require libraries that are not supplied with Ant Put them in Ant’s lib directory, or add to

the system classpath

Page 18: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Optional tasks - gotchas from

http://ant.apache.org/manual/OptionalTasks/junit.html

Note: You must have junit.jar and the class files for the <junit> task in the same classpath. You can do one of: Put both junit.jar and the optional tasks jar file in

ANT_HOME/lib. Do not put either in ANT_HOME/lib, and instead

include their locations in your CLASSPATH environment variable.

Do neither of the above, and instead, specify their locations using a <classpath> element in the build file. See the FAQ for details.

Page 19: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

If – Then – Else<target name="if-then-else"> <condition property="condition"> <available file="fileone"/> </condition> <antcall target="then"/> <antcall target="else"/></target>

<target name="then" if="condition"> <echo>THEN BODY EXECUTED</echo></target>

<target name="else" unless="condition"> <echo>ELSE BODY EXECUTED</echo></target>

Page 20: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

“Subroutines” with parameters<target name="add_third_party_themes"> <antcall target="__load_extensions"> <param name="extensions_dir" value="${third_party_themes_dir}"/> <param name="destination_dir“ value="${build_wpmu_themes_dir}"/> </antcall></target>

<target name="__load_extensions"> <copy todir="${destination_dir}" overwrite="true"> <fileset dir="${extensions_dir}"> <patternset refid="skip_unwanted_files"/> </fileset> </copy></target>

Page 21: Live and Learn – ant. Resources  Ant User Manual   Skip the book, most of the material is right here  Apache.

Loops<taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath path="${ant_contrib_jar_path}" /></taskdef>

<target name="looper"> <foreach target="unzip_extension" param="zip_file" inheritall="Yes"> <path> <fileset dir="${extensions_dir}" includes="*.zip"/> </path> </foreach></target>

<target name="unzip_extension"> <unzip src="${zip_file}" dest="${destination_dir}"/></target>