|
|
- Build.xml is what ANT looks for by default. - The Ant installation comes with a JAXP-Compliant XML parser, this means that the installation of an external XML parser is not necessary
The root element of an Ant build file is the project element, it has three attributes. 1. name: The name of the project, it can be any combination of alphanumeric characters that constitute valid XML. 2. default: The default target to use when no target is specified, out of these three attributes default is the only required attribute. 3. basedir: The base directory from which any relative directories used within the Ant build file are referenced from. If this is omitted the parent directory of the build file will be used.
basedir the absolute path of the project's basedir (as set with the basedir attribute of <project>). ant.file the absolute path of the buildfile. ant.version the version of Ant ant.project.name the name of the project that is currently executing; it is set in the name attribute of <project>. ant.java.version the JVM version Ant detected; currently it can hold the values "1.1", "1.2", "1.3" and "1.4"
If - for example, if="gui-ready" could be used to only execute the encapsulating target's instructions if the property gui-ready was is (to any value). Unless - unless="gui-ready" could be used to conditionally execute the contents of the encapsulating target. The targets' contents will be executed unless the the property gui-ready is set (to any value).
ant -buildfile test.xml -Dbuild=build/classes dist runs Ant using the test.xml file in the current directory, on the target called dist, setting the build property to the value build/classes.
You can specify PATH- and CLASSPATH-type references using both ":" and ";" as separator characters. Ant will convert the separator to the correct character of the current operating system. As a shortcut, the <classpath> tag supports path and location attributes of its own, so: <classpath> <pathelement path="${classpath}"/> </classpath> can be abbreviated to: <classpath path="${classpath}"/>
The shortcuts previously mentioned for <classpath> are also valid for <path>.For example: <path id="base.path"> <pathelement path="${classpath}"/> </path> can be written as: <path id="base.path" path="${classpath}"/>
The order in which the files building up a FileSet are added to the path-like structure is not defined. This builds a path that holds the value of ${classpath}, followed by all jar files in the lib directory, the classes directory, all directories named classes under the apps subdirectory of ${build.dir}, except those that have the text Test in their name, and the files specified in the referenced FileList.
Only Java files that have no corresponding .class file or where the class file is older than the .java file will be compiled. Ant uses only the names of the source and class files to find the classes that need a rebuild. It will not scan the source and therefore will have no knowledge about nested classes, classes that are named different from the source file, and so on. See the <depend> task for dependency checking based on other than just existence/modification times.
When the antfile attribute is omitted, the file "build.xml" in the supplied directory (dir attribute) is used. If no target attribute is supplied, the default target of the new project is used By default, all of the properties of the current project will be available in the new project. Alternatively, you can set the inheritAll attribute to false and only "user" properties (i.e., those passed on the command-line) will be passed to the new project. In either case, the set of properties passed to the new project will override the properties that are set in the new project
The document root is where JSP pages, client-side classes and archives, and static web resources are stored. lib: a directory that contains jar archives of libraries (tag libraries and any utility libraries called by server-side classes). If the Web application uses Enterprise Beans, it can also contain ejb-jars. This is necessary to give to the Web components the visibility of the EJB classes. However, if the war is intended to be packed in a ear, the ejb-jars must not be placed here. In this case, they are directly included in the ear. Due to the use of the class loader hierarchy, Web components have the visibility of the EJB classes.
Introduction to Ant www.scmGalaxy.com scmGalaxy Author: Rajesh Kumar info@scmGalaxy.com
www.scmGalaxy.com scmGalaxy What is ANT? Installation Sample build.xml Executing ant script Closer look at the structure of ANT file Advantages of using ANT Packaging – JAR, WAR, EAR Agenda
www.scmGalaxy.com scmGalaxy What is ANT? Another Neat Tool Java-based build tool Like make, without make’s wrinkles An open source Apache Jakarta project http://ant.apache.org/ Implemented in Java, implemented for Java
www.scmGalaxy.com scmGalaxy Installation Download the binaries from http://jakarta.apache.org/ant/index.html Unzip to a suitable directory Set ANT_HOME = path_to_ant_folder Append PATH=%PATH%;%ANT_HOME%\bin to the PATH environment variable Append the .jar files in /path_to_ant/lib/ to the CLASSPATH environment variable Ensure that JAVA_HOME points to the location of the JDK installation on your machine & /path_to_jdk/lib/* is a part of the CLASSPATH environment variable
www.scmGalaxy.com scmGalaxy Working with ANT Every project using ANT will have a build file – build.xml <?xml version="1.0"?> <project name="test" default="compile" basedir="."> <property name="src" value="."/> <property name="build" value="build"/> <target name="init"> <mkdir dir="${build}"/> </target> <target name="compile" depends="init"> <!-- Compile the java code --> <javac srcdir="${src}" destdir="${build}"/> </target> </project>
www.scmGalaxy.com scmGalaxy A simple build.xml <?xml version="1.0"?> XML declaration specifying version of XML used. All XML constraints hold good for build.xml
www.scmGalaxy.com scmGalaxy A simple build.xml [Contd.] <property name="src" value="."/> <property name="build" value="build"/> Property declaration is like user-defined variables to use within the build file It has 2 attributes name – name of property value – desired value of the property To reference a property, ${property_name} - ${src} Built in properties that ANT provides : basedir v ant.file ant.version v ant.project.name ant.java.version
www.scmGalaxy.com scmGalaxy A simple build.xml [Contd.]
www.scmGalaxy.com scmGalaxy A simple build.xml [Contd.] <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${build}"/> </target> depends – comma separated list of all the targets on which this target depends, i.e, targets that must be executed prior to the execution of this target javac element is a task which is performed under the target compile By default, only those .java input files that have a more recent timestamp than their corresponding .class output files will be compiled
www.scmGalaxy.com scmGalaxy Executing ant script ant [options] [target [target2 [target3] ...]] Options: -help print this message -projecthelp print project help information -version print the version information and exit -diagnostics print information that might be helpful to diagnose or report problems. -quiet, -q be extra quiet -verbose, -v be extra verbose -debug print debugging information -emacs produce logging information without adornments -logfile <file> use given file for log -l <file> '' -logger <classname> the class which is to perform logging -listener <classname> add an instance of class as a project listener -buildfile <file> use given buildfile -file <file> '' -f <file> '' -D<property>=<value> use value for given property -propertyfile taking precedence -inputhandler <class> the class which will handle input requests -find <file> <name> load all properties from file with -D properties search for buildfile towards the root of the file system and use it
www.scmGalaxy.com scmGalaxy Path-like structures - classpath Wherever path-like values need to be specified, a nested element can be used : location - specifies a single file or directory relative to the project's base directory or an absolute filename path - accepts “:” or “;” separated lists of locations <classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath> <classpath path =“${classpath}”/>
www.scmGalaxy.com scmGalaxy Path-like structures - path To use the same path-like structure for several tasks, you can define them with a <path> element at the same level as targets, and reference them via their id attribute <path id="project.class.path"> <path refid="base.path"/> <pathelement location="lib/"/> </path> <classpath refid="project.class.path"/>
www.scmGalaxy.com scmGalaxy Path-like structures – dirset, fileset,filelist <classpath> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <pathelement location="classes"/> <dirset dir="${build.dir}"> <include name="apps/**/classes"/> <exclude name="apps/**/*Test*"/> </dirset> <filelist dir="${src.dir}" files="foo.xml,bar.xml"/> </classpath>
www.scmGalaxy.com scmGalaxy Tasks Piece of code that can be executed Built-in tasks : javac, java, javadoc, javah, junit mkdir, copy, move, delete, fileset jar, war, zip, unjar, unwar, unzip echo, cvs, exec ant, antcall You can also write your own tasks
www.scmGalaxy.com scmGalaxy javac Compiles a java source tree Source & destination directory recursively scanned for .java files to compile By default only check made for rebuild is existence / modification time To define any other java class dependencies use task <depend> <javac debug="${debug}" optimize="${optimize}" deprecation="${deprecation}" destdir="${build.dest}" includes="com/**" excludes="**/*.xml"> <src path="${src.dir}" /> <classpath refid="classpath" /> </javac> There are many more attributes / options available. Check Ant User Manual for more info.
www.scmGalaxy.com scmGalaxy Building subprojects - ant Runs Ant on supplied build file This task must not be used outside of a target if it invokes the same build file that it is part of <ant antfile="subproject/subbuild.xml" dir="subproject" target="compile"/> <ant inheritAll="false" antfile="subproject/subbuild.xml"> <property name="output.type" value="html"/> </ant>
www.scmGalaxy.com scmGalaxy Calling other targets - antcall Call another target within the same build-file (optionally specifying properties) This task must no be used outside of a target <target name="default"> <antcall target="doSomethingElse"> <param name="param1" value="value"/> </antcall> </target> <target name="doSomethingElse"> <echo message="param1=${param1}"/> </target>
www.scmGalaxy.com scmGalaxy Advantages Ease of use Configuration files are XML based Same config file (build.xml) can be used across platforms Platform independent Special support for Java Easy to create JavaDocs, WAR & JAR files Built-in support for JUnit FTP CVS
www.scmGalaxy.com scmGalaxy Advantages [Contd.] Particularly good for automating complicated repetitive tasks which is what most build processes are all about! ANT can be integrated with most Java IDEs Eclipse Jbuilder IntelliJIdea
PACKAGING
www.scmGalaxy.com scmGalaxy ejb-jar file will contain : Beans’ class files Beans’ deployment descriptors META-INF/ejb-jar.xml Application server specific file (e.g. META-INF/jboss.xml) Sample ejb-jar.xml Sample application specific descriptor file EJB Packaging – ejb-jar
www.scmGalaxy.com scmGalaxy Has specific hierarchical directory structure Top-level directory is the document root of application WAR has the following folders (usually): jsp – JSP files images – images used in the JSPs css – style sheet files scripts – javascript files WEB-INF WEB-INF folder contains Configuration files like web.xml, struts-config.xml .tld files (if any) lib : directory that contains jar archives of libraries classes : directory that contains the servlet classes and utility classes Web ARchive (WAR)
www.scmGalaxy.com scmGalaxy An EAR file can contain : Web components (war) Beans (ejb-jar) Libraries (jar) J2EE deployment descriptor (META-INF/application.xml) Sample application.xml Enterprise ARchive (EAR)
www.scmGalaxy.com scmGalaxy References Ant home page http://ant.apache.org/ Ant manual http://ant.apache.org/manual/index.html Another beginner’s tutorial http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/ant/ant.html
www.scmGalaxy.com Thank You ! Author: Rajesh Kumar rajeshkumar.raj06@gmail.com
Summary: Configuration management (CM) is a field of management that focuses on establishing and maintaining consistency of a system's or product's performance and its functional and physical attributes with its requirements, design, and operational information throughout its life. scmGalaxy.com is dedicated to software configuration, build and Release management. This covers CVS, VSS (Visual Source Safe),Perforce, SVN(Subversion) MKS Integrity, ClearCase,TFS,CM Synergy, Best Practices ,AnthillPro, Apache Ant, Maven, Bamboo, Cruise Control and many more tools. It provides quality training in SCM practice world.Contact us on info@scmGalaxy.com
| URL: |
No comments posted yet
Comments