Demo in Ant build.xml

Build.xml
 
Case one
< project >

         < target name ="clean" >
                 < delete dir ="build" />
         </ target >

         < target name ="compile" >
                 < mkdir dir ="build/classes" />
                 < javac srcdir ="src" destdir ="build/classes" />
         </ target >

         < target name ="jar" >
                 < mkdir dir ="build/jar" />
                 < jar destfile ="build/jar/HelloWorld.jar" basedir ="build/classes" >
                         < manifest >
                                 < attribute name ="Main-Class" value ="demo.HelloWorld" />
                         </ manifest >
                 </ jar >
         </ target >

         < target name ="run" >
                 < java jar ="build/jar/HelloWorld.jar" fork ="true" />
         </ target >

</ project >
 
Case two
 
< project name ="antdemo" basedir ="." default ="deploy" >

  <!-- Project settings -->
   < property name ="project.distname" value ="antdemo" />
   < property name ="src.dir" value ="src" />
   < property name ="lib.dir" value ="lib" />

  <!-- Local system paths -->
   < property name ="build.dir" value ="build" />
   < property name ="classes.dir" value ="${build.dir}/classes" />
   < property name ="jar.dir" value ="${build.dir}/jars" />

   < property name ="main-class" value ="demo.HelloWorld" />

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

   < target name ="clean" >
     < delete dir ="${build.dir}" />
   </ target >

   < target name ="compile" >
     < mkdir dir ="${classes.dir}" />
    <!-- In javac, we should improt the chasspathref which needed. -->
     < javac srcdir ="${src.dir}" destdir ="${classes.dir}"   classpathref ="classpath" />    
     < copy todir ="${classes.dir}" >
                         < fileset dir ="${src.dir}" excludes ="**/*.java" />
                 </ copy >
    
   </ target >

  <!-- Create jar -->
   < target name ="jar" depends ="compile" >
     < mkdir dir ="${jar.dir}" />
     < jar destfile ="${jar.dir}/${ant.project.name}.jar" basedir ="${classes.dir}" >
       < manifest >
         < attribute name ="Main-Class" value ="${main-class}" />
       </ manifest >
     </ jar >
   </ target >

   < target name ="run" depends ="jar" >
    <!-- <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>-->
    <!-- set classpath -->
     < java fork ="true" classname ="${main-class}" >
       < classpath >
         < path refid ="classpath" />
         < path location ="${jar.dir}/${ant.project.name}.jar" />
       </ classpath >
     </ java >
   </ target >

   < target name ="clean-build" depends ="clean,jar" />

   < target name ="deploy" depends ="clean,run" />


</ project >
 
Compare two cases, in front one, which just defind some single target, we should call them name clearly, each target have no relationship.
 
While in case two, it much more complex, which defined some environment variables, and defined each target relationship, and most improtant is in case two, which have use some extern libs, so in which build.xml, we should user "classpath" to describe them, and in complie target, we should set javac's classpath, if not, when you run it, it will throw some lib not found exceptions.
 
Source Code For DownLoad

你可能感兴趣的:(ant,职场,build.xml,休闲)