SoFunction
Updated on 2025-04-04

Using ANT in Eclipse

Preface

ant is an important part of the Java developer toolbox. Junit, xdoclet, etc. are closely related to it. Programmers may be accustomed to the automatic construction and even deployment functions provided by the IDE, thus ignoring the ant itself. In fact, mainstream IDEs usually have built-in ant tasks to complete these tasks. Being familiar with the internal mechanism of ant, reading or simply modifying can undoubtedly help you integrate and manage application projects more flexibly. If you need to learn open source project management solutions like maven, you should also be based on understanding ant. Also, the process of using ant actually documented the build, it's not about the IDE, imagine that maybe one-third of your colleagues are using JbuilderX, one-third use eclipse, and some are other.

I use eclipse3.0.1. The previous construction and release work were done by the myeclipse plugin. I practiced manual construction on the weekend and remembered this memo.

practice

Preparation: This is my personal habit. Put all public class library jars in a fixed directory and classify them well. Do not throw them in a folder, such as jakarta-commons, hibernate, spring, struts, etc. These are what you need to use when building the source code. Some of them may not need to be entered during deployment, such as. If you have your own framework, put it here too. Then, open eclipse, enter Windows->Preferences->Java->User Libraries, and add your own library, such as mylib, and add all the public jars just now. This has an advantage. In the eclipse project, you no longer have to see annoying long list of jars, which is relatively neat.

Go down to formally proceed:

1. Create a new Java Project. At this time, don’t choose some built-in options in your j2ee plug-in, just simple.

2. Create several folders under root. We can often see these in open source projects downloaded online, such as:

src - Source code
classes - Compile
web - jsp etc.
lib - library, here you can simply copy the things under mylib to facilitate the release of source code in the future.
dlist - output jar or war

Of course, we need to create one, and a small ant icon will appear in eclipse. Generally, after this file is created, the next project will be copied in a simple way and a little change is enough.

3. Open the project's property page, add our customized public library mylib in the library options of Java Build Path. As for the Builders method, you don't need to change it. Just use the default Java Builer. I just use ant when deploying the project. I will leave the usual troubleshooting work to the IDE.

4. The top priority is to write about you. The online articles are very popular. I won’t talk about them anymore. Basically, I will divide them into several tasks:

4.1 First declare some path variables, such as

<property name="" value="dlist" />

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

4.2 Declare the compiled classpath, as follows:

<path >
<fileset dir="${}/struts">
<include name="struts-menu-2." />
<include name="" />
</fileset>
<fileset dir="${}/jakarta-commons">
<include name="commons-*.jar" />
</fileset>
<fileset dir="${}/ibatis2.0.9">
<include name="ibatis-*.jar" />
</fileset>
<fileset dir="${}/jdbcdriver">
<include name="jtds-0." />
</fileset>s
......
</path>

4.3 Clear the output directory, such as web, dlist, etc.

4.4 Compile and build:

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

<mkdir dir="${}" />

<javac destdir="${}" source="1.3" target="1.3" debug="true" deprecation="false" optimize="false" failonerror="true">
<src path="${}" />
<classpath ref />
</javac>

<copy todir="${}" preservelastmodified="true">
<fileset dir="${}">
<include name="**/*.xml" />
<include name="**/*.properties" />
</fileset>
</copy>
<!-- ============================================= -->
<!-- According to tests, resource files cannot be typed into jar files, and the rest can be -->
<!-- ============================================= -->
<copy todir="${}/conf" preservelastmodified="true">
<fileset dir="${}/conf">
<include name="springResources*.properties" />
</fileset>
</copy>

<mkdir dir="${}" />

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

<copy todir="${}" preservelastmodified="true">

<fileset dir="${}">
<include name="log4j-1.2." />
</fileset>
<fileset dir="${}/struts">
<include name="struts-menu-2." />
<include name="" />
</fileset>
<fileset dir="${}/jakarta-commons">
<include name="commons-*.jar" />
</fileset>
<fileset dir="${}/spring-1.1.3">
<include name="" />
<include name="" />
</fileset>
......

</copy>

</target>

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

<mkdir dir="${}" />
<war warfile="${}/${name}.war" basedir="${}" webxml="${}/WEB-INF/">
<include name="*" />
<include name="WEB-INF/*.*" />
<exclude name="WEB-INF/" />
<include name="WEB-INF/classes/*.*" />
<include name="WEB-INF/lib/**" />
<exclude name="**/.*" />
</war>

</target>


4.5 Typing into war

<target name="warfile" description="Build the web application archive">

<mkdir dir="${}" />
<war warfile="${}/${name}.war" basedir="${}" webxml="${}/WEB-INF/">
<include name="*" />
<include name="WEB-INF/*.*" />
<exclude name="WEB-INF/" />
<include name="WEB-INF/classes/*.*" />
<include name="WEB-INF/lib/**" />
<exclude name="**/.*" />
</war>

</target>

4.6 String up several tasks and create a default target

<target name="all">
<antcall target="clean" />
<antcall target="build" />
<antcall target="warfile" />
</target>

Finish the work after the fight. In practice, I found that some configuration files, such as ibatis and spring xml, can be entered into jar files, and the spring resource file seems to be not good, so you have to copy them separately to WEB-INFclasses. In addition, you have to put them in advance in your web folder, as well as some tld files.