首页 > 代码库 > 组建自动化工具Ant

组建自动化工具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>antBuildfile: build.xml does not exist!Build failedC:\Users\yuki> 

2.编译
ant运行后会找build.xml文件
D:\Ant\app1下新建HelloWorld.javabuild.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>antBuildfile: D:\Ant\app1\build.xmlcompile:[javac] D:\Ant\app1\build.xml:5: warning: ‘includeantruntime‘ was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds[javac] Compiling 1 source file to D:\Ant\app1BUILD SUCCESSFULTotal time: 2 secondsD:\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>antBuildfile: D:\Ant\app1\build.xmlexecute:[java] hello world!BUILD SUCCESSFULTotal time: 0 secondsD:\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>antBuildfile: D:\Ant\app1\build.xmlcompile:[javac] D:\Ant\app1\build.xml:5: warning: ‘includeantruntime‘ was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds[javac] Compiling 1 source file to D:\Ant\app1execute:[java] hello world!BUILD SUCCESSFULTotal time: 1 secondD:\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.xmlcreate:[mkdir] Created dir: D:\Workspaces\Eclipse-Ant\app1-ant\buildBUILD SUCCESSFULTotal time: 720 milliseconds

项目右键>Refresh>新建了文件夹build
删除文件夹可以使用delete

 

   <target name="remove">        <delete dir="build"/>    </target>

 

 

 

Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xmlremove:[delete] Deleting directory D:\Workspaces\Eclipse-Ant\app1-ant\buildBUILD SUCCESSFULTotal 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.xmlinit:[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\distcopySrc:[copy] Copying 1 file to D:\Workspaces\Eclipse-Ant\app1-ant\build\srcBUILD SUCCESSFULTotal 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.xmlinit:BUILD SUCCESSFULTotal 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.xmlinit:[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\distBUILD SUCCESSFULTotal time: 491 milliseconds 

9.复制文件夹下所有的文件
src下新建text.properties,执行copySrc,看到这个文件没有被复制
文件集改为includes="**/*.*"后,看到文件已被复制

<fileset dir="src" includes="**/*.*"></fileset>

 

Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build.xmlinit:[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\distcopySrc:[copy] Copying 2 files to D:\Workspaces\Eclipse-Ant\app1-ant\build\srcBUILD SUCCESSFULTotal 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.xmlinit:[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\distcompile:[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\classesBUILD SUCCESSFULTotal 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.xmlinit:[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\destcompile:[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\classesjar:[jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dest\hello.jarBUILD SUCCESSFULTotal 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.0Ant-Version: Apache Ant 1.9.4Created-By: 1.8.0_20-b26 (Oracle Corporation)Main-Class: yuki.ant.app1.HelloWorldBuild-By: YuKi 

14.运行程序
可以依赖jarcompile,基于类路径的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.xmlinit:[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\destcompile:[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\classesjar:[jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dest\hello.jarexecute:[java] hello值1[java] hello值2BUILD SUCCESSFULTotal 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.xmlinit:[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\distcompile:[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\classesjar:[jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist\hello.jarexecute:[echo] 基于类路径的classname完成执行[java] hello值1[java] hello值2[echo] 基于jar文件执行BUILD FAILEDD:\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.xmlinit:[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\distcompile:[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\classesjar:[jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist\hello.jarexecute:[echo] 基于类路径的classname完成执行[java] hello值1[java] hello值2[echo] 基于jar文件执行[java] hellovalue1[java] hellovalue2[java] hellovalue3BUILD SUCCESSFULTotal 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="http://www.mamicode.com/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.xmltest:[echo] build/classesBUILD SUCCESSFULTotal time: 554 milliseconds

修改valuelocation

 

<property name="build.classes" location="${build.dir}/classes"></property>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build2.xmltest:[echo] D:\Workspaces\Eclipse-Ant\app1-ant\build\classesBUILD SUCCESSFULTotal time: 583 milliseconds

当路径使用location时,会把路径转换为当前操作系统能够识别的路径

19.定义常量并替换

/app1-ant/build2.xml

<?xml version="1.0" encoding="UTF-8"?><project default="execute">        <!-- <property name="build.dir" value="http://www.mamicode.com/build"></property> -->    <property name="build.dir" value="binary"></property>    <!-- <property name="build.classes" value="http://www.mamicode.com/${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.xmlinit:[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\distcompile:[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\classesjar:[jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\build\dist\hello.jarcopySrc:[copy] Copying 3 files to D:\Workspaces\Eclipse-Ant\app1-ant\build\srcexecute:[echo] 基于jar文件执行[java] hellovalue1[java] hellovalue2[java] hellovalue3BUILD SUCCESSFULTotal time: 2 seconds

build改为bin后再执行,没有看到bin文件
原因是bin是项目的默认存放class文件的文件夹
build改为binary后再执行

Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build2.xmlinit:[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\distcompile:[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\classesjar:[jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\binary\dist\hello.jarcopySrc:[copy] Copying 3 files to D:\Workspaces\Eclipse-Ant\app1-ant\binary\srcexecute:[echo] 基于jar文件执行[java] hellovalue1[java] hellovalue2[java] hellovalue3BUILD SUCCESSFULTotal 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.HelloWorldjar.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.xmlinit:[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\distcompile:[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\classesjar:[jar] Building jar: D:\Workspaces\Eclipse-Ant\app1-ant\binary\dist\hello.jarcopySrc:[copy] Copying 3 files to D:\Workspaces\Eclipse-Ant\app1-ant\binary\srcexecute:[echo] 基于jar文件执行[java] hellovalue1[java] hellovalue2[java] hellovalue3BUILD SUCCESSFULTotal time: 2 seconds 

21.Ant的值和环境变量的值
Ant本身有一些值ant.homeant.version

    <target name="test">        <echo>${ant.home}</echo>        <echo>${ant.version}</echo>    </target>
Buildfile: D:\Workspaces\Eclipse-Ant\app1-ant\build4.xmltest:[echo] D:\Idea\config\apache-ant-1.9.4[echo] Apache Ant(TM) version 1.9.4 compiled on April 29 2014BUILD SUCCESSFULTotal 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.xmltest:[echo] C:\Java\jdk1.8.0[echo] Windows_NTBUILD SUCCESSFULTotal 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.xmlinit:[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\classescompile:[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\classesexecute:[echo] 基于类路径的classname完成执行[java] hello值1[java] hello值2[java] rrrBUILD SUCCESSFULTotal time: 6 seconds

 

 

目录结构:

 

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


kongdongyang
2014/9/7

 

 

 

组建自动化工具Ant