Using ant_jsp programming in eclipse

Source: Internet
Author: User
Tags mkdir
Objective

Ant is an important part of the Java Developer Toolbox, and Junit,xdoclet is closely associated with it, and programmers may be accustomed to the automatic build and even deployment capabilities provided by the IDE, ignoring the ant itself, in fact, the mainstream IDE is usually built into the ant task to do the work, Familiar with Ant's internal mechanism, you can read or simply modify Build.xml can undoubtedly help you to more flexibly integrate, manage application projects, if you need to learn maven this open source project management solution, but also to understand Ant based on yo. In addition, the process of using ant actually documents the build, which is nothing about the IDE, and imagine that One-third of your colleagues may be using Jbuilderx, One-third with Eclipse, and some others.

I use eclipse3.0.1, the previous construction and release work by the MyEclipse Plug-ins, while the weekend practice a manual build, remember this memo.

   Practice

Preparation: This is my personal habit, put all the common library jar in a fixed directory, divided into classes, do not throw in a folder, such as Jakarta-commons, Hibernate, Spring, struts and so on, these are the source code to build the need to use, There may be some need to be deployed, such as Servlet.jar. If you have your own framework, put it here as well. Then, open eclipse, go into windows->preferences->java->user libraries, add a library of your own, such as Mylib, to add just the public jars, which is a good thing, in the Eclipse Project, No longer have to see the annoying long list of jars, more tidy.

To come down formally:

1. Create a new Java Project, and then do not choose your EE plug-in built-in some of the options, to Jane can.

2. Create several folders under root, which we can often see in open source projects downloaded from the Web, such as:

SRC-source
Classes-Compiling
WEB-JSP, etc.
Lib-Library, here can simply put the mylib under the Dongdong copy over, easy to release the source code in the future.
Dlist-output jar or war

Of course, we want to build a build.xml,eclipse will appear a small ant icon, generally this file is established, the next project simple copy past, a little change on it.

3. Open the Project's property page, and in the Java Build Path library option, join our customized public Library Mylib. As for the builders mode, use the default Java builer, I just use ant for project deployment, Let the IDE do the usual job of scheduling mistakes.

4. The most important, write your build.xml, online article is very sea, I here will not long-winded, basically divided into those several tasks:

4.1 First declare some path variables, such as

<property name= "War.dir" value= "Dlist"

It can also be written to the properties file, where it is referenced;

4.2 Declares the compiled classpath as follows:

<path id= "Master-classpath"
<fileset dir= "${lib.root}/struts"
<include name= "Struts-menu-2.3.jar"/>
<include name= "Struts.jar"/>
</fileset>
<fileset dir= "${lib.root}/jakarta-commons"
<include name= "Commons-*.jar"/>
</fileset>
<fileset dir= "${lib.root}/ibatis2.0.9"
<include name= "Ibatis-*.jar"/>
</fileset>
<fileset dir= "${lib.root}/jdbcdriver"
<include name= "Jtds-0.9-rc2.jar"/>
</fileset> s
......
</path>

4.3 Empty The output directory, such as Web,dlist.

4.4 Compilation Build:

<target name= "Build" description= "Compile main source tree Java files into class files, generate jar files" >

<mkdir dir= "${build.dir}"/>

<javac destdir= "${build.dir}" source= "1.3" target= "1.3" debug= "true" deprecation= "false" Optimize= "false" FailOnError = "true" >
<src path= "${src.dir}"/>
<classpath refid= "Master-classpath"/>
</javac>

<copy todir= "${build.dir}" preservelastmodified= "true" >
<fileset dir= "${src.dir}" >
<include name= "**/*.xml"/>
<include name= "**/*.properties"/>
</fileset>
</copy>
!--=============================================-->
!--It is tested that the resource file cannot be hit into the jar file and the rest can be-->
!--=============================================-->
<copy todir= "${webclasses.dir}/conf" preservelastmodified= "true"
<fileset dir= "${src.dir}/conf"
<include name= "Springresources*.properties"/>
</fileset>
</copy>

<mkdir dir= "${weblib.dir}"/>

<jar jarfile= "${weblib.dir}/${name}.jar" compress= "true"
<fileset dir= "${build.dir}" >
<include name= "* *"/>
</fileset>
</jar>

<copy todir= "${weblib.dir}" preservelastmodified= "true" >

<fileset dir= "${lib.root}" >
<include name= "Log4j-1.2.8.jar"/>
</fileset>
<fileset dir= "${lib.root}/struts"
<include name= "Struts-menu-2.3.jar"/>
<include name= "Struts.jar"/>
</fileset>
<fileset dir= "${lib.root}/jakarta-commons"
<include name= "Commons-*.jar"/>
</fileset>
<fileset dir= "${lib.root}/spring-1.1.3"
<include name= "Spring.jar"/>
<include name= "Aopalliance.jar"/>
</fileset>
......

</copy>

</target>

!--=============================================-->
!--Compile main Java sources and copy libraries-->
!--=============================================-->
<target name= "Warfile" description= "Build the Web application archive"

<mkdir dir= "${dist.dir}"/>
<war warfile= "${dist.dir}/${name}.war" basedir= "${war.dir}" webxml= "${war.dir}/web-inf/web.xml"
<include name= "*"/>
<include name= "Web-inf/*.*"/>
<exclude name= "Web-inf/web.xml"/>
<include name= "Web-inf/classes/*.*"/>
<include name= "web-inf/lib/**"/>
<exclude name= "**/.*"/>
</war>

</target>


4.5 Dozen into war

<target name= "Warfile" description= "Build the Web application archive"

<mkdir dir= "${dist.dir}"/>
<war warfile= "${dist.dir}/${name}.war" basedir= "${war.dir}" webxml= "${war.dir}/web-inf/web.xml"
<include name= "*"/>
<include name= "Web-inf/*.*"/>
<exclude name= "Web-inf/web.xml"/>
<include name= "Web-inf/classes/*.*"/>
<include name= "web-inf/lib/**"/>
<exclude name= "**/.*"/>
</war>

</target>

4.6 String up a few tasks and get a default target

<target name= "All" >
<antcall target= "clean"/>
<antcall target= "Build"/>
<antcall target= "Warfile"/>
</target>

Finish the work. In practice, some configuration files, such as Struts-config.xml Ibatis and spring XML, can be typed into the jar file, and the spring resource file does not seem to be able to be copy to Web-infclasses alone, and Your Web folder, you have to put a good web.xml, as well as some tld files yo.
Related Article

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.