组建自动化工具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

时间: 2024-10-06 20:34:31

组建自动化工具Ant的相关文章

使用Chef管理windows集群 | 运维自动化工具

但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半天时间.虽然你编写了大量的shell(或python,perl)脚本来实现各种自动化场景,但最后会发现你又陷入了脚本的汪洋大海之中,管理和维护这么多的脚本的成本也不小.你需要一款基础设施自动化工具,希望它能具有以下功能. 1批量执行 这个不多说了吧,试想要为每一台机器打补丁的情形吧. 2任务编排 现

Grunt-beginner前端自动化工具-高清视频

[课程介绍]: 作为一名开发人员,在WEB前端项目开发中,重复而枯燥的工作太多,Grunt自动化的项目构建工具,帮你解决这些问题,对重复执行的任务像压缩,合并,编译,单元测试及代码检查等, 通过配置Grunt自动化工具,可以减轻您的劳动,简化您的工作. [课程须知]: 1.想提高运行前端开发工作流程 : 2.有一些项目开发经验,效果会更好. [你能学到什么?] 1. Grunt工具和插件的安装 2. 如何进行项目配置 3. 如何配置任务 4. 如何执行任务 5. Grunt的使用 [课程提纲]:

运维自动化工具Cobbler之——安装实践

运维自动化工具--Cobbler实践 第1章 About Cobbler 1.1 Cobbler Introduction Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等. Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用.Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web

前端自动化工具 grunt 插件的简单使用(一)

Grunt 的简介: Grunt 是一套前端自动化工具,是一个基于 node.js 的命令行工具,它一般用于: 1.压缩文件: 2.合并文件: 3.简单的语法检测: 4.监听文件变动: 5.less 编译: PS:Grunt 官网 (https://gruntjs.com/).Grunt  就像是一个工具箱,拥有非常丰富的任务插件,可以帮助开发人员实现各种各样目标任务的构建.在Grunt工具箱中,按目标任务类型我们可以分为: 1.编译文档型:比如编译 LESS.Sass.Stylus.Coffe

Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第一篇)

Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第一篇) 一.前言 大家好,今天我要来讲讲一个比较实用的爬虫工具,抓取淘宝的关键字商品信息,即是: 输入关键字,按照价格等排序,抓取列出的商品信息以及下载图片,并且支持导出为Excel. 如果如下: 看完下面的讲解,Python语言就掌握得差不多,中级水平了,而且这个封装后的工具还是很好用的. 感觉自己萌萌哒~~ 二.原理 大家知道什么叫爬虫,它也叫网络蜘蛛,机器人等,意思就是说自动的程序,可以去抓取使用网络协议传输的内容. 目前来讲爬虫主

前端自动化工具 -- fis 使用简介

FIS也是一个新生的前端自动化工具,好下面就来简单介绍一下,真的好简单介绍啊.. 个人觉得FIS已经囊括了一个“大前端”的范畴. 先来看看别人怎么看FIS和Grunt和Gulp F.I.S在实际项目中的应用体验如何? 前端工具里面gulp和fis,有哪些优缺点? FIS和Grunt的区别 FIS入门很简单,提高难度也不大,因为官方已经提供了非常多的文档 围绕着最基本的三条命令 fis install <name> fis release [options] fis server <com

自动化工具——ansible

一.什么是ansible 我们在管理服务器时首先应该是安装操作系统,而关于自动化安装操作系统工具常见的有:cobbler.而在装完操作系统后就是配置系统的相关服务的配置文件,而关于此类的自动化工具常见的有:puppet.saltstack.而日常维护中我们还需要在各主机执行相关命令等操作,而此类的自动化工具就有:func.fabric已经ansible.最后也是重要的监控系统运行状态的相关自动化工具:cacti.nagios和zabbix等.下面就是将ansible工具的使用. ansible是

自动化运维-自动化工具其实只是个噱头

现在运维圈子里都流行利用各个自动化工具进行运维,个人感觉,这些只是一个噱头,是一些培训机构利益获取的幌子,也是各个运维人员提高身价的砝码,本身并没有什么. 对于大批量的系统运维,不外乎几大业务场景: 1.统一配置管理 (如批量更改服务器的某个参数,批量上传一个文件,批量更改服务器的一个文件) 有人说puppet可以做得很好,其实,写个循环脚本,针对每一个ip,执行一个实现配置功能shell脚本 (ssh可以实现远程更改一个参数,远程更改一个文件,scp可以实现upload一个文件),是一个件ea

python模块之lib2to3(py2转py3自动化工具)

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之lib2to3(py2转py3自动化工具) #http://tieba.baidu.com/p/3939904893 #操作步骤: 1.需要转换test.py文件为py3代码 #test.py文件放置在Scripts目录下,如果test.py文件不放置在Scripts目录下则 -w后面写完整的路径 #如: C:\python27\Tools\Scripts>2to3.py -w C:\P