自动化每日构建(二)用Ant来完成java工程的每日构建

过去写的文章,不过是英文的。后面有附带的build.xml文件。

Start a new Java project by using Ant

This document has 5 sections:

Brief

System requirement

Directories and files

The Build file

Run Ant

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Brief

Start a new Java project by using Ant, so we can make Daily Building. All have to do list below:

1.Install java SDK1.4 and new version Ant, and make Ant works.

2.Copy Directories and files from dproject(d for Java) template library.

3.Make project files stay in the right directory.

4.Edit the Build.xml file. Let it fit the project.

5.Run Ant.

System requirement

Java SDK1.4 or higher

Ant 1.6 or higher

Directories and files

You can make directories yourself, or copy from template. But make sure the directory tree like this:

Every directory is made for a certain kind of files. Directory names and what kind of files should be put in list below:

Directory Files should be put in

Build building files

Data project’s data files

Doc project‘s documents for installation and deploying

Lib libraries project depending on

Res resources project using

Install resources for installation

Src project’ source files

Config project’s configuration files

Database project’s database files

Java project’s Java source code files

Docs project’s documents for manager, developer, tester

Scripts project’s script files

Sql project’s script files for database

Now put the Java files into the /src/Java directory.

The Build file

The build file is /build/build.xml. In the template we already have a default build file. Because every project has different name and different configuration, so we must edit the build file to suit project. We must edit the project name and path in the build file.

Run Ant

After doing that, now start a command-line prompt, change path to ./build, and type ant. We can see a function list like this:

Now Ant runs. We can EDIT the build file to add new features: unit testing, packing, deploying, etc, as you like.

build.xml文件:


<project basedir="." default="usage"></project>


<project default="usage" basedir=".">

<!-- =================================================================== -->
<!-- Initialization target -->
<!-- =================================================================== -->
<target name="init">
<tstamp/>
<buildnumber/>
<property environment="env" />
<property file="build.number"/>
<!-- You have to fill the ***** space with your project label. -->
<property name="Name" value="*****"/>
<property name="name" value="*****"/>
<property name="year" value="2004"/>


<echo message="----------- ${Name} Build ${build.number} [${year}] ------------"/>

<property name="build.compiler" value="modern"/>
<property name="debug" value="on"/>
<property name="optimize" value="on"/>
<property name="deprecation" value="on"/>

<property name="s.home" value=".."/>
<property name="s.src" value="${s.home}/src"/>
<property name="s.lib" value="${s.home}/lib"/>
<property name="s.res" value="${s.home}/res"/>
<property name="s.build" value="${s.home}/build"/>
<property name="s.run" value="${s.home}/run"/>
<property name="s.web" value="${s.src}/jsp"/>

<property name="s.build.assemble" value="${s.build}/assemble"/>
<property name="s.build.classes" value="${s.build.assemble}/classes"/>
<property name="s.build.gensrc" value="${s.build.assemble}/gen-src"/>
<property name="s.build.ejb" value="${s.build}/assemble/META-INF/"/>
<property name="s.build.javadocs" value="${s.build}/apidocs"/>
<property name="s.build.lib" value="${s.home}/res/xdoclet-1.2"/>
<property name="s.build.release" value="${s.build}/release"/>
<property name="s.build.web" value="${s.build}/web"/>
<property name="s.build.dist" value="${s.build}/dist"/>
<property name="s.main" value="com"/>

<property file="${s.build}/build.properties" />

<filter token="year" value="${year}"/>
<filter token="version" value="${version}"/>
<filter token="date" value="${TODAY}"/>
<filter token="log" value="true"/>
<filter token="verbose" value="true"/>

<path id="s.classpath">
<fileset dir="${s.lib}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
</path>

<path id="buildtools.classpath">
<fileset dir="${s.build.lib}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
<fileset dir="${s.lib}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
</path>

<tstamp>
<format property="TODAY" pattern="d-MM-yy"/>
</tstamp>

<taskdef
name="xdoclet"
classname="xdoclet.DocletTask"
classpathref="buildtools.classpath"
/>
<taskdef
name="ejbdoclet"
classname="xdoclet.modules.ejb.EjbDocletTask"
classpathref="buildtools.classpath"
/>
<taskdef
name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="buildtools.classpath"
/>

</target>

<!-- =================================================================== -->
<!-- Help on usage -->
<!-- =================================================================== -->
<target name="usage">
<echo message=""/>
<echo message=""/>
<echo message="***** Build file"/>
<echo message="-------------------------------------------------------------"/>
<echo message=""/>
<echo message=" available targets are:"/>
<echo message=""/>
<echo message=" jar --> generates the *****.jar file (default)"/>
<echo message=" compile --> compiles the source code"/>
<echo message=" release --> build the installation package"/>
<echo message=" run --> build application into the run dir"/>
<echo message=" clean.run --> cleans up run directory"/>
<echo message=" javadoc --> generates the API documentation"/>
<echo message=" clean --> cleans up the directory"/>
<echo message=""/>
<echo message=" See the comments inside the build.xml file for more details."/>
<echo message="-------------------------------------------------------------"/>
<echo message=""/>
<echo message=""/>
</target>

<!-- =================================================================== -->
<!-- Compiles the source directory -->
<!-- =================================================================== -->
<target name="compile" depends="init, ejbdoclet">
<mkdir dir="${s.build.classes}"/>
<echo message=""/>
<echo message="Compiling main source..."/>
<javac srcdir="${s.src}/java;${s.build.gensrc}"
destdir="${s.build.classes}"
includes="${s.main}/**/*.java"
debug="${debug}"
classpathref="s.classpath"
optimize="${optimize}"/>

<copy todir="${s.build.classes}/com/wml/domain">
<fileset dir="${s.src}/java/com/wml/service/persistence">
<include name="*.xml"/>
</fileset>
</copy>


<copy todir="${s.build.classes}">
<fileset dir="${s.src}/java/">
<include name="**/*.gif"/>
<include name="**/*.jpg"/>
<include name="**/*.png"/>
</fileset>
</copy>

<copy todir="${s.build.classes}">
<fileset dir="${s.src}/config/default/schat/server">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>
</copy>

</target>

<!-- =================================================================== -->
<!-- Creates the class package -->
<!-- =================================================================== -->
<target name="jar-ejb" depends="compile">

<mkdir dir="${s.build.assemble}/jar"/>
<jar jarfile="${s.build.assemble}/jar/${name}-ejb.jar"
excludes="**"
basedir="${s.build.classes}">

<fileset dir="${s.build.classes}">
<include name="**/*.class" />
<include name="**/*.xml" />
<include name="*.properties"/>
<exclude name="**/web/**/*" />
<exclude name="**/application.xml"/>
<exclude name="**/client/**/*" />
<exclude name="**/test/**/*" />
</fileset>

<metainf dir="${s.src}/java/META-INF" includes="*.xml" excludes="application.xml" />
<metainf dir="${s.build.ejb}/" includes="*.xml" excludes="application.xml" />
</jar>

</target>

<!-- =================================================================== -->
<!-- Creates the class package -->
<!-- =================================================================== -->
<target name="jar-optconsole" depends="compile">

<mkdir dir="${s.build.assemble}/jar"/>
<jar jarfile="${s.build.assemble}/jar/${name}-optconsole.jar"
excludes="**"
basedir="${s.build.classes}">

<fileset dir="${s.build.classes}">
<include name="**/*.class" />
<include name="**/*.xml" />
<include name="**/*.gif" />
<include name="**/*.jpg" />
<include name="**/*.png" />
<exclude name="*.properties"/>
<exclude name="**/web/**/*" />
<exclude name="**/application.xml"/>
<exclude name="comft/**/*" />
<exclude name="comut/**/*" />
<exclude name="test/**/*" />
<exclude name="org/**/*" />
<exclude name="META-INF/**/*" />
<exclude name="config/**/*" />
</fileset>
</jar>

</target>


<!-- =================================================================== -->
<!-- Creates the ear package -->
<!-- =================================================================== -->
<target name="ear" depends="jar-ejb">

<mkdir dir="${s.build.dist}"/>
<ear destfile="${s.build.dist}/wmlschat.ear"
appxml="${s.src}/java/META-INF/application.xml" >
<fileset dir="${s.lib}" includes="*.jar"/>
<fileset dir="${s.build.assemble}/jar/" includes="*.jar"/>

</ear>

</target>

<target name="war" depends="compile">
<echo>building war...</echo>
<mkdir dir="${s.build.assemble}/jar"/>
<mkdir dir="${s.run}"/>

<war warfile="${s.build.assemble}/jar/wml.war" webxml="${s.web}/WEB-INF/web.xml">
<fileset dir="${s.run}" >
<include name="member/**/*" />
</fileset>
<fileset dir="${s.web}"/>
<classes dir="${s.build.classes}">
<include name="**/*.class" />
<include name="**/*.xml" />
<include name="*.properties"/>
<exclude name="**/comft/**/*" />
<exclude name="**/comut/**/*" />
<exclude name="**/org/**/*" />
<exclude name="**/application.xml"/>
<exclude name="**/test/**/*" />
</classes>
<lib dir="${s.lib}">
<include name="*.jar"/>
</lib>
</war>
</target>

<!-- =================================================================== -->
<!-- Creates the deploy -->
<!-- =================================================================== -->
<target name="deploy" depends="jar-ejb, war">

<echo>JBOSS deploy dir set to: ${jboss.deploy.dir}</echo>
<copy todir="${jboss.deploy.dir}">
<fileset dir="${s.src}/config/default/jboss/wml" />
</copy>
<copy todir="${jboss.deploy.dir}/lib">
<fileset dir="${s.build.assemble}/jar/" includes="*-ejb.jar"/>
<fileset dir="${s.lib}" />
</copy>

<copy todir="${jboss.deploy.dir}/conf">
<fileset dir="${s.src}/config/default/schat/server">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>
</copy>
<copy todir="${jboss.deploy.dir}/deploy">
<fileset dir="${s.build.assemble}/jar/" includes="*-ejb.jar"/>
<fileset dir="${s.build.assemble}/jar/" includes="*.war"/>
</copy>

</target>


<!-- =================================================================== -->
<!-- Build a runnable version of the app in the RUN dir -->
<!-- =================================================================== -->
<target name="run" depends="jar-optconsole">

<!-- Operator -->

<mkdir dir="${s.run}/operator/lib"/>
<copy todir="${s.run}/operator/lib">
<fileset dir="${s.build.assemble}/jar/">
<include name="*.jar"/>
</fileset>

<fileset dir="${s.lib}/">
<include name="*.jar"/>
</fileset>
</copy>

<mkdir dir="${s.run}/operator/jre" />

<unzip src="${s.res}/jre142.zip" dest="${s.run}/operator/" overwrite="false"/>

<copy todir="${s.run}/operator">
<fileset dir="${s.src}/scripts/"/>
<fileset dir="${s.src}/images/icons/">
<include name="*.ico"/>
</fileset>
</copy>

<mkdir dir="${s.run}/operator/docs"/>
<copy todir="${s.run}/operator/docs">
<fileset dir="${s.src}/docs">
<exclude name="**/*.*~"/>
</fileset>
</copy>

<copy todir="${s.run}/operator">
<fileset dir="${s.src}/config/default/schat/client">
<include name="**/*.xml"/>
</fileset>
</copy>

<mkdir dir="${s.run}/operator/logs" />

</target>


<!-- =================================================================== -->
<!-- Invoke XDoclet's ejbdoclet -->
<!-- =================================================================== -->
<target name="ejbdoclet" depends="init" >

<echo/>
<echo>Running Generating EJBs...</echo>
<echo/>

<mkdir dir="${s.build.gensrc}"/>
<mkdir dir="${s.build.ejb}"/>

<ejbdoclet
destdir="${s.build.gensrc}"
mergedir="parent-fake-to-debug"
excludedtags="@version,@author,@todo"
addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
ejbspec="2.0"
force="false"
verbose="false"
>

<fileset dir="${s.src}/java">
<include name="com/wml/service/**/*Bean.java"/>
</fileset>

<packageSubstitution packages="ejb" substituteWith="interfaces"/>

<remoteinterface/>
<localinterface/>
<homeinterface/>
<localhomeinterface/>

<session/>

<utilobject cacheHomes="true" includeGUID="true"/>

<deploymentdescriptor
destdir="${s.build.ejb}"
validatexml="true"
mergedir="fake-to-debug"
description=""
>
<configParam name="clientjar" value="blah.jar"/>
</deploymentdescriptor>


<jboss
version="3.2"
unauthenticatedPrincipal="nobody"
xmlencoding="UTF-8"
destdir="${s.build.ejb}"
validatexml="true"
preferredrelationmapping="relation-table"
/>

</ejbdoclet>

</target>


<!-- =================================================================== -->
<!-- Build the installation packge -->
<!-- =================================================================== -->
<target name="release" depends="clean, clean.run, release.client, release.server">

</target>


<target name="release.server" depends="jar-ejb, war">

<echo message=""/>
<echo message="Building server installation..."/>
<mkdir dir="${s.build}/assemble/install-server/wml"/>

<copy todir="${s.build}/assemble/install-server/wml">
<fileset dir="${s.src}/config/default/jboss/wml" />
</copy>
<copy todir="${s.build}/assemble/install-server/wml/lib">
<fileset dir="${s.lib}" />
</copy>

<copy todir="${s.build}/assemble/install-server/wml/conf">
<fileset dir="${s.src}/config/default/schat/server">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>
</copy>
<copy todir="${s.build}/assemble/install-server/wml/deploy">
<fileset dir="${s.build.assemble}/jar/" includes="*-ejb.jar"/>
<fileset dir="${s.build.assemble}/jar/" includes="*.war"/>
</copy>

<mkdir dir="${s.build.release}/Server-linux"/>
<zip destfile="${s.build.release}/Server-linux/jboss-wml.zip" basedir="${s.build}/assemble/install-server/"/>
<copy todir="${s.build.release}/Server-linux/">
<fileset dir="${s.res}" includes="*.rpm"/>
<fileset dir="${s.res}" includes="jboss-3.2.3.zip"/>
<fileset dir="${s.res}" includes="j2sdk*.gz"/>
</copy>

<mkdir dir="${s.build.release}/Server-linux"/>
<copy todir="${s.build.release}/Server-linux/database">
<fileset dir="${s.src}/sql" includes="*.sql"/>
</copy>

<copy todir="${s.build.release}/">
<fileset dir="${s.src}/docs" includes="Install*.doc"/>
</copy>

</target>

<target name="release.client" depends="run">
<!-- clients -->
<echo message=""/>
<echo message="Building client installation..."/>
<mkdir dir="${s.build}/assemble/install-client"/>
<mkdir dir="${s.build}/assemble/install-res"/>
<mkdir dir="${s.build.release}/Client"/>
<unzip src="${s.res}/nsis.zip" dest="${s.build}/assemble/install-res"/>
<exec dir="${s.build}" os="Windows 2000" executable="./assemble/install-res/NSIS/makensis.exe">
<arg line="../src/install/client-install.nsi"/>
</exec>
<move todir="${s.build.release}/Client" >
<fileset dir="${s.build}/assemble/install-client/">
<include name="**/*.exe"/>
</fileset>
</move>
</target>
<!-- =================================================================== -->
<!-- Creates the API documentation -->
<!-- =================================================================== -->
<target name="javadoc" depends="init">
<mkdir dir="${s.build.javadocs}"/>
<javadoc packagenames="com.*"
sourcepath="${s.src}/java;${s.build.gensrc}"
destdir="${s.build.javadocs}"
classpathref="s.classpath"
author="true"
version="true"
use="true"
splitindex="true"
noindex="false"
windowtitle="${Name} API"
doctitle="${Name}"
bottom="Copyright &#169; ${year} *****, All Rights Reserved."
/>
</target>

<!-- =================================================================== -->
<!-- Clean targets -->
<!-- =================================================================== -->
<target name="clean" depends="init">
<delete dir="${s.build.assemble}"/>
<delete dir="${s.build.javadocs}"/>
<delete dir="${s.build.release}"/>
</target>

<target name="clean.run" depends="init">
<delete dir="${s.run}" quiet="true"/>
<mkdir dir="${s.run}"/>
</target>

</project>

<!-- End of file -->

你可能感兴趣的:(java)