组建自动化工具Ant
组建自动化工具Ant

Ant可以帮助我们自动化的完成项目的构建
下面是维基百科对Ant的介绍:http://zh.wikipedia.org/wiki/Apache_Ant
Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。由Apache软件基金会所提供。默认情况下,它的buildfile(XML文件)名为build.xml。每一个buildfile含有一个<project>和至少一个默认的<target>,这些targets包含许多task elements。每一个task element有一个用来被参考的id,此id必须是唯一的。
1.安装
apache-ant-1.9.4-bin.zip:http://ant.apache.org/bindownload.cgi

设置环境变量:Path+=D:\Idea\config\apache-ant-1.9.4\bin
打开命令行,执行ant,显示build.xml文件不存在,说明Ant已经安装好了
C:\Users\yuki>ant Buildfile: build.xml does not exist! Build failed C:\Users\yuki>
2.编译
ant运行后会找build.xml文件
在D:\Ant\app1下新建HelloWorld.java和build.xml
在project标签下添加javac标签
<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">
<!-- 编译 -->
<target name="compile">
<javac destdir="." srcdir="."></javac>
</target>
</project>
cmd进入这个目录,执行ant
D:\Ant\app1>ant Buildfile: D:\Ant\app1\build.xml compile: [javac] D:\Ant\app1\build.xml:5: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; se t to false for repeatable builds [javac] Compiling 1 source file to D:\Ant\app1 BUILD SUCCESSFUL Total time: 2 seconds D:\Ant\app1>
D:\Ant\app1\HelloWorld.class文件会出现,编译成功

3.执行
修改build.xml文件,添加java标签
D:\Ant\app1\build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">
<!-- 运行 -->
<target name="execute">
<java classpath="." classname="HelloWorld"></java>
</target>
</project>
D:\Ant\app1>ant Buildfile: D:\Ant\app1\build.xml execute: [java] hello world! BUILD SUCCESSFUL Total time: 0 seconds D:\Ant\app1>
4.编译且执行
每一次都修改文件似乎不太好
删除HelloWorld.class,修改build.xml文件,
在[name="execute"]的target上添加depends="compile"
<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">
<!-- 编译 -->
<target name="compile">
<javac destdir="." srcdir="."></javac>
</target>
<!-- 运行 -->
<target name="execute" depends="compile">
<java classpath="." classname="HelloWorld"></java>
</target>
</project>
执行ant,看到执行的结果
D:\Ant\app1>ant Buildfile: D:\Ant\app1\build.xml compile: [javac] D:\Ant\app1\build.xml:5: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; se t to false for repeatable builds [javac] Compiling 1 source file to D:\Ant\app1 execute: [java] hello world! BUILD SUCCESSFUL Total time: 1 second D:\Ant\app1>
target:表示一项具体的任务
target.depends:关联不同的任务
project.default:默认执行的任务
4.编译流程
创建文件夹
build:文件信息
src:源码信息
classes:编译好的文件
dist:编译好的jar文件
编译源代码,将源代码打包为jar,直接运行程序
eclispe>New Java Project>Finish,新建yuki.ant.app1.HelloWorld.java
打印main函数的传入参数
/app1-ant/src/yuki/ant/app1/HelloWorld.java
package yuki.ant.app1;
public class HelloWorld {
public static void main(String[] args) {
for(String arg : args)
System.out.println("hello" + arg);
}
}
5.eclipse配置
可以设置文档的路径为本地的
Preferences>Ant>DocumentationURL=D:\Idea\config\apache-ant-1.9.4\manual

Preferences>Ant>Runtime>Classpath>Ant Home:这里默认是eclipse提供的
右侧的Ant Home>选择D:\Idea\config\apache-ant-1.9.4>Apply

6.创建文件夹
项目右键>New XML File>File name=build.xml>Finish
build.xml右键>Open With>Other…>Ant Editor
写入创建文件夹的命令,mkdir
<target name="create">
<mkdir dir="build"/>
</target>
build.xml>Run As>Ant Build…>Target勾选create>Run

Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml create: [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build BUILD SUCCESSFUL Total time: 720 milliseconds
项目右键>Refresh>新建了文件夹build
删除文件夹可以使用delete
<target name="remove">
<delete dir="build"/>
</target>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml remove: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\build BUILD SUCCESSFUL Total time: 695 milliseconds
7.文件集
将src文件夹的数据拷贝到build/src
如何获取每一个文件夹中的文件
文件集:src目录下的所有java文件
<fileset dir="src" includes="**/*.java"></fileset>
<!-- 复制文件 -->
<target name="copySrc" depends="init">
<copy todir="build/src">
<fileset dir="src" includes="**/*.java"></fileset>
</copy>
</target>
build.xml>Run As>copySrc
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist copySrc: [copy] Copying 1 file to D:\Workspaces\Eclipse-Ant\app1-ant\build\src BUILD SUCCESSFUL Total time: 848 milliseconds
选择app1-ant按下F5,看到源文件被复制到build下
8.删除文件夹
删除build文件夹,
如果不删除在执行创建文文件夹时发现文件已存在,命令就不会执行
build.xml>init
<!-- 创建文件夹 -->
<target name="init">
<mkdir dir="build" />
<mkdir dir="build/src" />
<mkdir dir="build/dist" />
</target>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: BUILD SUCCESSFUL Total time: 616 milliseconds
在init中的mkdir前添加:<delete dir="build"></delete>
<!-- 创建文件夹 -->
<target name="init">
<delete dir="build"></delete>
<mkdir dir="build" />
<mkdir dir="build/src" />
<mkdir dir="build/dist" />
</target>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist BUILD SUCCESSFUL Total time: 491 milliseconds
9.复制文件夹下所有的文件
src下新建text.properties,执行copySrc,看到这个文件没有被复制
文件集改为includes="**/*.*"后,看到文件已被复制
<fileset dir="src" includes="**/*.*"></fileset>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist copySrc: [copy] Copying 2 files to D:\Workspaces\Eclipse-Ant\app1-ant\build\src BUILD SUCCESSFUL Total time: 623 milliseconds
10.包含与排除
可以把文件集定义在外面,在需要用的时候引用就可以了
<fileset id="src.path" dir="src" includes="**/*.*"></fileset>
<fileset refid="src.path"></fileset>
<!-- 文件集 -->
<fileset id="src.path" dir="src" includes="**/*.*"></fileset>
<!-- 复制文件 -->
<target name="copySrc" depends="init">
<copy todir="build/src">
<fileset refid="src.path"></fileset>
</copy>
</target>
假设文件集的路径比较多,可以把属性includes写成子标签
比如复制/src下排除掉Test开头的文件,有标签exclude
添加标签:<exclude name="**/Test*"/>
<fileset id="src.path" dir="src">
<include name="**/*.*"/>
<!-- <exclude name="**/Test*"/> -->
<exclude name="**/*.java"/>
</fileset>
新建文件TestHello.java,运行copySrc
会看到TestHello.java文件没有被复制
也可以排除*.java类型的文件:<exclude name="**/*.java"/>
<!-- 文件集 -->
<!-- <fileset id="src.path" dir="src" includes="**/*.*"></fileset> -->
<fileset id="src.path" dir="src">
<include name="**/*.*"/>
<!-- <exclude name="**/Test*"/> -->
<exclude name="**/*.java"/>
</fileset>
运行build.xml>copySrc,
看到不仅java文件没有被复制,所在的文件夹也没有被复制

11.编译源代码
依赖的是init,编译到build/classes目录下
<!-- 编译源代码 -->
<target name="compile" depends="init">
<javac destdir="build/classes" srcdir="src"></javac>
</target>
build.xml>compile
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build.xml:32: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\build\classes BUILD SUCCESSFUL Total time: 5 seconds
刷新项目,看待build/classes目录下的class文件
12.把源代码打成jar
需要依赖compile,打包到build/dest/hello.jar
<!-- 打成jar -->
<target name="jar" depends="compile">
<jar destfile="build/dist/hello.jar" basedir="build/classes"></jar>
</target>
build.xml>jar
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dest compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build.xml:32: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\build\classes jar: [jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dest\hello.jar BUILD SUCCESSFUL Total time: 1 second
打开D:\Workspaces\Eclipse-Ant\app1-ant\build\dest\hello.jar
看到在包里的class文件

13.运行打好的jar包
打开hello.jar\META-INF\MANIFEST.MF
在这个文件中,增加一个Main-Class属性就可以直接运行了
在打jar的时候增加一些属性:manifest.attribute
<!-- 打成jar -->
<target name="jar" depends="compile">
<jar destfile="build/dist/hello.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="yuki.ant.app1.HelloWorld"/>
<attribute name="Build-By" value="YuKi"/>
</manifest>
</jar>
</target>
运行jar,查看hello.jar\META-INF\MANIFEST.MF
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.9.4 Created-By: 1.8.0_20-b26 (Oracle Corporation) Main-Class: yuki.ant.app1.HelloWorld Build-By: YuKi
14.运行程序
可以依赖jar或compile,基于类路径的classname完成执行
设置运行的主函数所在的类,传入参数,设置项目的默认操作default="execute"
<!-- 运行程序 -->
<target name="execute" depends="jar">
<java classname="yuki.ant.app1.HelloWorld" classpath="build/classes">
<arg value="值1"/>
<arg value="值2"></arg>
</java>
</target>
Run As>build.xml>execute
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dest compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build.xml:32: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\build\classes jar: [jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dest\hello.jar execute: [java] hello值1 [java] hello值2 BUILD SUCCESSFUL Total time: 2 seconds
15.echo
在执行前会打印:[echo]
基于jar文件执行
<echo>基于jar文件执行</echo>
<java jar="build/dist/hello.jar">
<arg value="value1"/>
<arg value="value2"/>
<arg value="value3"/>
</java>
运行后报错,这是因为在使用ant的编译环境
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build.xml:32: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\build\classes jar: [jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist\hello.jar execute: [echo] 基于类路径的classname完成执行 [java] hello值1 [java] hello值2 [echo] 基于jar文件执行 BUILD FAILED D:\Workspaces\Eclipse-Ant\app1-ant\build.xml:56: Cannot execute a jar in non-forked mode. Please set fork='true'. Total time: 2 seconds
16.fork
设置java标签的属性fork="true"可以使用jdk的编译环境
<echo>基于jar文件执行</echo>
<java jar="build/dist/hello.jar" fork="true">
<arg value="value1"/>
<arg value="value2"/>
<arg value="value3"/>
</java>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build.xml:32: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\build\classes jar: [jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist\hello.jar execute: [echo] 基于类路径的classname完成执行 [java] hello值1 [java] hello值2 [echo] 基于jar文件执行 [java] hellovalue1 [java] hellovalue2 [java] hellovalue3 BUILD SUCCESSFUL Total time: 2 seconds
设置<target name="execute" depends="jar, copySrc">
就可以在运行之前运行copySrc的依赖任务,执行前复制源代码
/app1-ant/build-file.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<!--<target name="create">
<mkdir dir="build"/>
</target>
<target name="remove">
<delete dir="build"/>
</target>-->
<!-- 文件集 -->
<!-- <fileset id="src.path" dir="src" includes="**/*.*"></fileset> -->
<fileset id="src.path" dir="src">
<include name="**/*.*"/>
<!-- <exclude name="**/Test*"/> -->
<exclude name="**/*.java"/>
</fileset>
<!-- 创建文件夹 -->
<target name="init">
<delete dir="build"></delete>
<mkdir dir="build" />
<mkdir dir="build/src" />
<mkdir dir="build/dist" />
</target>
<!-- 复制文件 -->
<target name="copySrc" depends="init">
<copy todir="build/src">
<!-- <fileset dir="src" includes="**/*.java"></fileset> -->
<!-- <fileset dir="src" includes="**/*.*"></fileset> -->
<fileset refid="src.path"></fileset>
</copy>
</target>
</project>
/app1-ant/build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">
<!-- 文件集 -->
<!-- <fileset id="src.path" dir="src" includes="**/*.*"></fileset> -->
<fileset id="src.path" dir="src">
<include name="**/*.*"/>
<!-- <exclude name="**/Test*"/> -->
<!-- <exclude name="**/*.java"/> -->
</fileset>
<!-- 创建文件夹 -->
<target name="init">
<delete dir="build"></delete>
<mkdir dir="build" />
<mkdir dir="build/src" />
<mkdir dir="build/classes" />
<mkdir dir="build/dist" />
</target>
<!-- 复制文件 -->
<target name="copySrc" depends="init">
<copy todir="build/src">
<!-- <fileset dir="src" includes="**/*.java"></fileset> -->
<!-- <fileset dir="src" includes="**/*.*"></fileset> -->
<fileset refid="src.path"></fileset>
</copy>
</target>
<!-- 编译源代码 -->
<target name="compile" depends="init">
<javac destdir="build/classes" srcdir="src"></javac>
</target>
<!-- 打成jar -->
<target name="jar" depends="compile">
<!-- <jar destfile="build/dist/hello.jar" basedir="build/classes"></jar> -->
<jar destfile="build/dist/hello.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="yuki.ant.app1.HelloWorld"/>
<attribute name="Build-By" value="YuKi"/>
</manifest>
</jar>
</target>
<!-- 运行程序 -->
<!-- <target name="execute" depends="jar"> -->
<target name="execute" depends="jar, copySrc">
<echo>基于类路径的classname完成执行</echo>
<java classname="yuki.ant.app1.HelloWorld" classpath="build/classes">
<arg value="值1"/>
<arg value="值2"></arg>
</java>
<echo>基于jar文件执行</echo>
<java jar="build/dist/hello.jar" fork="true">
<arg value="value1"/>
<arg value="value2"/>
<arg value="value3"/>
</java>
</target>
</project>
17.property
文件路径从build改为bin
如果用原来的配置方法,就要修改所有的build
可以通过property来解决
新建文件build2.xml在build.xml的同级目录下
添加标签<property name="build.dir" value="build"></property>
把原来使用build的地方改成${build.dir}
<property name="build.dir" value="build"></property>
<target name="init">
<delete dir="${build.dir}"></delete>
<mkdir dir="${build.dir}" />
</target>
18.目录分割符
linux里不识别反斜杠作为目录的分割符
我们可以试着输出目录${build.dir}/classes
<property name="build.classes" value="${build.classes}"></property>
<target name="test">
<echo>${build.classes}</echo>
</target>
build2.xml>test
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build2.xml test: [echo] build/classes BUILD SUCCESSFUL Total time: 554 milliseconds
修改value为location
<property name="build.classes" location="${build.dir}/classes"></property>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build2.xml test: [echo] D:\Workspaces\Eclipse-Ant\app1-ant\build\classes BUILD SUCCESSFUL Total time: 583 milliseconds
当路径使用location时,会把路径转换为当前操作系统能够识别的路径
19.定义常量并替换
/app1-ant/build2.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">
<!-- <property name="build.dir" value="build"></property> -->
<property name="build.dir" value="binary"></property>
<!-- <property name="build.classes" value="${build.classes}"></property> -->
<!-- 使用属性定义相应的路径时,应该使用location -->
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.src" location="${build.dir}/src"></property>
<property name="build.lib.dir" location="${build.dir}/dist"></property>
<property name="execute.class" value="yuki.ant.app1.HelloWorld"></property>
<property name="jar.name" value="hello.jar"></property>
<!-- <target name="test">
<echo>${build.classes}</echo>
</target> -->
<fileset id="src.path" dir="src">
<include name="**/*.*"/>
</fileset>
<target name="init">
<delete dir="${build.dir}"></delete>
<mkdir dir="${build.dir}" />
<mkdir dir="${build.src}" />
<mkdir dir="${build.classes}" />
<mkdir dir="${build.lib.dir}" />
</target>
<target name="copySrc" depends="init">
<copy todir="${build.src}">
<fileset refid="src.path"></fileset>
</copy>
</target>
<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="src"></javac>
</target>
<target name="jar" depends="compile">
<jar destfile="${build.lib.dir}/${jar.name}" basedir="${build.classes}">
<manifest>
<attribute name="Main-Class" value="${execute.class}"/>
<attribute name="Build-By" value="YuKi"/>
</manifest>
</jar>
</target>
<target name="execute" depends="jar, copySrc">
<echo>基于jar文件执行</echo>
<java jar="${build.lib.dir}/${jar.name}" fork="true">
<arg value="value1"/>
<arg value="value2"/>
<arg value="value3"/>
</java>
</target>
</project>
修改完毕后执行build2.xml>execute
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build2.xml init: [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build2.xml:37: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\build\classes jar: [jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist\hello.jar copySrc: [copy] Copying 3 files to D:\Workspaces\Eclipse-Ant\app1-ant\build\src execute: [echo] 基于jar文件执行 [java] hellovalue1 [java] hellovalue2 [java] hellovalue3 BUILD SUCCESSFUL Total time: 2 seconds
把build改为bin后再执行,没有看到bin文件
原因是bin是项目的默认存放class文件的文件夹
build改为binary后再执行
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build2.xml init: [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\dist compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build2.xml:37: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\binary\classes jar: [jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\binary\dist\hello.jar copySrc: [copy] Copying 3 files to D:\Workspaces\Eclipse-Ant\app1-ant\binary\src execute: [echo] 基于jar文件执行 [java] hellovalue1 [java] hellovalue2 [java] hellovalue3 BUILD SUCCESSFUL Total time: 2 seconds
20.build.properties
在使用ant时首先要做的就是创建大量的属性
如果不是路径就使用value,如果是路径就使用location
当属性很多时,可以新建build.properties文件
路径不建议使用外部文件定义,定义在外部文件中的就只能是value了
写入:<property file="build.properties"></property>
/app1-ant/build.properties
execute.class = yuki.ant.app1.HelloWorld jar.name = hello.jar
/app1-ant/build3.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">
<property name="build.dir" value="binary"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.src" location="${build.dir}/src"></property>
<property name="build.lib.dir" location="${build.dir}/dist"></property>
<property file="build.properties"></property>
<fileset id="src.path" dir="src">
<include name="**/*.*"/>
</fileset>
<target name="init">
<delete dir="${build.dir}"></delete>
<mkdir dir="${build.dir}" />
<mkdir dir="${build.src}" />
<mkdir dir="${build.classes}" />
<mkdir dir="${build.lib.dir}" />
</target>
<target name="copySrc" depends="init">
<copy todir="${build.src}">
<fileset refid="src.path"></fileset>
</copy>
</target>
<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="src"></javac>
</target>
<target name="jar" depends="compile">
<jar destfile="${build.lib.dir}/${jar.name}" basedir="${build.classes}">
<manifest>
<attribute name="Main-Class" value="${execute.class}"/>
<attribute name="Build-By" value="YuKi"/>
</manifest>
</jar>
</target>
<target name="execute" depends="jar, copySrc">
<echo>基于jar文件执行</echo>
<java jar="${build.lib.dir}/${jar.name}" fork="true">
<arg value="value1"/>
<arg value="value2"/>
<arg value="value3"/>
</java>
</target>
</project>
执行build3.xml>execute
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build3.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\binary [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\classes [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\dist compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\build3.xml:30: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\binary\classes jar: [jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\binary\dist\hello.jar copySrc: [copy] Copying 3 files to D:\Workspaces\Eclipse-Ant\app1-ant\binary\src execute: [echo] 基于jar文件执行 [java] hellovalue1 [java] hellovalue2 [java] hellovalue3 BUILD SUCCESSFUL Total time: 2 seconds
21.Ant的值和环境变量的值
Ant本身有一些值ant.home,ant.version
<target name="test">
<echo>${ant.home}</echo>
<echo>${ant.version}</echo>
</target>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build4.xml test: [echo] D:\Idea\config\apache-ant-1.9.4 [echo] Apache Ant(TM) version 1.9.4 compiled on April 29 2014 BUILD SUCCESSFUL Total time: 493 milliseconds
还可以使用获取环境
<?xml version="1.0" encoding="UTF-8"?>
<project default="test">
<!-- <target name="test">
<echo>${ant.home}</echo>
<echo>${ant.version}</echo>
</target> -->
<!-- 把环境变量中的参数导出到evn这个变量中 -->
<property environment="env"></property>
<target name="test">
<echo>${env.JAVA_HOME}</echo>
<echo>${env.OS}</echo>
</target>
</project>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build4.xml test: [echo] C:\Java\jdk1.8.0 [echo] Windows_NT BUILD SUCCESSFUL Total time: 507 milliseconds
[ONE POINT ADVICE] Ant自动部署
之前曾经提到过热部署:http://www.cnblogs.com/kodoyang/p/Frame_Spring_AOP_AspectJ_Tutorial.html
在tomcat的jdk虚拟机参数中添加:-Dcom.sun.management.jmxremote=true
这里说的自动部署指的是当源代码发生改变时触发的部署行为
热部署是指单一java文件发生改变后,自动重新加载这个类
这里的自动部署指的是,文件发生修改触发的一个自动部署行为
自动部署运行在计算机上的其实和手动部署是一样的,只是由按预设完成而已
新建/app1-ant/autobuild.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="execute">
<property name="build.dir" value="binary"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.src" location="${build.dir}/src"></property>
<property name="execute.class" value="yuki.ant.app1.HelloWorld"></property>
<property name="jar.name" value="hello.jar"></property>
<fileset id="src.path" dir="src" includes="**/*.*"></fileset>
<target name="init">
<delete dir="${build.dir}"></delete>
<mkdir dir="${build.dir}" />
<mkdir dir="${build.src}" />
<mkdir dir="${build.classes}" />
</target>
<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="src"></javac>
</target>
<target name="execute" depends="compile">
<echo>基于类路径的classname完成执行</echo>
<java classname="${execute.class}" classpath="${build.classes}">
<arg value="值1" />
<arg value="值2" />
</java>
</target>
</project>
app1-ant>右键>properties>Builders>New…>Main

Name=New_Builder,Buildfile=${workspace_loc:/app1-ant/autobuild.xml}

Target>Auto Build>Set Targets>选择execute>OK

Build Options>保持Allocate Console(necessary for input)的勾选>
勾选Specify working set of relevant resources>Specify Resources…>
勾选app1-ant.src>Finish>Apply

如果修改名字New_Builder会报错
Errors occurred during the build. Errors running builder 'Integrated External Tool Builder' on project 'app1-ant'. The builder launch configuration could not be found. The builder launch configuration could not be found.
勾选Allocate Console后会在控制台输出ant的执行语句
Specify Resources…选择的是改动哪些文件后触发操作
显然,这样的自动部署是非常消耗性能的
在HelloWorld.java中输入两个空格后按下保存,看到
package yuki.ant.app1;
public class HelloWorld {
public static void main(String[] args) {
for(String arg : args)
System.out.println("hello" + arg);System.out.println("rrr"); //空格
}
}
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\autobuild.xml init: [delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\binary [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\src [mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\binary\classes compile: [javac] D:\Workspaces\Eclipse-Ant\app1-ant\autobuild.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to D:\Workspaces\Eclipse-Ant\app1-ant\binary\classes execute: [echo] 基于类路径的classname完成执行 [java] hello值1 [java] hello值2 [java] rrr BUILD SUCCESSFUL Total time: 6 seconds
目录结构:

本文参考了[孔浩Ant视频教程]的课程
更多好文请关注:http://www.cnblogs.com/kodoyang/

kongdongyang
2014/9/7
组建自动化工具Ant的更多相关文章
- 使用Chef管理windows集群 | 运维自动化工具
但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半 ...
- jmeter--接口自动化jmeter+ant+jenkins
的 一.介绍 接口自动化工具:jmeter+ant+jenkins 流程: -1.jmeter已录制或手动设置好脚本后 -2.配置ant,使用Ant工具,进行批量执行jmeter的脚本 -3.使用Je ...
- 自动化工具-jenkins
jenkins自动化工具使用教程 自动化构建.测试.部署.代码检测越来越重要.主要有一下几点原因 企业做大,项目变多,多端支持(web,h5,小程序等) 微服务提倡高内聚低耦合,项目因拆分变多 Dev ...
- Web自动化工具对比
首先说一下我对Web自动化测试与CS自动化测试的认识.从宏观对比都是通过脚本自动化完成功能的验证,区别不大.Web测试更为显著的浏览器兼容性.安全,以及与Web技术相关的表单测试.链接测试等,其实都是 ...
- web自动化工具-开篇
web自动化工具-开篇 最近几年,前端技术风一样的速度迭代更新,各种框架工具雨后春笋般涌现,作为一个平凡的开发者,也只能在洪流中沉沉浮浮,微不足道,以前前端叫做切图仔.美工,如今改了称号叫前端工程师, ...
- web自动化工具-liveStyle
web自动化工具-liveStyle LiveStyle. The first bi-directional real-time edit tool for CSS, LESS and SCSS主要用 ...
- web自动化工具-livereload
web自动化工具-livereload livereload是一个很神奇的工具,主要解放了F5键,监听文件变动,整个页面自动刷新.可搭载gulp等构建工具使用.和liveStyle 针对样式文件相比, ...
- web自动化工具-Browsersync
web自动化工具-Browsersync browser-sync才是神器中的神器,和livereload一样支持监听所有文件.可是和livereload简单粗暴的F5刷新相比,browsersync ...
- 前端自动化工具gulp自动添加版本号
之前,我介绍了学习安装并配置前端自动化工具Gulp,觉得gulp确实比grunt的配置简单很多,于是我决定再深入学习一下gulp,就去网上查了资料,发现gulp还可以自动添加版本号,这个功能就为我平时 ...
随机推荐
- SQL SERVER ->> Data Compression
最近做了一个关于数据压缩的项目,要把整个SQL SERVER服务器下所有的表对象要改成页压缩.于是趁此机会了解了一下SQL SERVER下压缩技术. 这篇文章几乎就是完全指导手册了 https://t ...
- 机器人学 —— 轨迹规划(Sampling Method)
上一篇提到,机器人轨迹规划中我们可以在 Configuration Space 中运行A* 或者 DJ 算法.无论A* 还是DJ 算法,都必须针对邻域进行搜索,如果2自由度则有4邻域,2自由度则有8邻 ...
- Echarts - js
<script type="text/javascript"> var myChart; myChart = echarts.init(document.getElem ...
- 【Tech】android真机测试——小米3
开始学习android了,自带的虚拟AVD慢的不忍直视,只能拿自己的小米3开刀了.弄了好久,记录如下. 首先,我承认到现在我不知道小米3的驱动到底是怎么安装的,我按照网上的方法自己下载过小米的驱动,但 ...
- Android 获取最近应用的缩略图
最近有项需求是获取应用的缩略,用于在动画时显示.因此就对此块知识简要了解了一下. 在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 新视频增加后需要SDCard重新扫描才能给新增 ...
- POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)
伸展数最基本操作的模板,区间求和,区间更新.为了方便理解,特定附上一自己搞的搓图 这是样例中的数据输入后建成的树,其中的1,2是加入的边界顶点,数字代表节点编号,我们如果要对一段区间[l, r]进行操 ...
- CSS和JavaScript标签style属性对照表
CSS和JavaScript标签style属性对照表一般情况是把"-"去掉,后面字母用大写. CSS语法 (不区分大小写) JavaScript语法 (区分大小写) border ...
- Spring事务的来龙去脉
引言 Spring是一个IOC框架,在IOC框架的基础上,提供了DAO集成,AOP事务控制,JNDI等等一系列的高级功能,个人觉得,在Spring中最值得称道的不仅仅它是一个非入侵的IOC容器,而在于 ...
- cdev、udev
udev :应用层的守护进程,由启动脚本加载,负责建立热拨插的接点 cdev :建立字符设备接口 platform device :相关平台直接总线建立的设备,主要出现需要自己直接挂到平台的时候,因为 ...
- leetcode:Insertion Sort List
Sort a linked list using insertion sort. 分析:此题要求在链表上实现插入排序. 思路:插入排序是一种O(n^2)复杂度的算法,基本想法就是每次循环找到一个元素在 ...