Compile the ant Script Template of the Java Web project

Source: Internet
Author: User

The advantage of process and automation tools is that we can not easily make some low-level errors while ensuring efficiency. Using ant to compile java web projects is like this, which makes it more standard and efficient in the release process. Here is a set of ant scripts I use.

I. Prerequisites

The directory structure of your code should be as follows:

Src
Resources
----- Common is the same configuration file regardless of the test environment or formal environment, such as web. xml and strtus. xml.
----- Daily test environment configuration files, such as application and database configuration files
------ Formal official environment configuration file
WebRoot ------- web Resource Directory (such as JS and img)
------- WEB-INF
= Lib dependent package
Ii. How to modify the Script Template: 1. Change apprelease to your application name. 2. <copy todir = "$ {buildtype}-$ {release. classes. dir} "node will copy the configuration file to the class directory. You need to define the file to be copied under this node and add or remove the" include "content. If you do not need to copy the file, delete the corresponding fileset Node 3 and <copy todir = "$ {buildtype}-$ {release. inf. dir} "> node, will copy the configuration file to the WEB-INF. Same as above, modified as needed
<? Xml version = "1.0" encoding = "UTF-8"?> <Project name = "apprelease" default = "build" basedir = ". "> <property name =" release. dir "value =" apprelease "/> <property name =" release. inf. dir "value =" $ {release. dir}/WEB-INF "/> <property name =" release. classes. dir "value =" $ {release. inf. dir}/classes "/> <property name =" release. lib. dir "value =" $ {release. inf. dir}/lib "/> <property name =" release. file "value =" ${release.dir0000.zip "/> <property name =" webroot. dir" Value = "WebRoot"/> <property name = "webroot. class. dir "value =" WEB-INF/classes "/> <property name =" src. dir "value =" src "/> <property name =" resources. dir "value =" resources "/> <property name =" lib. dir "value =" WEB-INF/lib "/> <property name =" classes. dir "value =" bin "/> <property name =" daily "value =" daily "/> <property name =" formal "value =" formal "/> <! -- Set the class path --> <path id = "compile. classpath "> <fileset dir =" $ {webroot. dir}/$ {lib. dir} "> <exclude name = "**/. svn "/> <exclude name =" test/"/> </fileset> </path> <target name =" build "description =" Build this project "depends =" clean, compile "> <antcall target =" buildWithType "> <param name =" buildtype "value =" $ {formal} "/> </antcall> <antcall target =" buildWithType "> <param name = "buildtype" value = "$ {daily}"/> </ Tcall> </target> <target name = "clean" description = "Remove all generated files. "> <delete dir =" $ {classes. dir} "/> <delete dir =" $ {formal}-$ {release. dir} "/> <delete file =" $ {formal}-$ {release. file} "/> <delete dir =" $ {daily}-$ {release. dir} "/> <delete file =" $ {daily}-$ {release. file} "/> </target> <target name =" compile "> <mkdir dir =" $ {classes. dir} "/> <javac encoding =" UTF-8 "srcdir =" $ {src. dir} "classpathref =" Compile. classpath "destdir =" $ {classes. dir} "/> </target> <target name =" buildWithType "description =" Build formal or daily "> <property name =" buildtype "value =" formal "/> <copy todir = "$ {buildtype}-$ {release. classes. dir} "> <fileset dir =" bin "> <exclude name = "**/. svn "/> <exclude name = "*. properties "/> <exclude name = "*. property "/> <exclude name = "*. xml "/> </fileset> <! --- Copy the configuration file from the resources/daily or resources/formal (daily) directory to the classes directory --> <fileset dir = "$ {resources. dir}/$ {buildtype} "> <include name =" log4j. properties "/> <include name =" databases. properties "/> <include name =" application. properties "/> <include name =" struts. xml "/> <include name =" shiro. ini "/> <include name =" mess. properties "/> </fileset> <! --- Copy the configuration file from the resources/common directory to the classes directory --> <fileset dir = "$ {resources. dir}/common "> <include name =" key. ini "/> <include name =" struts. xml "/> </fileset> </copy> <copy todir =" $ {buildtype}-$ {release. inf. dir} "> <! --- Copy the configuration file from the resources/common directory to the WEB-INF directory --> <fileset dir = "$ {resources. dir}/common "> <exclude name = "**/. svn "/> <include name =" web. xml "/> </fileset> <! --- Copy the configuration file from the resources/daily or resources/formal (daily) directory to the WEB-INF directory --> <fileset dir = "$ {resources. dir}/$ {buildtype} "> <include name =" beans. xml "/> </fileset> </copy> <copy todir =" $ {buildtype}-$ {release. dir} "> <fileset dir =" $ {webroot. dir} "> <exclude name = "**/. svn "/> <exclude name =" **/$ {lib. dir}/** "/> <exclude name =" **/$ {webroot. class. dir}/** "/> </fileset> </copy> <copy todir =" $ {buildtype}-$ {release. lib. dir} "flatten =" true "> <fileset dir =" $ {webroot. dir}/$ {lib. dir} "> <exclude name = "**/. svn "/> <include name = "**/*. jar "/> </fileset> </copy> <zip destfile =" $ {buildtype}-$ {release. file} "> <fileset dir =" $ {buildtype}-$ {release. dir} "/> </zip> </target> </project>

Iii. Script description

1. The. svn file will be filtered out.

2, content under the WebRoot directory, in addition to WEB-INF \ lib and WEB-INF \ classes, other content, will be copied to the final zip package

3. If you <copy todir = "$ {buildtype}-$ {release. inf. dir} "> the node defines to copy the configuration file from the resource Directory, which is subject to the resource Directory. For example, if you define resources \ common \ web under that node. copy xml to $ {buildtype}-$ {release. inf. dir}, so the web. xml is the share of resources \ common, not the share of the WebRoot directory.

4. Two daily and formal packages are generated. The difference is that daily uses resources in resources \ daily, while formal uses resources \ formal.

5. java files are fully compiled every time, so that some problems can be avoided. For example, if the static variable is modified, use ant to compile java projects, java files that use the static variable will not be recompiled.

6, WEB-INF \ lib, can exist Level 2 Directory, script execution, will be level 2 directory content, also copy to lib, this avoids the problem of "the lib level-2 Directory jar package is not loaded under the web Container ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.