Background: Recently, my personal responsibility for maintaining the testing service environment is as follows:
Remotely log on to host 438 -- enter the build directory -- Delete the two folders cloud and dist -- enter the ant directory -- type build to start building (because the system is very large, it usually takes 12 minutes) -- after the build is complete, open the SSH file transfer to upload the newly built folders to the RHEL server -- start cloud and DC in RHEL.
At the beginning, it was okay to get tired of having more days. So how can we automate the above process to the maximum extent?
One of the following methods is possible:
A. Set the build to a random start (because the server is generally not closed, so it is considered to be timed) (Write A. CMD script)
B. Put the deletion and upload work in the build script (modify the ant script)
So I started to learn how to use ant ......
--------Simple demo of ant Learning------
I. Basic Concepts
1. What is ant
2. What is building?
3. Ant advantages (compared with make)
. Cross-platform
. Easy to use
. Clear syntax
. Powerful functions
Ii. Ant Installation
1. Download ant.apache.org/bindownload.cgi. the latest version is 1.8.1.
2. Create a new variable named ant_home = ant, similar to the JDK environment.
Set path to % ant_home %/bin
Note: To enable ant to support JUnit, You must place JUnit. jar in the ant lib directory and remember to modify the JUnit settings in classpath.
3. Enter the CMD interface and enter ant-V
If you have a version prompt, your installation is successful.
Now, let's start writing ant ......
Iii. Ant simple demo
Purpose: To use ant to accomplish one task.
. Compile a program
. Compile them
. Compress them into jar packages.
. Set their storage locations
. Run them
1. First write a helloworld. Java
package test.ant;public class HelloWorld {public static void main(String[] args) {System.out.println("Welcome to The Ant World");}}
2. Write the build script build. xml
<?xml version="1.0" encoding="UTF-8"?><project name="HelloWorld" default="run" basedir="."> <property name="src" value="src"/> <property name="dest" value="classes"/> <property name="hello_jar" value="hello1.jar"/> <target name="init"> <mkdir dir="${dest}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${dest}" includeantruntime="false"/> </target> <target name="build" depends="compile"> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <java classname="test.ant.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <delete dir="${dest}"/> <delete file="${hello_jar}"/> </target> <target name="return" depends="clean,run"> <ant target="clean"/> <ant target="run"/> </target></project>
3. interpreting scripts
Write a target for everything you want to do. Depends is the target on which it depends. Before executing the current target, you must first execute the Target defined in depends.
Note: In the above script, return this target is a special target, which is equivalent to restoring
4. Execute
Type ant and you will find that all the tasks you have defined have been completed. Sometimes we may not want to execute the program, but just want to execute some of the steps, you can take the following practices:
Ant targetname. For example, if you only run clean, type ant clean.
^_^ Demo is now complete. Take a break and continue learning later ......
Problems encountered on the way:
1. Ant warning: 'includeantruntime' was not set, defaulting to build. sysclasspath = last; set to false for Repeatable builds
Solution: add the attribute includeantruntime = "false" to the javac tag.
---------Ant learning-ant Integration------------
Background: our current project is divided into 8 modules, 13 small projects in total, and different teams are responsible for different modules or projects. For testing, each group has its own build. XML, and I am the test maintenance personnel responsible for combining these modules. When I write a build. xml file, I am disappointed:
1. Write a new build. xml? -- This seems troublesome.
2. Try to use build. xml already written by colleagues to reduce your work.
Because I decided to adopt the second method, I am now learning ant integration.
-----------------------------------------------------------
Suppose there are only three modules, and each group is responsible for one module. Each of them has a SRC module and a compiled build. xml module. Now you get their SRC. What you need to do is to create three folders: src1, src2, and src3, and merge their SRC and build. xml into a new build. xml.
<?xml version="1.0" encoding="UTF-8"?><project name="main" default="build" basedir="."> <property name="bin" value="${basedir}/bin"/> <property name="src1" value="${basedir}/src1"/> <property name="src2" value="${basedir}/src2"/> <property name="src3" value="${basedir}/src3"/> <target name="init"> <mkdir dir="${bin}"/> </target> <target name="run"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="clean"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="build" depends="init"> <copy todir="${bin}"> <fileset dir="${src1}"> <include name="*.jar"/> </fileset> <fileset dir="${src2}"> <include name="*.jar"/> </fileset> <fileset dir="${src3}"> <include name="*.jar"/> </fileset> </copy> </target> <target name="rebuild" depends="build,clean"> <ant target="clean"/> <ant target="build"/> </target></project>
OK, your task is finished!
----------Ant advanced learning----------
As you have seen, there are still a lot of problems with the above practices. The focus is that there are a lot of repetitive things. Every time you modify a directory, You have to modify multiple places. Can you make your work better?
Two things are introduced:
1. Property
2. xml include
Both of these two features can be used together to separate build. xml </property>.
Differences:
Property is easy to maintain and requires only simple key-value pairs.
The XML include feature not only extracts attributes, but also extracts attributes from target.
--------------------------------------
To continue with the preceding example, if we want to extract the three attributes src1 src2 src3 from XML, we can create a new all. properties file with the following content:
Src1 = file location...
Src2 = file location...
Src3 = File Location
Then your build. XML can be written in this way. Others only need to modify your configuration file all. properties without modifying your build. xml:
<?xml version="1.0" encoding="UTF-8"?><project name="main" default="build" basedir="."> <property name="bin" value="${basedir}/bin"/> <property file="all.properties"/> <target name="init"> <mkdir dir="${bin}"/> </target> <target name="run"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="clean"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="build" depends="init"> <copy todir="${bin}"> <fileset dir="${src1}"> <include name="*.jar"/> </fileset> <fileset dir="${src2}"> <include name="*.jar"/> </fileset> <fileset dir="${src3}"> <include name="*.jar"/> </fileset> </copy> </target> <target name="rebuild" depends="build,clean"> <ant target="clean"/> <ant target="build"/> </target></project>
Sometimes you want to add several targets to build. xml of each group. One way is to write the target to each build. XML, but there is a better way:
You can write an Include. xml file with the following content:
<?xml version="1.0" encoding="UTF-8"?> <property name="src" value="src"/> <property name="dest" value="classes"/> <target name="test"> <ant target="run"/> </target></xml>
Then you can change each of your build. XML as follows and write it in each build. xml:
<!-- include a xml file, it can be common property, can be also a target --> <!DOCTYPE project[ <!ENTITY share-variable SYSTEM "file:../include.xml"> ]>
Write & Share-variable; between <project> </Project> In build. xml.
As follows:
<?xml version="1.0" encoding="UTF-8"?> <!-- include a xml file, it can be common property, can be also a target --> <!DOCTYPE project[ <!ENTITY share-variable SYSTEM "file:../include.xml"> ]><project name="HelloWorld" default="run" basedir="."><!--use the include -->&share-variable;<!--define the property --><!-- via include <property name="src" value="src"/> <property name="dest" value="classes"/>--> <property name="hello_jar" value="hello1.jar"/><!--define op --> <target name="init"> <mkdir dir="${dest}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${dest}" includeantruntime="false"/> </target> <target name="build" depends="compile"> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <java classname="test.ant.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <delete dir="${dest}"/> <delete file="${hello_jar}"/> </target> <target name="return" depends="clean,run"> <ant target="clean"/> <ant target="run"/> </target></project>