一个基于Maven项目Ant的构建模板

一般的我创建的项目都是基于Maven的,但是很多人也喜欢用Ant打包jar,或者war.我一般都会在项目中既包含Maven的pom.xml,也会有ant的build.xml

只要在控制台执行:


mvn dependency:copy-dependencies
然后执行:



ant OR ant javadoc
就会打包程序或者生成项目的Javadoc文档.


打包的程序一般在:${project.path}/target/dist/${version}/

JavaDoc文档一般在${project.path}/target/ant/javadoc/

一般执行mvn dependency:copy-dependencies会把依赖的jarcopy到${project.path}/target/dependency/目录下.可以很容易的利用好Ant.

build.xml


<?xml version="1.0" encoding="UTF-8"?>
<project name="dsolrcloud" default="default" basedir=".">
    <description>Builds, tests, and runs the project dsolrcloud.</description>

    <property environment="env"/>

    <property name="version" value="1.0"/>
    <property name="project" value="dsolrcloud"/>
    <property name="package" value="dsolrcloud"/>
    <property name="domain" value="net"/>
    <property name="author" value="ZhenQin"/>
    <property name="vendor" value="ZhenQin"/>

    <property name="src.dir" location="src/main/java"/>
    <property name="build.dir" location="target/ant/classes" />
    <property name="dist.dir" location="target/dist"/>

    <property name="config.dir" location="src/main/resources" />
    <property name="doc.dir" location="target/ant/javadoc"/>
    <property name="web.root" location="src/main/webapp" />

    <property name="lib.dir" location="target/dependency"/>
    <property name="test.dir" location="src/test/java"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>

    <target name="makedir" description="建立文件夹">
        <mkdir dir="${dist.dir}"/>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${lib.dir}"/>
        <mkdir dir="${test.dir}"/>
    </target>

    <target name="compile" depends="makedir" description="编译代码">
        <!-- 编译所有的代码,Server and client -->
        <javac srcdir="${src.dir}"
               destdir="${build.dir}"
               encoding="UTF-8"
               includeantruntime="no"
               excludes="**/test/*,**/test/bean/*,**/*.svn,*.svn"
               includes="**/*.java"
               source="1.6"
               target="1.6"
               deprecation="true"
               failonerror="true"
               debug="true">
            <classpath refid="classpath" />
        </javac>

        <!-- 把所有文件copy到build目录 -->
        <copy todir="${build.dir}">
            <fileset dir="${config.dir}" includes="**/*"/>
        </copy>
    </target>

    <!--生成Javadoc 文档 -->
    <target name="javadoc" description="生成Javadoc文档.">
        <delete dir="${doc.dir}"/>
        <mkdir dir="${doc.dir}"/>
        <javadoc destdir="${doc.dir}" windowtitle="${project} Javadoc" use="yes"
                 linksource="no" splitindex="yes" encoding="UTF-8"
                 docencoding="UTF-8" locale="zh_CN">
            <classpath refid="classpath"/>
            <packageset dir="${src.dir}" defaultexcludes="yes">
                <include name="${domain}/${package}/**"/>
            </packageset>
        </javadoc>
    </target>

    <target name="package" depends="compile" description="打包成Jar" >
        <mkdir dir="${dist.dir}/${version}" />
        <tstamp>
            <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss"/>
            <format property="TIME" pattern="yyMMddHHmm"/>
        </tstamp>

        <manifest file="${web.root}/META-INF/MANIFEST.MF">
            <attribute name="Created-By" value="${author}"/>
            <attribute name="Built-Date" value="${TODAY}"/>
            <attribute name="Implementation-Title" value="${project}"/>
            <attribute name="Implementation-Version" value="${version}"/>
            <attribute name="Implementation-Vendor" value="${vendor}"/>
        </manifest>

        <!-- 打包所有的编译文件 -->
        <jar jarfile="${dist.dir}/${version}/${domain}.${package}.${project}.bin.${version}.v${TIME}.jar"
             basedir="${build.dir}" manifest="${web.root}/META-INF/MANIFEST.MF">
        </jar>

        <!-- 生成Web配置及部署文件 -->
        <war destfile="${dist.dir}/${version}/${project}.v${TIME}.war"
             webxml="${web.root}/WEB-INF/web.xml" manifest="${web.root}/META-INF/MANIFEST.MF">
            <fileset dir="${web.root}" excludes="${web.root}/WEB-INF/classes/*"></fileset>
            <classes dir="${build.dir}" />
            <lib dir="${lib.dir}" includes="**/*.jar" />
        </war>

        <!--
          <jar jarfile="${dist.dir}/${version}/${domain}.${package}.${project}.client.bin.${version}.v${TIME}.jar"
               basedir="${client.build.dir}" manifest="${web.root}/META-INF/MANIFEST.MF">
          </jar>
          <jar jarfile="${dist.dir}/${version}/${domain}.${project}.sources.${version}.jar"
               basedir="${src.dir}" manifest="${web.root}/META-INF/MANIFEST.MF">
          </jar>
          <jar jarfile="${dist.dir}/${version}/${domain}.${project}.testsource.${version}.jar"
              basedir="${test.dir}" manifest="${web.root}/META-INF/MANIFEST.MF">
          </jar>

          <jar jarfile="${dist.dir}/${version}/${domain}.${project}.javadoc.${version}.jar"
              basedir="${doc.dir}" manifest="${web.root}/META-INF/MANIFEST.MF"/>
          -->

    </target>

    <target name="destory" description="最后的清理" >
        <delete  includeemptydirs="true">
            <fileset dir="${build.dir}" includes="**/*" />
        </delete>
    </target>
    <target name="default" depends="package, destory">

    </target>
</project>



你可能感兴趣的:(ant,Ant构建)