ArticleDirectory
- 1. Some Concepts
- 2. Common Tasks
- 3. Extend the task
Ant is an automated Java-based build tool that relies on JDK. Therefore, you must install JDK before using ant and direct the java_home and PATH environment variables to JDK. For example, in windows, the environment variables look like this:
Java_home = c: \ Program Files \ Java \ jdk1.6.0 _ 31
Ptah = % java_home % \ bin;
Ant is similar to GNU make in terms of usage and concept, but it uses XML to mark make. In addition, ant allows us to write extensions in Java, so that we can complete some complicated builds, or even deployment.
1. Some Concepts
Similar to make, which searches for makefile in the current directory by default to build a project, ant searches for build. XML in the current directory to build a project. The typical build. XML looks like this:
<Project name = "demoproject" default = "Dev"> <! -- Build directory --> <property name = "build. dir" value = "./build"/> <! -- Default target, it depends the target of Clean & makedirs --> <target name = "Dev" depends = "clean, makedirs"> <echo message = "build folder has been created! "/> </Target> <! -- Target: clean --> <target name = "clean"> <Delete dir = "$ {build. dir}"/> </Target> <! -- Target: makedirs --> <target name = "makedirs"> <mkdir dir = "$ {build. dir}"/> </Target> </Project>
The build. xml build file consists of a build project. Each build project can have several targets as needed, and each target has several tasks. Similar to this structure:
Project => Target (S) => task (s)
Ant also supports attributes, which are similar to the variables in make. The build. dir attribute defined in the previous example is then referenced in multiple places.
For a construction project, there must be a project to indicate the current construction project. This project generally has two common attributes: Project name: demoproject and default target, the default target is the target that ant executes if you directly use the ant command in the directory without adding any parameters. Similar to the role of the first target in make.
Target generally has two common attributes. One is that the name is used to indicate the current target. You can specify the target name on the command line to call this target, for example, we want to call the clean target
$ Ant clean
Another important attribute of target is depends. Ant implements dependency management through the depends attribute. For example, for the dev target, it depends on the clean and makedirs targets. Before the dev target is executed, ant will execute the target in the order specified by the depends attribute, run clean in the example, makedirs, and Dev.
The real work in ant is these tasks. The complicated part configured in ant is various tasks. For details, see task overview.
2. Common Tasks
- Copy is used to copy files and directories.
- Mkdir is used to create a directory.
- Delete is used to delete files and folders.
- Move and rename files and directories
- Replace variable replacement, such as <replace file = "Main. JS "token =" @ revision @ "value =" $ {revision} "/> this task will. replace the @ revsion @ tag in JS with the value of the $ {revision} variable.
3. Extend the task
If You Think ant's built-in tasks are not enough, you can write Java jar to extend the task function. For details, see write your own task.