Use ANT to assist in developing Java projects

zhaozj2021-02-16  75

ANT is an open source project of Apache, currently widely adopted in Java project development, and is very powerful! So we need to be familiar with this Java-based tool.

For related introductions about Ant, please refer to www.apache.org.

First we should install Ant, you can use it directly to the machine after downloading from apache.org. It is best to set the relevant environment variable, although many systems can be automatically identified. You should still set up Ant_Home, Java_Home, these environment variables, such as your Ant installation in C: / da Da1.6, installed in C: /J2SDK1.4.2. So we can perform the following operations Set the environment variable ( WinXP): SET ANT_HOME = C: /ANT1.6SET JAVA_HOME = C: /J2SDK1.4.2.2Set Path =% Path%;% PATH% / bin Set good Ant After the Ant -Version is executed in Command, see if it can be output Ant Version information confirmation installation success

Since many development tools have built-in Ant, I will use an Eclipse as an example to show how to develop Java projects in Ant. The project is very simple. New project is Testant, then write two classes in SRC as follows: Package COM. North;

/ ** * @Author p2800 * * Todo to change the template for this generated type comment go to * window - preferences - java - code style - code templates * / public class testant {

Public static void main (string [] args) {new mywork (). print ();}}

Package com.north; / * * created on 2004-7-23 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @author P2800 * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class MyWork {public void print () {System.out.println (System.Getenv ("classpath"));}} I don't have to introduce too much Ant's use, and you can get started very quickly by viewing the user manual. Instead, it directly gives the content of Build.xml and makes some necessary instructions on the relevant content. You will build a file build.xml in Project Testant, this is the file that antires to find, if you use other file names, you should use ant -buildfile mybuildfile.xml this is my test for ant Tool This build.xml does not complicate his purpose is to compile the source code inside Project and package it to the dist of the DIST. There are several elements in build.xml: Project Target Task Property. The Property is letting you define some attribute values, and use it in the future Target or Task. Each Project must specify a Default's target to execute. Each Target is a collection of Task, used to complete a specific task, typically complicated than a single TASK.

One parameter depends of the Target illustrates the order of compilation, for example, here you want JAR execution, you must execute first, while Compile is executed before INIT. So the order is init-> compile-> jar. Task is the most important, there are many built-in Task in Ant, and you will see that Ant is really very powerful. Specifically, which task you want to use will refer to his documentation. Select Build.xml Right click Select Run-> Ant Build, you will see BuildFile: c: /eclipse/Workspace/testant/build.xmlinit: [MKDIR] Created Dir: C: C: / Eclipse / Workspace / Testant / Dist [ Mkdir] Created Dir: C: / Eclipse / Workspace / Testant / ClassesCompile: [Javac] Compiling 2 Source Files To C: / Eclipse / Workspace / Testant / Classes [Javac] NOTE: C: C: C: C: / Eclipse / Workspace / Testant / SRC / COM / North / MyWork.java Uses Or Overrides a Deprecated API. [Javac] Note: Recompile with -DepRecation for Details.jar: [JAR] Building Jar: c: /eclipse/Workspace/testant/dist/myjar-20040723.jarbuild SuccessFultotal Time: 2 Seconds This means that the compilation is successful, but it is prompted to have a DepRecated API in the program. This is left to the reader to see how the manual shows which API?

转载请注明原文地址:https://www.9cbs.com/read-13621.html

New Post(0)