ant实现java项目自动构建并打包war

ant实现java项目自动构建并打包war

基于netbeans,打包war

1.目录结构

        proA


        proB 
            ... src                 JAVA源码编辑目录  
            ... WebRoot             web文件存放地方  
                ... WEB-INF   
                    ...lib          jar包(类库)存放目录  
            ... build               编译生成的class文件存放目录  
            ... dist                war和javadoc存放目录  
            ... build.xml           ant脚本  

目的是构建proB,但proB的编译需要依靠proA的构建jar,只需要在proA构建成功后拷贝至proB的lib目录下进行引用

2.build.xml如下


<project name="proB"  default="war" basedir=".">
    <property name="src.dir" value="src" />
    <property name="lib.dir" value="WEB-INF/lib" />
    <property name="web.dir" value="web" />
    <property name="web-inf.dir" value="${web.dir}/WEB-INF" />
    <property name="build.dir" value="build" />
    <property name="dist.dir" value="dist" />
    <property name="classes" value="build/classes"/>
    <property name="libjar.dir" value="lib" />
    <property name="truck" value="../" />   

    
    <path id="project.classpath">
        <fileset dir="${libjar.dir}">
            <include name="**/*.jar" />
        fileset>
        <pathelement location="${build.dir}/classes" />
    path>

    
    <target name="copy" >
        <copy todir="${libjar.dir}" file="${truck}/proA/build/proA.jar" />
    target>

    
    <target name="clear" depends="copy">
        <delete dir="${build.dir}" />
    target>

    
    <target name="init" depends="clear">
        <mkdir dir="${classes}" />
    target>   


    
    <target name="compile" depends="init">
        <javac srcdir="src" destdir="${classes}">
            <compilerarg line="-encoding UTF-8 -verbose" />
            <classpath refid="project.classpath" />
        javac>
    target>

    
    <target name="copyjar" depends="compile">
        <copy todir="${web.dir}/${lib.dir}">
            <fileset dir="${libjar.dir}">
            fileset>
        copy>     
    target>

    
    <target name="war" depends="copyjar">
       <war destfile="${dist}/tdtb.war" webxml="web/WEB-INF/web.xml">
           
           <fileset dir="${web.dir}"/>

           
           <lib dir="${web.dir}/${lib.dir}" />
           
           <classes dir="${classes}"/>
       war>
    target>

project>

3.构建成功后的war包结构

            proB
                ···META-INF
                ···WEB-INF
                    ···class
                    ···lib
                    ···web.xml
                ···redirect.jsp

具体参数参考

http://tech.it168.com/jd/2007-11-09/200711091344781_all.shtml

你可能感兴趣的:(ant)