Ant installation and configuration

xiaoxiao2021-04-05  246

Ant's installation is very simple, unwrapping Jakarta-ant-1.5.1-bin.zip downloaded online into a directory (hereinafter assumed) is installed in the directory D: /JAKARTA-ANT-1.5.1). Next, environmental variable configuration is required:

SET ANT_HOME = D: /JAKARTA-ANT-1.5.1 // Note is an ANT installation directory, not bin subdirectory set path =% Path%;% ANT_HOME% / BIN;

Before configuring the environment variable, make sure that the Java_Home system variable has been properly set. Enter the ANT command to see the following output description has been successfully installed:

BuildFile: build.xml does not exist! Build failed

The prompt information indicates that there is no build.xml configuration file in the current directory, but this has been successfully running Ant successfully.

Get started

The following is the simplest and most classic example -helloworld to feel Ant.

//HelloWorld.javaPackage com.Sharetop.antdemo; public class helloworld {public static void main (string args []) {system.out.println ("Hello World.");}}

Let Ant build this file, first you need to write a build profile. In general, this file is named Build.xml.

< Property name = "dest" value = "classes" /> < / target>

Let's take a look at the content of this file, which describes the following information: The name of the project is HelloWorld, and the project has four Targets, which are init, compil, build, and run, default is Run. Compile only one task JavaC, the source file is located in the src directory, and the output class file is placed in the CLASSES directory. Build's task is JAR, the generated JAR file is hello.jar, which is packaged as the root directory when packaged. Run is to execute this HelloWorld class, use hello.jar as a ClassPath. There is a dependency between these four Targets, which is specified in Depends. That is, if Target A depends on Target B, the Target B will be executed first before performing Target A. Therefore, from the output of the default target (RUN) below, the order of execution of these four Targets is: init → compile → build → run. The file directory structure is shown in Figure 1. HelloWorld.java files under src / com / sharetop / antdemo subdirectory. Figure 1 Directory structure of Ant_Demo application

Enter the command in the command line: Ant, then run, you can see the following output:

If the configuration file name is not build.xml, such as build_front.xml, then you can use the -buildfile command parameter:

G: / mydoc / ant_demo> ant -buildfile building_front.xml

You can also perform a specified TARGET separately, for example, you can use only the input command below:

G: / mydoc / ant_demo> ANT Compile

The compiled helloWorld.class file is found in the corresponding directory.

Take a look at the build.xml configuration file above, the file starts three attributes, specifies the source file output path, class file output path, and the generated JAR file name, and the reference to these paths is passed by one $ { Property name} to reference. Therefore, pay attention to such a principle "directory definition and directory reference should be separated".

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

New Post(0)