1. View the version of ant plug-in in eclipse
Enter the plugins folder in the eclipse installation directory to see the corresponding version of the ant plug-in (not only ant, but also other plug-in versions):
2. Demo-TestTomcat (using previous testTomcat projects)
Create a new build. XML in the root directory (name can be changed, but usually this), followed by the following directory:
Right-click Run as Ant Build for build.xml and run as follows:
3. Example of build. XML
Below is a complete build.xml, and then we will explain each sentence in detail:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="TestTomcat" default="run" basedir=".">
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<property name="testAnt_jar" value="testAnt.jar"/>
<target name="init">
<mkdir dir="${dest}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>
<target name="build" depends="compile">
<jar jarfile="${testAnt_jar}" basedir="${dest}"/>
</target>
<target name="run" depends="build">
<java classname="com.zd.ant.TestAnt" classpath="${testAnt_jar}"/>
</target>
<target name="clean">
<delete dir="${dest}" />
<delete file="${testAnt_jar}" />
</target>
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>
</project>
Interpretation:
<?xml version="1.0" encoding="UTF-8" ?>
The first sentence in build.xml has no practical significance.
<project name="TestTomcat" default="run" basedir=".">
</project>
ant's content must be included in this. Name is the name you gave it. basedir's name means the root directory of the work. It represents the current directory. Default stands for what is done by default.
<property name="src" value="src"/>
Variables in similar programs, name src, value src, representing the SRC directory:
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>
Write everything you want to do as a target. It has a name, depends is the target it depends on. Before executing the target, such as the compile here, ant will check whether the init has been executed. If it has been executed, it will execute the compile directly. If not, it will first execute the target it depends on, such as the init here, and then execute the target:
Compile:
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>
Making jar bags:
<target name="build" depends="compile">
<jar jarfile="${testAnt_jar}" basedir="${dest}"/>
</target>
Function:
<target name="run" depends="build">
<java classname="com.zd.ant.TestAnt" classpath="${testAnt_jar}"/>
</target>
In order to avoid copying, we can define the target folder at the beginning so that ant can put the results directly in the target folder.
New folder:
<target name="init">
<mkdir dir="${dest}"/>
</target>
Two more target s have been added for a little more functionality.
Delete the generated file:
<target name="clean">
<delete dir="${dest}" />
<delete file="${ testAnt_jar }" />
</target>
Run again, showing how to call other targets in a target.
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>
4. More information - Labels
1.project
Each build file has a project tag with the following attributes:
- default: Represents the default running target, which is required.
- basedir: Represents the baseline catalog for the project.
- name: Represents the project name.
- Description: Represents the description of the project.
2.target
Tasks, one or more target tags under a project tag, represent tasks, and there can be dependencies between tasks. There are the following attributes:
- name: for identification, this is necessary
- depend: Used to specify the tasks to be relied on.
<!-- Initialization task -->
<target name="init">
<echo message=" init ${init} ..."/>
</target>
<!-- Compile -->
<target name="compile" depends="init">
<delete dir="${classes.dir}" />
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}">
<classpath refid="master-classpath" />
</javac>
</target>
3.delete
Delete files or file directories with the following attributes
- file: Delete files
- dir: Delete directories
- includeEmptyDirs: Is it worth deleting empty directories? The default is true
- failonerror: Whether the error is stopped by default is true
- verbose: Whether to list deleted files, default is false
<!--clean other dir-->
<target name="clean_other_dir">
<echo message="begin clean_other_dir..."/>
<delete dir="${basedir}/${compress.dir}"/>
<delete dir="${basedir}/pub"/>
<echo message="begin clean html module-xx..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/src/html" >
<include name="**/module-*/**"/>
</fileset>
</delete>
<echo message="begin clean res/module-xx,component-xx,res-base..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/res" >
<include name="module-*/**"/>
<include name="component-*/**"/>
<include name="res-base/**"/>
</fileset>
</delete>
</target>
4.copy
Copy files or directories with the following attributes:
- file: Represents the source file.
- tofile: Represents the target file.
- todir: Represents the target directory.
- Overwrite: Whether to overwrite the target file, default is false.
- includeEmptyDirs: Copy empty directories by default to true.
- failonerror: The default value is true if the target does not find whether it stops automatically.
- verbose: Whether to display details, default value false.
<target name="cp">
<copy todir="${compress.dir}" overwrite="true">
<fileset dir="${ob_baseline.dir}">
<include name="pub/" />
<include name="res/" />
<include name="mail_template/" />
</fileset>
</copy>
</target>
5.import
Introduce other xml files to improve reusability:
<import file="./env-judge.xml"/>
<import file="./tasks.xml"/>
6. Similar to constants, it can be used by other tags in build.xml. There are two characteristics:
- Case-sensitive
- It can't be changed. Whoever sets it first can't change it later.
The tag can be used in conjunction with multiple attributes.
- name and value:
<property name="module_name" value="admin"/>
The latter can be used directly:
<echo message="begin nej-build ${module_name}..."/>
- name and refid:
<property name="srcpath" refid="dao.compile.classpath"/>
The dao.compile.classpath is defined elsewhere. Of course, it can also be quoted directly:
<property name="baseline.dir" value="${ob_baseline.dir}"/>
- name and location:
<property name="srcdir" location="src"/>
Set the value of srcdir to the current file path / src.
- file:
<property file="./omad/build.properties"/>
Import all the variables in the relative file, where build.properties are used to store various variables.
•environment:
<property environment="env"/>
Set the prefix of the system's environment variable to env. such as
<property name="tomcat.home" value="${env.CATALINA_HOME}"/>
Set the Tomcat installation directory of the system to the tomcat.home property.
(More information can be consulted:
https://wenku.baidu.com/view/695488d676eeaeaad1f330ab.html?from=search)