ant in 30 minutes or less

31
Ant In 30 Minutes Or Less

Upload: loredana-pop

Post on 10-Nov-2015

221 views

Category:

Documents


2 download

DESCRIPTION

learn ANT now

TRANSCRIPT

  • Ant In 30 Minutes Or Less

  • The Who, What, When and WherePart of the Jakarta project, a sub-project of ApacheJakarta is the sub-heading for all the Java oriented Apache projectsOpen source, BSD style license application providing build functions for Java programsAvailable since mid 2000http://jakarta.apache.org/ant/

  • The BasicsAnt replaces the popular 'make' utility used by many C/C++ developers. 'Make' lacks a file format that is easily parsed, has variations in implementations, and lacks extensibility.Ant attempts to address all of these flaws.Ant files are simple XML files with nodes in the XML file representing a hierarchy of major tasks (called 'targets') that can be performed and a series of steps to perform each one. This makes them easy for both man and machine to create and understand.

  • The Basics (2)Since Ant is written in Java itself, it is available on any platform for which you will be developing programs.You can write new custom tasks for unique development needs and put the code into an Ant extension JAR. With the JAR in place your tasks are indistinguishable from any other task in Ant.

  • TargetsRepresent the fundamental tasks you want the build file to perform.For Java applications these are often things like creating the Javadocs for the application, compiling and creating a JAR file, running unit tests, running a compiled application, etc.Complex tasks can be broken up into multiple targets representing separate steps.Referred to by name.

  • DependenciesAllow you to structure a set of targets so that a given target won't execute until any necessary precursor targets have successfully run (and any dependencies they may have as well).

  • TasksAnt has a large set of built-in tasks.There are over 50 different "core" tasks that Ant knows how to perform out of the box.Some of the most common tasks involve compiling source code, creating JAR files, copying files/directories, deleting files, and running Java programs.Once you've learned as little as eight of the built-in tasks you should be able to set up a build file for the most important jobs in most projects.

  • Tasks (2)You aren't limited to just the core set.There are over 30 more tasks that are enabled with a set of optional JAR files you put in an Ant subdirectory. Most of these tasks are either very seldom used or are of use only if you are using a specific tool in your development (e.g. Perforce, JProbe, Clearcase, etc.).More third party tasks are coming all the time.You can create your own tasks.

  • PropertyCreates a name-value pair that can be used throughout a build script. Useful for storing settings like directory names that can change in one place (even in a file outside the main buildfile if you desire).Sets the property "foo.dist" to be equal to the value "dist". This can then be used in any of the attributes of other Ant tasks using "${foo.dist}".

  • Property (2)Pulls all the properties from a file. The format of the file is the traditional "name=value" style that Java properties files have always used.

    Demonstrates that properties can also be pulled from system environment variables on certain operating systems (not necessarily all though).

  • CopyCopies a file or set of files to another location. Does not overwrite existing files if they are newer unless you specify that you want it to overwrite.Has the secondary ability to do simple replacements in text as it copies.Copy a single file.

  • Copy (2)Copy all the files from one directory to another, skipping any java source files. Copy all the files from the directory ../backup/dir to src_dir. Replace occurrences of "@TITLE@" in the files with "Foo Bar".

  • DeleteDeletes files, directories, or sets of files.Delete a single file.

    Delete all the .bak files from this directory and sub-directories. Delete the build directory and everything (files and directories) inside it.

  • MkdirCreates a directory.Creates a directory with a name and location specified by the "DistributionDir" property.

    Creates a subdirectory called "jars" in the location specified by the "Distribution" property.

  • JavacCompiles Java source code. Attempts to do so in an intelligent fashion so that files that already have an up to date .class file are not recompiled.Compiles all java source files under the ${src} directory and puts the resulting .class files in the ${build} directory. xyz.jar is included in the classpath during the compilation. Debugging information will be included in the .class files.

  • Javac (2)Compiles the code in the directories ${src} and ${src2} to the ${build} directory. The xyz.jar is included in the classpath during compilation. Note that the include and exclude entries narrow the set of code that will be compiled out of the two source directories. As with the previous example, debugging information will be included in the .class files.

  • JarCreates a JAR file from a set of files or updates an already existing JAR.Will automatically supply a manifest file for the JAR or use one you specify.

  • Jar (2)Creates a JAR file called app.jar from all the files in the ${build}/classes directory. The file is put in the ${dist}/lib directory.

    Note the use of properties like ${dist} and ${build}. These are variables which can be set in the Ant file, via an external file, or even set on the command line as the Ant build file is invoked.

  • Jar (3)Creates a JAR file from all the files in ${build}/classes and ${src}/resources. Note that any files named Test.class in the first directory are not included in the JAR. Also note that we used two distinct filesets to specify the files neede for this JAR.

  • JavadocCreates Javadocs from Java source code files.Builds Javadoc only for the packages beginning with "com.lumptylump..." in the "src" directory.

  • JavaCan be used to invoke a Java program from within an Ant build file. Capable of forking off a separate process so that a System.exit() cannot kill the Ant build.Invokes a class named test.Main from within test.jar. Passes the argument -h to test.Main.

  • Java (2)Invokes a class named test.Main in a separate VM. Passes the argument -h to test.Main and "-Xrunhprof:cpu=samples,file=log.txt,depth=3" to the forked VM to request profiling.

  • An Example

  • An Example (2)

  • An Example (3)

  • Output From The Exampleant cleanBuildfile: build.xml init: [propertyfile] Updating property file: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build.properties [mkdir] Created dir: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build [mkdir] Created dir: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build/classes [mkdir] Created dir: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build/dist [mkdir] Created dir: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/docs/apidoc [echo] Build 0 clean: [delete] Deleting directory /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build BUILD SUCCESSFUL Total time: 2 secondsantNote: Just typing Ant invokes the default target.

  • Output From The Example (2)antNote: Just typing Ant invokes the default target.Buildfile: build.xml init: [propertyfile] Updating property file: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build.properties [mkdir] Created dir: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build [mkdir] Created dir: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build/classes [mkdir] Created dir: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build/dist [echo] Build 01 compile: [javac] Compiling 1 source file to /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build/classes jar: [jar] Building jar: /home/jmunsch/MyDocuments/MyDownloads/CoughUp/build/dist/sample.jar all: [echo] Application built. BUILD SUCCESSFUL Total time: 4 seconds

  • Output From The Example (3)ant -projecthelpBuildfile: build.xml Main targets: all Build everything. clean Clean all build products. javadoc Javadoc for the code. Default target: all

  • ToolsAutomated Build ToolsAnthillhttp://www.urbancode.com/projects/anthill/Cruise Controlhttp://cruisecontrol.sourceforge.net/OthersSee the "Related Projects" (http://jakarta.apache.org/ant/projects.html) page on the Ant site for more

  • Why Ant?FeaturesMore popular than 'make' for building Java projectsIt runs on any platform that the Java projects it builds are onUpdated and improved regularlyStraightforward XML syntaxPlug-in oriented architecture encourages expansionDirectly supported by some IDEs (with more coming)Free and open sourceFast, fast, and oh, did I mention, fast?

  • Questions?