java ant build.xml文件详细解释

<?xml version="1.0" encoding="UTF-8"?>                                    
                                     
<project name="ant_firsttest" default="dist" basedir=".">                                    
  <description>ant firsttest!</description>                                    
                                        
  <!-- set global properties for this build -->        
  <!--设定变量,之后用。location为文件夹路径-->                            
  <property name="src" location="src"/>                                    
  <property name="build" location="build"/>                                    
  <property name="dist"  location="dist"/>                                    
  <property name="web"  location="web"/>                                    
           
  <!--设置properties文件位置.这里没用到。-->                                       
  <!--<property file="nbproject/project.properties"/>-->                                    

  <!--初始化命令-->                                    
  <target name="init">                                    
    <!-- Create the time stamp -->                                    
    <tstamp/>                                    
                
    <!--mkdir是建立文件夹,${build}即刚才设定的变量。这几行都在干这事。-->    
    <!-- Create the build directory structure used by compile -->                                    
    <mkdir dir="${build}/WEB-INF/lib"/>                                    
    <mkdir dir="${build}/WEB-INF/classes"/>                                    
                                          
    <mkdir dir="${build}/WEB-INF/classes/javafile/package1"/>                                          
    <mkdir dir="${build}/WEB-INF/classes/javafile/package2"/>                                               
                                         
  </target>                                    

  <!--编译-->                                    
  <target name="compile" depends="init"                                    
        description
="compile the source " >                                    
                                            
    <!-- Compile the java code from ${src} into ${build} -->                                    
    <!--javac标签用来设置编译程序的参数,srcdir为java文件路径,destdir为编译后class文件的保存路径。-->
    <javac srcdir="${src}/javafile/package1" destdir="${build}/WEB-INF/classes/javafile/package1"/>                                    
    <javac srcdir="${src}/javafile/package2" destdir="${build}/WEB-INF/classes/javafile/package2"/>                                    
    <!--如果路径下还有别的文件需要一起打包,用copy 命令。-->    
    <copy file="${src}/hello_ant.xml" tofile="${build}/WEB-INF/classes/hello_ant.xml" />                                                                             
                                        
  </target>                                    
            
  <!--编译后就要打包了。-->                                    
  <target name="dist" depends="compile"                                    
        description
="generate the distribution" >                                    
    <!-- Create the distribution directory -->                                    
    <mkdir dir="${dist}"/>                                    
                      
       <!--像jsp,jar这些直接用不用编译的文件,直接用copy命令。-->                                
    <copy file="${web}/image/a.gif" tofile="${build}/image/a.gif" />                                    
    <copy file="${web}/WEB-INF/web.xml" tofile="${build}/WEB-INF/web.xml" />                                     
    <copy file="${web}/WEB-INF/lib/a.jar" tofile="${build}/WEB-INF/lib/a.jar" />                                          
    <copy file="${web}/index.jsp" tofile="${build}/index.jsp" />                                     
            
    <!--最后用jar命令打成jar/war文件,文件名和后缀随便起。basedir为欲打包的原文件路经-->                                    
    <jar jarfile="${dist}/ant_firsttest.jar" basedir="${build}"/>                                    
  </target>                                    
    
  <!--删除-->
  <target name="clean"                                    
        description
="clean up" >                                    
    <!--设定删除命令要删的路径。-->    
    <!-- Delete the ${build} and ${dist} directory trees -->                                    
    <delete dir="${build}"/>                                    
    <delete dir="${dist}"/>                                    
  </target>                                    
</project>                                        

4.控制台在firsttest目录,输入ant后回车就打包完成了!
  注:输入ant回车自动执行init,compile,dist命令。要想执行clean命令,输入ant clean即可。


你可能感兴趣的:(java ant build.xml文件详细解释)