Java build tool Ant build.xml file

0 Introduction

All the dry technology hall

文章收录在我的 GitHub 仓库,欢迎Star/fork:
Java-Interview-Tutorial
https://github.com/Wasabi1234/Java-Interview-Tutorial

Apache Ant is a Java language development tools provided by the Apache Software Foundation. Apache Ant configuration file written in XML is easy to maintain and write, but the structure is very clear.

1 Introduction

Ant build tools are cross-platform Java language developed under an Apache-based foundation. Before we learn more about Apache Ant, let us explain why the build tool is needed first to know.

Build demand for tools

Typically, developers spend a lot of time to do general tasks, such as: building and deploying usually contains the following work:

Compiled code
package binaries
deployed on the test server binaries
test change
from one location to another location copy the code
for automatic and simplify the work of the above, Apache Ant is very useful. It is a tool based on open operating systems to build and deploy, the tool needs to be performed from the command line.

History of Apache Ant

Ant by James Duncan Davidson developed (ie Tomcat original developer).
Was originally used to build Tomcat, it is used as part of a release of Tomcat.
The complexity of Apach Make toolkit with many problems spawned Apache Ant.
In 2000, when, Ant is used as a stand-alone project. The latest version of Apache Ant version 1.9.4 in May 2014.

Apache Ant features

Ant is the most complete build and deploy Java-based language development tools.
Ant is independent of platform, platform-specific properties can be processed, such as a file separator.
Ant can also be used to perform platform-specific tasks, such as using the touch command to modify a file modification time.
Ant XML script is done. If you have an understanding of XML, then you learn Ant, it will be more handy.
Ant specializes automate repetitive tasks.
Ant need to begin a series of predefined tasks.
Ant provides an interface to develop custom tasks.
Ant can be easily invoked from the command line, and it can be well integrated free commercial and integrated development environment.

2 environment to build

Apache Ant is released under the Apache Software License. Apache is an open source organization accredited full-fledged open source license. The latest version of Apache Ant includes the following sections: the complete source code, class files and documents, the content can be found in http://ant.apache.org above.

Install Apache Ant
premise installation of Apache Ant is already downloaded and installed the Java Development Kit (JDK) on your computer. If not already installed on your computer, download and install it, follow these tips.

Make sure you set the JAVA_HOME environment variable to install the Java Development Kit folder.
Download Library from http://ant.apache.org.
Unzip the file to a convenient address, such as c: \ folder. You may be used Winzip, winRAR, 7-zip or other similar tools to perform a decompression operation.
Create a new environment variable named ANT_HOME, the environment variable to point Ant installation folder, in this example, the folder c: \ apache-ant-1.8.2 -bin.
Add the path to the batch file Apache Ant to the PATH environment variable. In this example, the path should be c: \ apache-ant-1.8.2 -bin \ bin folder.
Verify Apache Ant installation
to verify that your computer has been successfully installed Apache Ant, you can enter ant at the command prompt. You should see an output similar to the following:

C:> Ant -version
the Apache Ant ™ Version 1.8.2 Compiled ON December 20 2010
if you do not see output similar to the above, please re-check that you have the right as you have previously been installed.

Installing Eclipse
This tutorial also includes Ant and Eclipse integrated development environment to inherit. So, if you do not have a good Eclipse installed, download and install Eclipse.

Follow these steps to install Eclipse:

Download the latest version of the Eclipse files from www.eclipse.org above.
Eclipse unzip the file to a convenient location, such as c: \ folder.
From the c: \ eclipse \ Run Eclipse eclipse.exe place.

3 build file

In general, Ant build files by default build.xml, on the top level project directory. However, there is no limit build file must be named bulid.xml, it does not limit the project on the top-level directory. You can build a file named other names, it can also be placed in other parts of the project.

This tutorial will show you a simple way how to automatically build and deployment process using the Apache Ant project. After completing this tutorial, you will find that you already have the next phase of learning Apache Ant moderate level of expertise.

For the following exercises, create a file named build.xml files stored anywhere on your computer and contains about content:

Hello World <xml version = "1.0"??> - Welcome to Apache Ant noted that the above exercise, before the declaration of the xml file without any blank lines or spaces!. If you join the blank lines or spaces when writing the statement xml file, when executed ant -build operation, the following error message appears:

. The processing instruction target matching "[ xX] [mM] [lL]" is not allowed
the meaning of the error message is: processing instruction target matching "[xX] [mM] [ lL]" is not allowed. All the build file needs to contain the project elements (project label) and at least one target element (target label).

Project elements build file has three attributes:

Attribute describes the
project name (name) represents the name of the project. (Optional)
default (default) represents the default target build scripts to run, namely to develop the default target. A project (project) may contain multiple targets (target). (Must)
reference directory (the basedir) indicates that when the attribute is not specified, the directory attachment member Ant file directory as a reference. (Optional)
a target (target) you want to run a series of tasks (tasks), as a unit is running. In our example, we use a simple goal to provide an information message for the user.

There may be dependencies between goals and objectives. For example, a deployment (Deploy) target may depend on the package (package) target, and this package may well depend on the target compiler (the compile) goals. Dependencies are represented as dependency property (depends). E.g:

.... .... .... .... build file of the target element has the following attributes:

Attribute describes
the target name (name) represents the name of the target. (Required)
dependent (The depends) is used to describe the dependency between the target, if present and a plurality of dependency target, need to be "," interval. Ant target sequence will occur in accordance depends attribute sequentially performed for each target. The first target will be relied upon to perform. (Optional)
a brief description about the target function description (description). (Optional)
If (IF) for verifying whether the specified property exists, if not, where the target will not be executed. (Optional)
unless the function and if the function attribute (The unless) the opposite of the property, which is also used to verify whether the specified property exists, if not, where the target will be executed. (Optional)
In the example above, the echo task is mainly responsible for printing messages. In our example, after the implementation of echo task, print out "hello world" message.

In order to run ant build file, open a command prompt and navigate to the file build.xml created folder. Enter ant ant info command or commands. Both commands can be run because info is to build the default target file. You speak will see the following output:

C:>ant
Buildfile: C:\build.xml

info: [echo] Hello World - Welcome to Apache Ant!

BUILD SUCCESSFUL
Total time: 0 seconds

C:>

4 Build projects

Now that we have learned Ant type of data, it is time to apply the knowledge in the actual process. In this chapter, we will build a project. The purpose of this chapter is to create an Ant build file that compiles Java source files and the files are stored in these classes WEB-INF \ classes folder under.

Consider the structure of the next building project:

Scripts are stored in the data folder db file.
java source files are stored in the src folder.
images (images), js (JavaScript script), folder style (css Cascading Style Sheets) file is stored in the war.
JSPs files are stored in the jsp file.
Third-party jar files are stored in the lib file.
java class files are stored in the WEB-INF \ classes file.
After completing the remainder of this tutorial, we will be able to know that this project is a fax Hello World application.

C:\work\FaxWebApplication>tree
Folder PATH listing
Volume serial number is 00740061 EC1C:ADB1
C:.
+---db
+---src
.  +---faxapp
.  +---dao
.  +---entity
.  +---util
.  +---web
+---war
   +---images
   +---js
   +---META-INF
   +---styles
   +---WEB-INF
      +---classes
      +---jsp
      +---lib

Build.xml file contents of these projects are given below. Let's take it one by one to analyze the statement to statement.

<?xml version="1.0"?> First of all, let's declare some of the source files, some of the property information web files and build files. In the example above:

src.dir indicate the source directory of the project, which is the place to store java files.
web.dir represent web directory of the project, which is the place to store JSPs file, web.xml, css, javascript and other web-related files.
build.dir represents the output file for the project.
Properties can also refer to other properties. In the above example, build.dir web.dir attribute references property.

In the above example, src.dir is where the project source files are stored.

The default goal of our project is to compile goal. But first let us look at the clean target.

clean goal, as its name suggests means the same, delete all the files in the folder build in.

Control classpath (master-classpath) to save the information class path. In this case, it contains all the class files and folders build jar folders. Finally, build target to build these files. First, we create a build directory, if the directory does not exist, we would execute javac command (specific to jdk 1.5 compiler environment as our goal). We provide a source folder and type the path to the javac task, and by executing javac task class files stored in the build folder. Execution on this file Ant, compiled java source files, class files compiled and stored in a local build folder.

After running the Ant file, you can see the following output:

C:>ant
Buildfile: C:\build.xml

SUCCESSFUL BUILD
Total Time: 6.3 seconds The
post file is compiled, stored in the folder build.dir file.

Published 351 original articles · won praise 339 · Views 250,000 +

Guess you like

Origin blog.csdn.net/qq_33589510/article/details/104609052