This article uses an example to explain how to compile and publish projects through Ant.
This example organizes the project according to the following directory structure.
D:/web/antsample Project root directory d:/web/antsample/src source code directory D:/web/antsample/src/sample/helloworld.java sample HelloWorld code file d:/ Web/antsample/app application directory d:/web/antsample/app/index.jsp default home file d:/web/antsample/app/web-infd:/web/antsample/app/ Web-inf/web.xml Application description file d:/web/antsample/app/web-inf/lib Application Library Directory d:/web/antsample/context.xml application Environment profile, data source, Information such as application path (path), document Path (DocBase) is configured here. D:/web/antsample/build.xml
Build.xml File Contents
<?XML version= "1.0" encoding= "UTF-8"?><Projectname= "Antsample"default= "Jar"Basedir="."> < PropertyEnvironment= "env"/> <Echomessage= "Ant version ${ant.java.version}"/> < Propertyname= "Tomcat.home"value= "/home/xiaojf/tomcat6"/> <!--Tomcat Directory - < Propertyname= "Web.home"value= "./app"/> <!--application Directory - < Propertyname= "Src.home"value= "./src"/> <!--Java source Files directory - < Propertyname= "Jarfile"value= "${web.home}/web-inf/lib/${ant.project.name}.jar"/> <!--jar File Location - < Propertyname= "Warfile"value= "./${ant.project.name}.war"/> <!--War File Location - < Propertyname= "Srcdeployfile"value= "./context.xml"/> <!--Source Deployment Files - < Propertyname= "Distdeployfile"value= "${tomcat.home}/conf/catalina/localhost/${ant.project.name}.xml"/> <!--Source Deployment Files - < Propertyname= "jdk1.5"value= "/usr/lib/jvm/jdk1.5/bin/javac"/> <!--set the full path of the jdk1.5 compiler - < Propertyname= "jdk1.6"value= "/usr/lib/jvm/jdk1.6/bin/javac"/> <!--set the full path of the jdk1.6 compiler - < Propertyname= "Javacompiler"value= "${jdk1.6}"/> <!--Javac used at compile time - <!--set Classpath, which is a library file that needs to be referenced at compile time - <PathID= "Classpath"> <Filesetdir= "${web.home}/web-inf/lib"> <!--refer to all jars under Application Web-inf/lib - <includename= "**/*.jar"/> </Fileset> <Filesetdir= "${tomcat.home}/lib"> <!--refer to all jars under Tomcat/lib - <includename= "**/*.jar"/> </Fileset> </Path> <!--clears the class file that has been generated - <Targetname= "clean"> <Delete> <Filesetdir= "${src.home}"includes= "**/*.class"/> </Delete> </Target> <!--compiles all Java files under the Src.home. Fork is set to True to specify the JDK used for compilation, where jdk1.5 is used. The encoding must be set according to the encoding of the Java source file. - <Targetname= "Compile"depends= "clean"> <JavacSrcdir= "${src.home}"Debug= "On"Deprecation= "On"optimize= "Off"includes="**"Includeantruntime= "On"encoding= "UTF-8"Fork= "Yes"executable= "${javacompiler}" > <ClasspathrefID= "Classpath"/> <Compilerargvalue= "-xlint:unchecked"/> </Javac> </Target> <!--Package and place the jar under Application Web-inf/lib. - <Targetname= "Jar"depends= "Compile"> <JarDestFile= "${jarfile}"Basedir= "${src.home}"includes= "**/*.class **/*.xml"/> </Target> <Targetname= "war"depends= "Jar"> <WarDestFile= "${warfile}"Basedir= "${web.home}"/> </Target> <!--laying out applications to Tomcat - <Targetname= "Deploy"> <Copyfile= "${srcdeployfile}"ToFile= "${distdeployfile}"/> </Target> <!--uninstalling the application from Tomcat - <Targetname= "Undeploy"> <Deletefile= "${distdeployfile}"/> </Target></Project>
To run this example, you must modify the full path of the Tomcat directory in Build.xml and the corresponding information in Context.xml.
Go to D:/web/antsample, enter the ant command, compile the program, and package the generated class file into a jar and decorate it to app/web-inf/lib.
Run ant deploy to copy Context.xml to Tomcat/conf/catalina/localhost/antsample.xml and complete the program layout.
Run ant undeploy to remove the Tomcat/conf/catalina/localhost/antsample.xml file to complete the uninstallation of the program.
Now you can enter Http://localhost:8080/antsample to access the example program.
Using ant to compile and publish projects