1 / 21

Live and Learn – ant

Live and Learn – ant. 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-dw-java-apant-i.html Totally righteous tutorial.

gypsy
Download Presentation

Live and Learn – ant

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Live and Learn – ant

  2. 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-dw-java-apant-i.html • Totally righteous tutorial

  3. Questions from Live and Learn • What is the basic structure of an Ant file? • What's required and what's optional? • What is the difference between build.xml and build.properties? • What is the advantages/disadvantages of defining properties in build.properties instead of build.xml? • How do I create and destroy directories? • How do I define projects to run using Ant? • Other hints and suggestions from an expert? • How to do junit tasks? • How to do task dependencies (don't compile unless fetch got stuff)

  4. 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

  5. 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>

  6. 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>

  7. 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>

  8. 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>

  9. 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/System.html#getProperties() • From the Property task

  10. 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

  11. 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>

  12. 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>

  13. 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>

  14. Path-like structures -simple <classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath>

  15. 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>

  16. 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>

  17. Optional tasks • Require libraries that are not supplied with Ant • Put them in Ant’s lib directory, or add to the system classpath

  18. 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.

  19. 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>

  20. “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>

  21. 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>

More Related