By John Ferguson Smart
Price: $59.99 USD
£37.50 GBP
Cover | Table of Contents | Colophon
$ java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)The
following two sections will discuss how to install Ant on Unix and Windows
.$ java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)The
following two sections will discuss how to install Ant on Unix and Windows
./usr/share directory. I also created a
convenient symbolic link to make later updates easier:# cd /usr/share # tar xvfz apache-ant-1.7.0-bin.tar.gz ant-1.7.0 # ln -s ant-1.7.0 Ant
# ls -al /usr/share/ant lrwxrwxrwx 1 root root 9 2007-08-04 22:36 Ant -> ant-1.7.0 # ls /usr/share/ant bin fetch.xml KEYS LICENSE.dom NOTICE docs get-m2.xml lib LICENSE.sax README etc INSTALL LICENSE LICENSE.xerces WHATSNEW
ANT_HOME=/usr/share/ant JAVA_HOME=/usr/lib/jvm/java PATH=$PATH:$ANT_HOME/bin:$JAVA_HOME/bin export PATH ANT_HOME JAVA_HOME
src/main directory, and place the
compiled classes in the build/classes directory: <target name="compile" depends="init" description="Compile Java code">
<javac srcdir="src" destdir="build/classes"/>
</target>This is equivalent to executing the javac tool as shown here: $ javac -nowarn -d build/classes src/mainlib. When you compile your
application, you need to tell Ant where to find these libraries.<path id="junit.classpath" location="lib/junit.jar"/>
<path id="junit.classpath" path="build/classes:lib/junit.jar"/>
<property name="javac.debug" value="on"/>
<property name="build.dir" location="build"/>This property value will be set to the absolute filename of the
build directory. <target name="display-properties">
<echo>Debug = ${javac.debug}</echo>
<echo>Build directory (build.dir) = ${build.dir}</echo>
</target>Calling this target would display the value of
this property on the console. On my machine, it displays the
following:$ ant display-properties
Buildfile: build.xml
display-properties:
[echo] Build directory (build.dir) = /home/john/projects/tax-calculator/build
BUILD SUCCESSFUL
Total time: 0 seconds <target name="display-properties">
<echoproperties />
</target>junit.jar file into the
$ANT_HOME/lib directory, alongside
the <target name="javadoc" depends="compile,init" description="Generate JavaDocs.">
<javadoc sourcepath="${src.dir}"
destdir="${reports.javadoc}"
author="true"
version="true"
use="true"
access="private"
linksource="true"
windowtitle="${ant.project.name} API">
<classpath>
<path refid="compile.classpath" />
<pathelement path="${build.classes.dir}" />
</classpath>
<doctitle><![CDATA[<h1>${ant.project.name}</h1>]]></doctitle>
<bottom><![CDATA[<i>Copyright © 2007 All Rights Reserved.
</i>]]></bottom>
</javadoc>
</target> <property name="project.name" value="{ant.project.name}" />
<property name="project.version" value="1.0" />
...
<target name="package" depends="compile" description="Generate JAR file">
<jar destfile="${dist.dir}/${project.name}-${project.version}.jar" basedir=
"${build.classes.dir}"/>
</target>$ ant clean package
Buildfile: build.xml
clean:
[delete] Deleting directory /home/wakaleo/projects/jpt-sample-code/ant-demo/dist
[delete] Deleting directory /home/wakaleo/projects/jpt-sample-code/ant-demo/reports
init:
[mkdir] Created dir: /home/wakaleo/projects/jpt-sample-code/ant-demo/dist
[mkdir] Created dir: /home/wakaleo/projects/jpt-sample-code/ant-demo/reports/xml
[mkdir] Created dir: /home/wakaleo/projects/jpt-sample-code/ant-demo/reports/html
compile:
package:
[jar] Building jar: /home/wakaleo/projects/jpt-sample-code/ant-demo/dist/
tax-calculator-1.0.jar
BUILD SUCCESSFUL
Total time: 0 secondsWe use the Maven convention for naming JAR files
here, which is to add the version number to the end of the filename.
This makes it easier to identify file versions at a glance. The project
name comes from the <property name="tomcat.install.dir" location="${user.home}/servers/tomcat
/apache-tomcat-5.5.23" />
<target name="local.deploy" depends="war" description="Deploy to local
Tomcat instance">
<copy file="${dist.dir}/${project.name}-${project.version}.war"
todir="${tomcat.install.dir}/webapps" />
</target>In this example, we simply defined a
property pointing to a local Tomcat installation, and used the <copy> task to copy the generated WAR
file to the Tomcat webapps directory, where Tomcat will be able to pick
it up and deploy it automatically. Many application servers work in the
same way. <property name="tomcat.install.dir" location="${user.home}/servers/tomcat
/apache-tomcat-5.5.23" />
<target name="local.deploy" depends="war" description="Deploy to local
Tomcat instance">
<copy file="${dist.dir}/${project.name}-${project.version}.war"
tofile="${tomcat.install.dir}/webapps/${project.name}.war" />
</target>bootstrap-findbugs.xml), which downloads and
installs the Findbugs package, is shown here:<project name="FindBugs Bootstrap script" default="bootstrap" basedir="." >
<!-- Define the environment-specific variable "findbugs.home" in this file. -->
<property file="${user.home}/ant-global.properties"/>
<!-- This default values used if no properties file is present -->
<property name="findbugs.home" value="${user.home}/.findbugs"/>
<property name="findbugs.version" value="1.2.0"/>
<echo>Installing FindBugs into ${findbugs.home}</echo>
<property name="sourceforge.mirror"
value="http://optusnet.dl.sourceforge.net/sourceforge" />
<available file="${findbugs.home}/findbugs.zip" property="findbugs.installed"/>
<echo>Bootstrap FindBugs</echo>
<target name="bootstrap" unless="findbugs.installed">
<echo>Installing FindBugs</echo>
<mkdir dir="${findbugs.home}" />
<get src="${sourceforge.mirror}/findbugs/findbugs-${findbugs.version}.zip"
dest="${findbugs.home}/findbugs.zip" usetimestamp="true"/>
<unzip src="${findbugs.home}/findbugs.zip"
dest="${findbugs.home}"/>
<move todir="${findbugs.home}">
<fileset dir="${findbugs.home}/findbugs-${findbugs.version}">
<include name="**/*"/>
</fileset>
</move>
<delete dir="${findbugs.home}/findbugs-${findbugs.version}"/>
</target>
</project>
<project name="xmltask-demo" default="main">
<!--xmltask.jar should be referenced via lib, or in the ${ant.home}/lib or similar
--> <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
<!-- you may need to reference a local copy of the DTD here if your XML
documents specify one. See below for more info -->
<xmlcatalog id="dtd">
<dtd
publicId="-//SPRING//DTD BEAN//EN"
location="./spring-1.0.dtd"/>
</xmlcatalog>
<target name="main">
<xmltask source="spring-template.xml" dest="spring.xml" preserveType="true">
<xmlcatalog refid="dtd"/>
<insert path="/beans" position="under">
<![CDATA[
<bean id="bean-to-insert" class="com.oopsconsultancy.example.Bean1">
<constructor-arg index="0">
.....
</constructor-arg>
</bean>
]]>
</insert>
</xmltask>
</target>
</project># cd /usr/local # tar xvfz maven-2.0.7-bin.tar.gz # lsThis will extract the maven installation in a directory called
maven-2.0.7. For convenience, on a
Unix system, I generally create a symbolic link to this directory to
make upgrades easier to manage:# ln -s maven-2.0.7 maven # ls -al total 16 drwxr-xr-x 3 root root 4096 2006-08-06 13:18 . drwxr-xr-x 53 root root 4096 2006-07-20 21:32 .. lrwxrwxrwx 1 root root 11 2006-08-06 13:17 maven -> maven-2.0.7 drwxr-xr-x 6 root root 4096 2006-08-06 13:17 maven-2.0.7
maven/bin
directory to your environment path. Typically, you will set this up in
one of your environment initialization scripts (for example, if you are
using Bash, you could place this configuration in the ~/.bashrc file if you just need to set it up
for your account, or in /etc/bashrc if you want to set it up for all
users on this machine). Don’t forget to make sure that the pom.xml
by default), which is placed in your project home directory.$ mvn compile
src/test directory. If any tests fail,
the build will stop. In all cases, Maven generates a set of test
reports in text and XML test reports in the target/surefire-reports directory (see
).
pom.xml) and two
subdirectories go into the project home directory: src for all source code
and target for generated artifacts. The src directory has a number of
subdirectories, each of which has a clearly defined purpose:src/main/javasrc/main/resourcessrc/main/filterssrc/main/configsrc/main/webappsrc/test/javasrc/test/resourcessrc/test/filterssrc/sitesettings.xml file. Each
user can have his or her own individual settings.xml file, which should be placed in the
$HOME/.m2 directory. This file is not
placed under version control, and therefore can contain details such as
usernames and passwords, which should not be shared in the source code
repository.$HOME/.m2/settings.xml.
You will have to create this file if it doesn’t already exist. To define
a proxy, just add a <proxy>
element in this file, as follows: <settings>
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>proxy.mycompany.com</host>
<port>8080</port>
<username>user</username>
<password>password</password>
<nonProxyHosts>*.mycompany.com</nonProxyHosts>
</proxy>
</proxies>
</settings>The
<nonProxyHosts> element is useful to define
servers that do not need proxy access, such as internal enterprise
repositories.settings.xml file is to configure mirror
servers. This typically is done to configure an organization-wide
repository. Many organizations use a local repository to store and share
internal packages and to act as a proxy to external repositories. This
solution is faster and more reliable than requiring users to go to the
Internet whenever a new dependency is required.lib), and are maintained either by hand or as
project artifacts that are stored in the source code repository along with
the source code. Maven, by contrast, uses a declarative approach. In a
Maven project, you list the libraries your application needs, including the exact
version number of each library. Using this information, Maven will do its
best to find, retrieve, and assemble the libraries it needs during the
different stages in the build lifecycle. In addition, using a powerful
feature called Transitive Dependencies (see ,” later in this section), it
will include not only the libraries that you declare but also all the
extra libraries that your declared libraries need to work
correctly.$HOME/.m2/repository directory
structure. If you use the same jar in two projects, it will only be
downloaded (and stored) once, which saves time and disk space. ...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
...