Ant build.xml sample

本文介绍了一个Java项目的构建过程,包括使用Ant进行编译、运行JUnit测试、利用DbUnit进行数据库初始化及导出数据等步骤,并配置了代码风格检查。

 

 
 A sample of my project file. There are still some bugs with dbunit and mysql. maybe it can work with other database. But it is still a good sample of how to compile, to run junit, to manage database and to check code style. Off course, you need the relvant .jar files in right directories.

 

[ the bug in my project is that one fileld name in database table is "e-mail" which makes trouble. (can change to to e_mail) In the mean time, it is better not use key word like "class" as field name. I remeber that it makes hibernate some trouble.  And in my case of ant target "data_inital", "&sessionVariables=FOREIGN_KEY_CHECKS=0" and "transaction='true' " is required by foreign key constrains ]

build.xml

<?xml version="1.0" encoding="UTF-8"?>

<project basedir="." default="test_all" name="AdSystem">
    
<property file="build.properties"/>
    
<property environment="env"/>
    
<property name="debuglevel" value="source,lines,vars"/>
    
<property name="target" value="1.5"/>
    
<property name="source" value="1.5"/>
    
<path id="Java EE 5 Libraries.libraryclasspath">
        
<pathelement location="${ECLIPSE_HOME}/plugins/com.genuitec.eclipse.j2eedt.core_6.0.0.zmyeclipse60020070820/data/libraryset/EE_5/javaee.jar"/>
        
<pathelement location="${ECLIPSE_HOME}/plugins/com.genuitec.eclipse.j2eedt.core_6.0.0.zmyeclipse60020070820/data/libraryset/EE_5/jsf-impl.jar"/>
        
<pathelement location="${ECLIPSE_HOME}/plugins/com.genuitec.eclipse.j2eedt.core_6.0.0.zmyeclipse60020070820/data/libraryset/EE_5/jsf-api.jar"/>
        
<pathelement location="${ECLIPSE_HOME}/plugins/com.genuitec.eclipse.j2eedt.core_6.0.0.zmyeclipse60020070820/data/libraryset/EE_5/jstl-1.2.jar"/>
    
</path>
    
<path id="JUnit 4.libraryclasspath">
        
<pathelement location="${ECLIPSE_HOME}/plugins/org.junit4_4.3.1/junit.jar"/>
    
</path>

    
<path id="AdSystem.classpath">
        
<pathelement location="WebRoot/WEB-INF/classes"/>
        
<path refid="Java EE 5 Libraries.libraryclasspath"/>
        
<pathelement location="WebRoot/WEB-INF/lib/commons-logging-1.1.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/freemarker-2.3.8.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/ognl-2.6.11.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/struts2-core-2.0.6.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/xwork-2.0.4.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/antlr-2.7.6rc1.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/asm-attrs.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/asm.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/cglib-2.1.3.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/commons-collections-2.1.1.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/commons-logging-1.0.4.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/dom4j-1.6.1.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/ehcache-1.1.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/hibernate3.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/jaas.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/jaxen-1.1-beta-7.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/jdbc2_0-stdext.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/jta.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/log4j-1.2.11.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/xerces-2.6.2.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/xml-apis.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/mysql-connector-java-3.1-nightly-20051207-bin.jar"/>
        
<pathelement location="WebRoot/WEB-INF/lib/spring.jar"/>
        
<path refid="JUnit 4.libraryclasspath"/>
    
</path>

    
<taskdef resource="checkstyletask.properties"
             classpath
="lib/checkstyle/checkstyle-all-4.4.jar"/>

    
<taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpath="lib/dbunit/dbunit-2.2.jar"/>

    
<target name="clean">
        
<delete dir="WebRoot/WEB-INF/classes"/>
    
</target>

    
<target name="build" depends="clean">
        
<echo message="${ant.project.name}: ${ant.file}"/>
        
<javac debug="true" debuglevel="${debuglevel}" destdir="WebRoot/WEB-INF/classes" source="${source}" target="${target}">
            
<src path="src"/>
            
<classpath refid="AdSystem.classpath"/>
        
</javac>
        
<javac debug="true" debuglevel="${debuglevel}" destdir="WebRoot/WEB-INF/classes" source="${source}" target="${target}">
            
<src path="test_src"/>
            
<classpath refid="AdSystem.classpath"/>
        
</javac>
    
</target>

    
<!-- -->
    
<target name="data_inital">
        
<dbunit driver="com.mysql.jdbc.Driver"          
            url
="jdbc:mysql://127.0.0.1:3306/newad?useUnicode=true&amp;characterEncoding=utf-8&amp;sessionVariables=FOREIGN_KEY_CHECKS=0"          
            userid
="root"          
            password
="root">
            
<classpath refid="AdSystem.classpath"/>
            
<operation type="CLEAN_INSERT" src="lib/dbunit/data_inital.xml" transaction="true"/>
        
</dbunit>
    
</target>

    
<target name="data_export">
        
<dbunit driver="com.mysql.jdbc.Driver"          
            url
="jdbc:mysql://127.0.0.1:3306/newad?useUnicode=true&amp;characterEncoding=utf-8"          
            userid
="root"          
            password
="root">
            
<classpath refid="AdSystem.classpath"/>
            
<export dest="lib/dbunit/export.xml"/> 
        
</dbunit>
    
</target>
    
    
    
<target name="test">
        
<junit>
            
<classpath refid="AdSystem.classpath" />
            
<formatter type="plain" usefile="false" />
            
<batchtest>
                
<fileset dir="${MY_TEST_DIRECTORY}">
                    
<include name="**/*.java"/>
                
</fileset>
            
</batchtest>
        
</junit>
    
</target>

    
<target name="checkstyle">
        
<checkstyle config="lib/checkstyle/cgogo_check.xml" classpath="lib/checkstyle/checkstyle-all-4.4.jar">
            
<fileset dir="${MY_CHECK_DIRECTORY}" includes="**/*.java" excludes="dao.model/*.*"/>
        
</checkstyle>
    
</target>

    
<target name="test_all">
        
<junit>
            
<classpath refid="AdSystem.classpath" />
            
<formatter type="plain" usefile="false" />
            
<batchtest>
                
<fileset dir="test_src">
                    
<include name="**/*.java"/>
                
</fileset>
            
</batchtest>
        
</junit>
    
</target>

    
<target name="checkstyle_all">
        
<checkstyle config="lib/checkstyle/cgogo_check.xml" classpath="lib/checkstyle/checkstyle-all-4.4.jar">
            
<fileset dir="src" includes="**/*.java" excludes="dao/model/**/*.java"/>
        
</checkstyle>
    
</target>

    
<target name="all" depends="build, test_all, checkstyle_all" />

</project>

 build.properties

ECLIPSE_HOME=D:/Program Files/MyEclipse 6.0/eclipse

MY_TEST_DIRECTORY
=test_src/dao
MY_CHECK_DIRECTORY
=src/helper

cgogo_check.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.1//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_1.dtd"
>

<module name="Checker">

    
<module name="TreeWalker">

        
<!--  
            <module name="RegexpHeader">
            <property name="headerFile"
            value="lib/checkstyle/java_head.txt" />
            <property name="multiLines" value="11" />
            </module>
        
-->

        
<module name="PackageName">
            
<property name="format" value="^[a-z]+(.[a-z][a-z0-9]*)*$" />
        
</module>
        
        
<module name="RedundantImport"/>
        
        
<!-- 检查作者注释 @author -->
        
<module name="JavadocType">
            
<property name="authorFormat" value="S" />
        
</module>
        
        
<!-- 检查产生日期注释 @since -->
        
<module name="WriteTag">
               
<property name="tag" value="@since"/>
               
<property name="severity" value="error"/>
        
</module>

        
<!-- 方法长度 50  -->
        
<module name="MethodLength">
            
<property name="tokens" value="METHOD_DEF" />
            
<property name="max" value="50" />
        
</module>

        
<!-- 内部类最大20行 -->
        
<module name="AnonInnerLength">
            
<property name="max" value="20" />
        
</module>

        
<!--System.out.print 输出 -->
        
<module name="GenericIllegalRegexp">
            
<property name="format" value="System.out.print" />
            
<property name="message"
                value
="bad practice of use System.out.print" />
        
</module>

    
</module>

</module>
It take me two days to figure out the problem. If you develop a java application  with JRE installed. the JRE can get a default TimeZone informtion. But if you jar your application with private JRE. this private JRE has no default TimeZone. Without setting default TimeZone, your time may be confusing.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值