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

原创 2004年07月14日 10:37:00

过去写的文章,不过是英文的。后面有附带的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 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 -->

[原创]每日构建实践篇(二)

     之前我们已经获取了当前项目的所有的代码,并把他们放入了指定的目录。下面是如何构建这些代码。本文以介绍VC的批处理过程,要注意的是,VC6的处理过程和VC.NET是不同,首先我们来看VC6如何...
  • snaill
  • snaill
  • 2004年10月13日 13:11
  • 1478

每日构建与持续集成联系与区别

每日构建与持续集成联系与区别 1)持续集成(Continuous Integration)贵在速度,强调一个快速反馈。      比如我一签入代码,就立刻集成,给我一个反馈,我要知道我的代码是否破坏...
  • xifeijian
  • xifeijian
  • 2013年07月18日 00:04
  • 7066

每日构建 Daily build

一个好的办法是每日构建(daily builds)。 每日构建意味着自动地,每天,完整地构建整个代码树、(译者按:“代码树”,原文为source tree,意思是将整个项目源代码的目录,子目录,文件的...
  • u013890660
  • u013890660
  • 2014年03月17日 10:49
  • 966

每日构建系列(1):入门篇

关键词:Daily Build、CI、CCNet、CCTray、VSS、SVN、MSBuild、NAnt、NUnit、Email、FxCop、NCover、NCoverExplorer、RSS、SCM...
  • u013948187
  • u013948187
  • 2015年12月13日 22:56
  • 279

一年多推行每日构建的经验总结

作者:朱金灿来源:http://blog.csdn.net/clever101         早在主持开发产品新版本之初,我就有一个略显激进的想法:新产品的测试版本发布频率至少在一周之内。开发人员倾...
  • clever101
  • clever101
  • 2014年08月31日 21:52
  • 3089

Visual Studio及TFS进行单元测试、负载测试、代码覆盖率、每日构建配置

这是以前在VS2010上让同事做的相关工作的备案,现在VS2012都出来了,2010都快过期,帖出来共享一下。 一、单元测试 微软官方给出的指导和示例在这里。一步一步按照说明下来就可以完成。(不截...
  • luminji
  • luminji
  • 2013年11月27日 14:03
  • 724

Ant构建Java项目

Ant构建Java项目 我们创建HelloWorld项目,将源文件.java发到src中,编译后字节码.class放到bin中,对应的jar包放到exe目录中。 首先使用命令行来构建项目: ...
  • u010852801
  • u010852801
  • 2015年06月21日 15:15
  • 1610

【Jenkins】——定时构建

一触即发
  • wangjinlong_
  • wangjinlong_
  • 2017年03月12日 15:14
  • 3260

初识持续集成(Continuous Integration)

随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题。尤其是近些年来,敏捷(Agile) 在软件工程领域越来越红火,如何能再不断变化的...
  • BonJean
  • BonJean
  • 2016年05月22日 11:06
  • 1051

Ant—使用Ant构建简单Java项目(一)

Apache Ant主要用于Java项目的构建。为什么要使用Apache Ant来构建项目呢:        1)项目构建不是简单的编译,各种资源需要合理构建,例如有的类需要压缩成jar文件,有的文件...
  • wangshuxuncom
  • wangshuxuncom
  • 2014年10月16日 11:00
  • 9169
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:自动化每日构建(二)用Ant来完成java工程的每日构建
举报原因:
原因补充:

(最多只允许输入30个字)