Use ANT in Eclipse

Source: Internet
Author: User

Preface

Ant is an important part of the java developer toolkit. junit and xdoclet are closely related to it. programmers may get used to the automatic building and deployment functions provided by IDE, thus ignoring ant itself, in fact, the mainstream IDE usually has built-in ant tasks to complete these tasks. Familiar with the internal mechanism of ant, you can read or simply modify build. xml can undoubtedly help you more flexibly integrate and manage application projects. If you need to learn the open source project management solution such as maven, you also need to understand ant. In addition, the process of using ant is actually documented for the build. It is not about IDE. Imagine that 1/3 of your colleagues may be using JbuilderX and 1/3 using eclipse, there are others.

I used eclipse3.0.1. I used the myeclipse plug-in for previous build and release work. I tried to build it manually over the weekend and remember this memo.

  Practice

Preparation: This is my personal habit. Put all the public class libraries in a fixed directory, classify classes, and do not leave them in a folder, such as jakarta-commons, hibernate, spring, struts, etc. These are required for source code construction. Some may not need to be re-called during deployment, such as servlet. jar. If you have your own framework, put it here. Then, open eclipse, go to Windows-> Preferences-> Java-> User Libraries, add a library of your own, such as mylib, and add all the public jar files just now, this is a benefit. In the eclipse project, you no longer need to see the annoying long jar list, which is neat.

And then proceed:

1. Create a New Java Project. In this case, do not select some of the built-in options of your j2ee plug-in.

2. Create Several folders under root. We often see these in open-source projects downloaded from the Internet, such:

Src-source code
Classes-compile
Web-jsp, etc.
Lib-library. Here we can simply copy the stuff under mylib to facilitate future source code release.
Dlist-jar or war output

Of course, we need to build a build. xml file, and a small ant icon will appear in eclipse. Generally, after this file is created, the next project simply copies it and you can just make a slight change.

3. open the properties page of the project and add the custom Public Library mylib to the library options of Java Build Path. as for the Builders method, you don't need to change it. Use the default Java Builer. I only use ant for project deployment. I will leave the troubleshooting work to IDE.

4. The most important thing is to write your build. xml. There are a lot of articles on the Internet. I will not talk about it here. Basically, there are several tasks:

4.1 declare some PATH variables first, such

<Property name = "war. dir" value = "dlist"/>

You can also write it to the properties file and reference it here;

4.2 declare the compiled class path 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 clear the output directory, such as web and dlist.

4.4 compile and 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>
<! -- ===================================================== =======-->
<! -- According to the test, 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 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 concatenate several tasks and create a default target

<Target name = "all">
<Antcall target = "clean"/>
<Antcall target = "build"/>
<Antcall target = "warfile"/>
</Target>

After finishing your work. In practice, it is found that some configuration files, such as struts-config.xml ibatis and spring xml can be imported into the jar file, spring resource file does not seem to work, you have to copy to WEB-INFclasses separately, in addition, in your web folder, you must set the web in advance. xml and some tld files.

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.