Introduction to Apache Ant automation scripting tutorial and common commands

  • 2020-05-10 23:19:13
  • OfStack

1 Ant profile

Apache Ant is a tool that automates software compilation, testing, deployment, and other steps together, mostly for automated builds in Java environments. It is a subproject in the Apache software foundation JAKARTA directory and has the following advantages:

Cross-platform. Ant is written in the pure Java language, so it is very cross-platform

Easy to operate. Ant is made up of one built-in task and one optional task. The Ant runtime requires one XML file (the build file). Ant performs various tasks by calling the target tree. Because the Ant buildfile is an XML file, it is easy to maintain and write, and is clearly structured. It can be easily integrated into various development environments
(note: refer to baidu encyclopedia for the above introduction)

2. Ant environment setup and introduction example

If you want to use ant, you first need to go to the website to download ant installation package, address: http: / / ant apache. org/bindownload cgi

Environment variable configuration for ant. The configuration process is similar to the environment variable configuration of Java, that is, the bin path of ant can be added to the "system variable" of Path of the system "environment variable", Path, as shown in the following figure: 0001

When the above environment variables are configured and ready for testing in cmd, the following hints indicate that they have been configured successfully: 1

Write an introductory instance and output "Hello World". Create a new file named "build_test1.xml" in a directory. The content is as follows:


<?xml version="1.0"?>
<project name="helloWorld">
<target name="sayHello">
<echo message="Hello,Ant!"/>
</target>
</project>

Then switch the working directory of cmd to the directory of the above file and execute the following command: ant-buildfile build_test1.xml sayHello. The output is as follows: 2
The code above is very simple, simply output 1 "Hello,Ant!" . What does each tag in the code above mean, I'll explain briefly below

Some common tags and examples of 3 Ant

(1) < project > Tags:

< project > The tag is the root tag of the build file, and each build file corresponds to one project. Its common attributes are as follows:

name: represents the name of the project project
default: represents the name of the task to be executed by default when the Ant project is started. If this parameter is not present, you need to manually specify the task to be executed when the project is running
basedir: represents the default directory for project execution

The sample code is as follows:


<?xml version="1.0"?>
<project name="hello" default="sayBaseDir" basedir="C:\\Users\\Administrator\\Desktop">
<target name="sayBaseDir">
<echo message="The base dir is: ${basedir}"/>
</target>
</project>

Execute the following commands in the console:

ant -buildfile build_test2.xml

The output is as follows:

Buildfile: C:\Users\Administrator\Desktop\ant\build_test2.xml
sayBaseDir:
[echo] The base dir is: C:\Users\Administrator\Desktop
BUILD SUCCESSFUL
Total time: 0 seconds

(2) < target > Tags:

target represents a task to be performed by one, and there can be more than one target tag under one project tag. At the same time, you can specify that one target depends on another target, so that the dependent task is executed first before the target task is executed. This property is very useful, for example, to compile and execute an Java file, the order of execution should be compiled first and then executed, at which point we can make the task of executing the class file depend on the task of compiling the Java file. Several common attributes of target tags are as follows:

name: represents the task name
depends: represents the task name on which the task depends
if: means that the task is performed only if the attribute exists
unless: the opposite of if, means to perform the task when the property is not set

The sample code is as follows:


<?xml version="1.0"?>
<project name="targetStudy" default="targetB">
<property name="zifangsky" value="www.zifangsky.cn" />
<target name="targetA" if="zifangsky">
<echo message="Java Version: ${ant.java.version}" />
</target>
<target name="targetB" depends="targetA" unless="xxx">
<echo message="The base dir is: ${basedir}" />
</target>
</project>

Output:

Buildfile: C:\Users\Administrator\Desktop\ant\build_test3.xml
targetA:
[echo] Java Version: 1.8
targetB:
[echo] The base dir is: C:\Users\Administrator\Desktop\ant
BUILD SUCCESSFUL
Total time: 0 seconds

(3) < mkdir > Tags:

As the name implies, you can create 1 folder

The sample code is as follows:


<mkdir dir="build/classes"/>

(4) < delete > Tags:

Files or folders can be deleted, common properties are as follows:

file: represents the file to be deleted

dir: represents the directory to be deleted

includeEmptyDirs: indicates whether to delete the empty directory in the specified directory, e.g. includeEmptyDirs= "true"

failonerror: indicates whether to stop when an error is encountered. The default is to stop automatically

The sample code is as follows:


<delete dir="build/classes" />

(5) < copy > Tags:

For copying files or directories, the common properties are as follows:

file: represents the source file
tofile: represents the target file
todir: represents the target directory
overwrite: indicates whether the target file is overwritten or not. The default is not overwritten

Copy a single file:


<copy file="old.txt" tofile="new.txt" />

Copy 1 file to another directory:


<copy file="old.txt" todir="test1/addtest" overwrite="true" />

Copy the directory:


<copy todir="test1/addtest">
<fileset dir="addtest"/>
</copy>

(6) < move > Tags:

Used to move files or directories, common properties and < copy > The labels are about the same

The sample code is as follows:


<move file="sourcefile" tofile="destfile" />
<move file="sourcefile" todir="destdir" />
<move todir="newdir" >
<fileset dir="olddir" />
</move>

(7) < filelist > Tags:

Represents a list of files. The common properties are as follows:

dir: represents the file directory
files: a comma-separated list of 1 file
refid: represents one definition of a place < filelist > A reference to the

The sample code is as follows:


<filelist id="resourceFiles" dir="${res.src}" files="web.xml,application.xml" />
<filelist refid="resourceFiles" />
<filelist id="resourceFiles" dir="${res.src}">
<file name="web.xml" />
<file name="application.xml" />
</filelist>

(8) < fileset > Tags:

Represents a list of 1 types of files. The commonly used properties are as follows:

include: represents a list of file modes

exclude: represents a list of files that do not contain these schemas

The sample code is as follows:


<copy todir="${buildwar.dest}/WEB-INF/classes" overwrite="true">
<fileset dir="${build.bin}">
<include name="**/*.class" />
</fileset>
</copy>
<copy todir="${buildwar.dest}" overwrite="true">
<fileset dir="${webapp.dir}">
<exclude name="/WEB-INF/classes/**" />
</fileset>
</copy>
<path id="buildpath">
<fileset refid="lib.runtime"/>
<fileset refid="lib.lib"/>
</path>

(9) < property > Tags:

Used to define 1 parameter

The sample code is as follows:


<property name="zifangsky" value="www.zifangsky.cn" />

(10) < path > Tags:

Used to define 1 path

The sample code is as follows:


<path id="classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${tomcat.home}/lib">
<include name="**/*.jar" />
</fileset>
</path>

(11) < javac > Tags:

For compiling Java files, the following properties are commonly used:

srcdir: represents the source code path

destdir: represents the path to the generated class file

The sample code is as follows:


<target name="compile" depends="clean">
<mkdir dir="build/classes" />
<javac srcdir="src" destdir="build/classes" />
</target>

(12) < java > Tags:

Used to execute the.class file. The common properties are as follows:

classname: represents the name of the class to be executed
jar: represents the JAR file name that contains the class
classpath: represents the classpath used

The sample code is as follows:


<target name="run" depends="compile">
<java classname="javase.base.Demo2">
<classpath>
<pathelement path="build/classes/" />
</classpath>
</java>
</target>

(13) < jar > Tags:

Used to package class files into jar packages with the following common properties:

basedir: represents the directory to be archived
destfile: represents the generated JAR file name

The sample code is as follows:

XHTML


<?xml version="1.0"?>
<project name="javacTest" default="makeJar" basedir=".">
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="run" depends="compile">
<java classname="javase.base.Demo2">
<classpath>
<pathelement path="build/classes/"/>
</classpath>
</java>
</target>
<target name="makeJar" depends="run">
<jar destfile="Demo.jar" basedir="build/classes">
<manifest>
<attribute name="Main-class" value="javase.base.Demo2"/>
</manifest>
</jar>
</target>
</project>

Related articles: