Ant自动化构建项目

1 普通java项目



<?xml version="1.0" encoding="UTF-8"?>
<project default="ftp">
    <property name="src.dir" location="src"></property>
    <property name="test.src.dir" location="test"></property>
    <property name="lib.dir" location="lib"></property>
    <property file="build.properties"></property>
    
    <property name="build.dir" location="build"></property>
    <property name="build.classes" location="${build.dir}/classes"></property>
    <property name="build.test.dir" location="${build.dir}/test"></property>
    <property name="build.test.classes" location="${build.test.dir}/classes"></property>
    <property name="build.test.report" location="${build.test.dir}/report"></property>
    <property name="build.jar.dir" location="${build.dir}/dist"></property>
    <property name="build.zip.dir" location="${build.dir}/zip"></property>
    <property name="build.doc.dir" location="${build.dir}/doc"></property>
    <property name="build.src" location="${build.dir}/src"></property>
    <property name="metadata.dir" location="metadata"/>
    
    <path id="compile-classpath">
        <fileset dir="${lib.dir}" includes="*.jar"></fileset>
    </path>
    
    <path id="compile-test-classpath">
        <path refid="compile-classpath"></path>
        <pathelement location="${build.classes}"/>
    </path>
    
    <path id="run-test-classpath">
        <path refid="compile-test-classpath"/>
        <pathelement location="${build.test.classes}"/>
    </path>
    
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>
    
    <target name="init" depends="clean,metadata">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes}"/>
        <mkdir dir="${build.test.dir}"/>
        <mkdir dir="${build.test.classes}"/>
        <mkdir dir="${build.test.report}"/>
        <mkdir dir="${build.jar.dir}"/>
        <mkdir dir="${build.zip.dir}"/>
        <mkdir dir="${build.doc.dir}"/>
        <mkdir dir="${build.src}"/>
        <mkdir dir="${metadata.dir}"/>
    </target>
    
    <target name="compile" depends="init">
        <javac destdir="${build.classes}" srcdir="${src.dir}" 
            classpathref="compile-classpath" 
               includeantruntime="true" failonerror="true"></javac>
        <copy todir="${build.classes}">
            <fileset dir="${src.dir}" includes="**/*.*" excludes="**/*.java"></fileset>
        </copy>
    </target>
    
    <target name="compile-test" depends="compile">
        <javac destdir="${build.test.classes}" srcdir="${test.src.dir}"
               includeantruntime="true" failonerror="true" 
               classpathref="compile-test-classpath"></javac>
    </target>
    
    <target name="run-test" depends="compile-test">
        <junit fork="true" haltonfailure="false" failureproperty="junit.fail">
            <classpath refid="run-test-classpath"></classpath>
            <formatter type="brief" usefile="false"/>
            <formatter type="xml"/>
            <batchtest todir="${build.test.report}">
                <fileset dir="${build.test.classes}" includes="${run.test.class}"></fileset>
            </batchtest>
        </junit>
        <junitreport todir="${build.test.report}">
            <fileset dir="${build.test.report}" includes="TEST-*.xml"></fileset>
            <report format="frames" todir="${build.test.report}/html"/>
        </junitreport>
        <fail if="${junit.fail}" message="单元测试失败,具体情况请查询${build.test.report}"/>
    </target>
    
    <target name="doc" depends="run-test">
        <javadoc sourcepath="${src.dir}" use="true" packagenames="cn.*" 
               charset="UTF-8" encoding="UTF-8" docencoding="UTF-8"
               destdir="${build.doc.dir}">
            <classpath refid="compile-classpath"></classpath>
        </javadoc>
    </target>
    
    <target name="jar" depends="doc">
        <tstamp>
            <format property="now" pattern="yyyy=MM-dd HH:mm:ss"/>
        </tstamp>
        <jar destfile="${build.jar.dir}/${project.jar.name}" 
                basedir="${build.classes}" duplicate="preserve">
            <manifest>
                <attribute name="Build-By" value="${user.name}"/>
                <attribute name="Build-time" value="${now}"/>
            </manifest>
        </jar>        
    </target>
    
    <target name="copy-src" depends="jar">
        <copy todir="${build.src}">
            <fileset dir="${src.dir}" includes="**/*.*"></fileset>
        </copy>
    </target>
    
    
    <target name="metadata">
        <mkdir dir="${metadata.dir}"/>
        <buildnumber/>
        <propertyfile file="${metadata.dir}/build.propterties">
            <entry key="Build-time" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss"/>
            <entry key="build-number" type="int" value="${build.number}"/>
        </propertyfile>
    </target>
    
    <target name="zip" depends="copy-src">
        <zip destfile="${build.zip.dir}/${project.zip.name}" duplicate="preserve">
            <zipfileset file="${build.jar.dir}/${project.jar.name}" prefix="${project.prefix}"/>
            <zipfileset dir="${build.src}" includes="**/*.*" prefix="${project.prefix}/src"></zipfileset>
            <zipfileset dir="${build.doc.dir}" includes="**/*.*" prefix="${project.prefix}/doc/api"></zipfileset>
            <zipfileset dir="${build.test.dir}" includes="**/*.*" 
                    excludes="report/*.*" prefix="${project.prefix}/test"></zipfileset>
            <zipfileset dir="." includes="build.xml,build.properties" prefix="${project.prefix}"></zipfileset>
        </zip>
    </target>
    
    <presetdef name="upload">
        <ftp userid="${ftp.username}" password="${ftp.password}" 
             server="${ftp.server}" remotedir="${ftp.dir}"></ftp>
    </presetdef>
    
    <target name="ftp" depends="zip">
        <upload action="mkdir"/>
        <upload action="put">
            <fileset dir="${build.zip.dir}" includes="${project.zip.name}"></fileset>
            </upload>
    </target>
    
</project>



build.properties

ftp.username = kh
ftp.password = 123
ftp.server = localhost
ftp.dir = user_manager
project.version = SNAPSHOT-0.1
project.prefix = usermanager-${project.version}
project.jar.name = ${project.prefix}.jar
project.zip.name = ${project.prefix}.zip
run.test.class = **/Test*.class


2 web项目

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <property name="src.dir" location="src"></property>
    <property name="build.dir" location="build"></property>
    <property name="build.classes" location="${build.dir}/classes"></property>
    <property name="build.war" location="${build.dir}/war"></property>
    
    <property name="web.name" value="hello"></property>
    <property name="web.root" value="WebContent"></property>
    <property name="web.WEB-INF" location="${web.root}/WEB-INF"></property>
    <property name="web.lib" location="${web.WEB-INF}/lib"></property>
    <property environment="env"></property>
    
    <path id="compile">
        <fileset dir="${web.lib}" includes="*.jar"></fileset>
        <fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"></fileset>
    </path>
    
    <target name="init">
        <delete dir="${build.dir}"></delete>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes}"/>
        <mkdir dir="${build.war}"/>
    </target>
    
    <target name="compile" depends="init">
        <javac destdir="${build.classes}" srcdir="${src.dir}" classpathref="compile"></javac>
    </target>
    
    <target name="war" depends="compile">
        <war destfile="${build.war}/${web.name}.war">
            <fileset dir="${web.root}" includes="**/*.*"></fileset>
            <lib dir="${web.lib}"></lib>
            <webinf dir="${web.WEB-INF}"></webinf>
            <classes dir="${build.classes}"></classes>
        </war>
    </target>
    
    <presetdef name="sql-admin">
        <sql userid="root" password="123456" url="jdbc:mysql://localhost:3306/itat_msg?useUnicode=true&amp;characterEncoding=utf-8" driver="com.mysql.jdbc.Driver">
            <classpath refid="compile"></classpath>
        </sql>
    </presetdef>
    
    <target name="init-mysql">
        <sql-admin>
            <transaction>
                drop database ant_test;
                create database ant_test;
                GRANT ALL ON ant_test.* TO "ant_test"@"localhost" IDENTIFIED BY '123'
            </transaction>
        </sql-admin>
    </target>
    
    <target name="deploy" depends="war">
        <copy todir="${env.CATALINA_HOME}/webapps">
            <fileset dir="${build.war}" includes="${web.name}.war"></fileset>
        </copy>
    </target>
</project>


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1751564

上一篇:java jvm内存管理/gc策略/参数设置


下一篇:JVM内存管理------垃圾搜集器参数精解