Using ant in eclipse

Time:2021-12-24

preface

Ant is an important part of java developer toolbox. JUnit and XDoclet are closely related to it. Programmers may be used to the automatic construction and even deployment functions provided by IDE, thus ignoring ant itself. In fact, mainstream ides usually have built-in ant tasks to complete these tasks. They are familiar with the internal mechanism of ant, and can read or simply modify build XML can undoubtedly help you integrate and manage application projects more flexibly. If you need to learn maven, an open source project management solution, it should also be based on understanding ant. In addition, the process of using ant actually documents the build. It is not about ide. Imagine that one third of your colleagues are using jbuilderx, one third are using eclipse, and some are others.

I use eclipse 3 0.1, the previous construction and release work were done by the MyEclipse plug-in. I practiced manual construction at the weekend and remember this note.

  practice

Preparatory work: This is my personal habit. Put all public class libraries jars in a fixed directory and divide them into classes. Don’t leave them in one folder, such as Jakarta commons, hibernate, spring, struts, etc. These are needed for source code construction. You don’t need to type them in during deployment, such as servlet jar。 If you have your own framework, you can also put it here. Then, open eclipse, enter windows – > Preferences – > java – > User libraries, add a library of your own, such as mylib, and add all the public jars just now. This has the advantage that in the eclipse project, you don’t need to see the annoying long jar list, which is cleaner.

It will be officially carried out:

  1. Create a new Java project. At this time, don’t select some built-in options of your J2EE plug-in. It’s as simple as possible.

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

SRC – source code
Classes – compile
Web – JSP, etc
Lib – library. Here you can simply copy the Dongdong under mylib to facilitate the release of source code in the future.
DLIST – jar or war output

Of course, we’re going to build a build XML, a small icon of ant will appear in eclipse. Generally, after this file is established, the next project will simply copy it and make a little change.

  3. Open the property page of the project, and add our customized public library mylib. In the Library Option of Java build path As for the builders method, you don’t have to change it. Just use the default Java builder. I just use ant during project deployment. I’ll leave the usual troubleshooting to the IDE.

  4. Top priority, write your build XML, there are a lot of articles on the Internet. I won’t be wordy here. I basically divide the tasks into:

4.1 first declare some path variables, such as

  <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>
  <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, DLIST, etc.

4.4 compilation and construction:

  <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 typed 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 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 several tasks to get a default target

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

Finish the work. In practice, it is found that some configuration files, such as struts config XML, ibatis and spring XML can be typed into jar files. Spring resource files don’t seem to work. You have to copy them separately to web-infclasses. In addition, you have to put web in your web folder in advance XML, and some TLD files.