Ant快速入门学习 一看就懂
Ant是一种基于Java的build工具。
1、构建出错信息
BUILD FAILED
Error reading project file D:build.xml:1字节的UTF-8序列的字节1无效
解决:
直接在XML文件中更改UTF-8为GBK或GB2312
<?xml version="1.0" encoding="GB2312"?>
重新运行如下:
学习目标:basedir
<?xml version=
"1.0"
?>
<project name=
"projectStudy"
default
=
"sayBaseDir"
basedir=
"D:\myec"
>
<target name=
"sayBaseDir"
description=
"打印提示信息"
>
<echo message=
"The base dir is: ${basedir}"
/>
</target>
</project>
学习目标:创建目录
<?xml version=
"1.0"
?>
<project name=
"propertyStudy"
default
=
"mkdir"
description=
"创建目录"
>
<target name=
"mkdir"
>
<mkdir dir=
"mkdirTest"
/>
</target>
</project>
学习目标:property变量
<?xml version=
"1.0"
?>
<project name=
"propertyStudy"
default
=
"example"
>
<property name=
"name"
value=
"amigo"
/>
<property name=
"age"
value=
"25"
/>
<target name=
"example"
>
<echo message=
"name: ${name}, age: ${age}"
/>
</target>
</project>
学习目标:拷贝单个文件 与 文件夹
<?xml version=
"1.0"
?>
<project name=
"propertyStudy"
default
=
"copydir"
>
<target name=
"copyfile"
description=
"拷贝文件"
>
<copy file=
"antStudy.txt"
tofile=
"copy.txt"
/>
</target>
<target name=
"copydir"
description=
"拷贝目录"
>
<copy todir=
"../AntProject/src_copy"
>
<fileset dir=
"src"
/>
</copy>
</target>
</project>
学习目标:删除单个文件 与 文件夹 / 删除备份目录及空目录
<?xml version=
"1.0"
?>
<project name=
"propertyStudy"
default
=
"deldir"
>
<target name=
"delfile"
description=
"删除文件"
>
<delete file=
"src_copy/com/cyjch/test/AntTest.java"
/>
</target>
<target name=
"deldir"
description=
"删除目录"
>
<delete dir=
"../AntProject/src_copy"
/>
</target>
<target name=
"delBakAndEmpty"
description=
"删除空目录及备份目录"
>
<delete includeEmptyDirs=
"true"
>
<fileset dir=
"."
includes=
"**/*.bak"
/>
</delete>
</target>
</project>
学习目标:移动文件、目录
<?xml version=
"1.0"
?>
<project name=
"propertyStudy"
default
=
"moveDirToDir"
>
<target name=
"moveFile"
description=
"移动文件"
>
<move file=
"mkdirTest/cantStudy.txt"
tofile=
"cantStudy.txt"
/>
</target>
<target name=
"moveFileToDir"
description=
"移动目录"
>
<move file=
"cantStudy.txt"
todir=
"mkdirTest"
/>
</target>
<target name=
"moveDirToDir"
description=
"移动目录到目录,也可做为目录更名"
>
<move todir=
"mkdirTestNew"
>
<fileset dir=
"mkdirTest"
/>
</move>
</target>
</project>
学习目标:编译
<?xml version=
"1.0"
?>
<project name=
"javacTest"
default
=
"compile"
basedir=
"."
>
<target name=
"clean"
description=
"删除编译后产生的目录及文件"
>
<delete dir=
"build"
/>
</target>
<target name=
"compile"
depends=
"clean"
description=
"编译源文件"
>
<mkdir dir=
"build/classes"
/>
<javac srcdir=
"src"
destdir=
"build/classes"
/>
</target>
</project>
学习目标:编译并运行JAVA程序
<?xml version=
"1.0"
?>
<project name=
"javaTest"
default
=
"run"
basedir=
"."
>
<target name=
"clean"
description=
"清除目录"
>
<delete dir=
"build"
/>
</target>
<target name=
"compile"
depends=
"clean"
description=
"编译"
>
<mkdir dir=
"build/classes"
/>
<javac srcdir=
"src"
destdir=
"build/classes"
/>
</target>
<target name=
"run"
depends=
"compile"
description=
"运行"
>
<java classname=
"AntTest"
>
<classpath>
<pathelement path=
"build/classes"
/>
</classpath>
</java>
</target>
</project>
学习目标:打JAR包
<?xml version=
"1.0"
?>
<project name=
"javaTest"
default
=
"jar"
basedir=
"."
>
<target name=
"jar"
description=
"打JAR包"
>
<jar destfile=
"helloworld.jar"
basedir=
"build/classes"
>
<manifest>
<attribute name=
"Main-class"
value=
"HelloWorld"
/>
</manifest>
</jar>
</target>
</project>
学习目标:打WAR包
学习目标:mkdir srcdir destdir delete war jar
<?xml version=
"1.0"
?>
<!--默认为打包操作-->
<project name=
"structured"
default
=
"archive"
>
<description>编译并打包一个应用程序</description>
<!--在工程目录下创建输出目录:build/classes 和 dist-->
<target name=
"init"
description=
"创建目录"
>
<mkdir dir=
"build/classes"
/>
<mkdir dir=
"dist"
/>
</target>
<!--编译输出目录:srcdir-源目录和destdir-目标目录-->
<target name=
"compile"
depends=
"init"
description=
"编译"
>
<javac srcdir=
"src"
destdir=
"build/classes"
/>
</target>
<!--打包 , depends依赖哪些target-->
<target name=
"archive"
depends=
"compile"
description=
"打包"
>
<war destfile=
"dist/antwebproject.war"
basedir=
"build/classes"
/>
<jar destfile=
"dist/project.jar"
basedir=
"build/classes"
/>
</target>
<!--清理删除-->
<target name=
"clean"
depends=
"init"
description=
"清理"
>
<delete dir=
"build"
/>
<delete dir=
"dist"
/>
</target>
</project>
学习目标: webproject打War包
<?xml version=
"1.0"
?>
<project name=
"antwebproject"
default
=
"war"
basedir=
"."
>
<!--定义常量,任务可以引用-->
<property name=
"classes"
value=
"build/classes"
/>
<property name=
"build"
value=
"build"
/>
<property name=
"lib"
value=
"WebRoot/WEB-INF/lib"
/>
<!-- 删除build路径-->
<target name=
"clean"
>
<delete dir=
"build"
/>
</target>
<!-- 建立build/classes路径,并编译
class
文件到build/classes路径下-->
<target name=
"compile"
depends=
"clean"
>
<mkdir dir=
"${classes}"
/>
<javac srcdir=
"src"
destdir=
"${classes}"
/>
</target>
<!-- 打war包-->
<target name=
"war"
depends=
"compile"
>
<war destfile=
"${build}/AntProject.war"
webxml=
"WebRoot/WEB-INF/web.xml"
>
<!-- 拷贝WebRoot下除了WEB-INF和META-INF的两个文件夹 , **意为:匹配0或者更多的目录-->
<fileset dir=
"WebRoot"
includes=
"**/*.jsp"
/>
<!-- 拷贝lib目录下的jar包-->
<lib dir=
"${lib}"
/>
<!-- 拷贝build/classes下的
class
文件-->
<classes dir=
"${classes}"
/>
</war>
</target>
</project>
(技术历程:http://www.cnblogs.com/cyjch/archive/2012/03/28/2420761.html)
具体实例:利用ant 构建和部署Java项目和JavaWeb项目
Ant 可以代替使用 javac 、 java 和 jar 等命令来执行 java 操作,从而达到轻松的构建和部署 Java 项目的目的。
1. 利用ant 的javac命令来编译Java程序
Ant 的javac命令用于实现编译Java 程序的功能。下面来看一个简单的例子:首先我们建立名为 JavaTestPro的Java项目,建立src目录为源代码目录,在src目录下建立HelloWorld.java这个类文件。该类文件的内容如下:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world!");
}
}
同时在JavaTestPro项目的根目录下建立build.xml 文件,在该文件中编译sr 目录下的Java文件,并将编译后的class文件放入build/classes 目录中,整个项目的目录结构如下:
|JavaTestPro
|src
|build
|classes
|build.xml
在编译前,需清除classes 目录,该文件的内容如下:
<?xml version="1.0" ?>
<project name ="javacTest" default="compile" basedir=".">
<target name="clean">
<delete dir="${basedir}/build"/>
</target>
<target name="compile" depends ="clean">
<mkdir dir ="${basedir}/build/classes"/>
<javac srcdir ="${basedir}/src" destdir ="${basedir}/build/classes"/>
</target>
</project>
在项目根目录(C:\ThinkInJavaCode\JavaTestPro)执行ant命令后,可在该目录下发现新生成的build/classes子目录,编译后生成的HelloWorld.class文件就在该目录下。
2. 使用java命令执行Java程序
Ant 中可以使用 java命令实现运行Java程序的功能。可以在上面的build.xml基础上做修改来实现:
<?xml version="1.0" ?>
<project name ="javacTest" default="run" basedir=".">
<target name="clean">
<delete dir="${basedir}/build"/>
</target>
<target name="compile" depends ="clean">
<mkdir dir ="${basedir}/build/classes"/>
<javac srcdir ="${basedir}/src" destdir ="${basedir}/build/classes"/>
</target>
<target name="run" depends ="compile">
<java classname ="HelloWorld">
<classpath>
<pathelement path="${basedir}/build/classes"/>
</classpath>
</java>
</target>
</project>
接着,就可以在控制台看见输出:"[java] hello world!"
3. 使用jar命令生成jar文件
还可以在上例的基础上更进一步,来生成jar包,可在run 这个 target 下再加上如下 target :
<?xml version="1.0" ?>
<project name ="javacTest" default="jar" basedir=".">
<target name="clean">
<delete dir="${basedir}/build"/>
</target>
<target name="compile" depends ="clean">
<mkdir dir ="${basedir}/build/classes"/>
<javac srcdir ="${basedir}/src" destdir ="${basedir}/build/classes"/>
</target>
<target name="run" depends="compile">
<java classname ="HelloWorld">
<classpath>
<pathelement path="${basedir}/build/classes"/>
</classpath>
</java>
</target>
<target name="jar" depends="run">
<jar destfile="helloworld.jar" basedir="${basedir}/build/classes">
<manifest>
<attribute name="Main-class" value="HelloWorld"/>
</manifest>
</jar>
</target >
</project>
其中,project的default 属性设置为应设为jar,ant运行完毕后,可看到在项目的根目录下生成了一个 helloworld.jar的jar包 。可通过运行以下命令来执行该jar包:
java -jar helloworld.jar
4. 使用war命令打包JavaEE项目
建立一个JavaEE项目,其中src 为Java源代码目录,WebContent为各jsp存放目录,lib 为项目引用的的包的目录。在WebTest项目目录下建立了build.xml 文件,该文件为该工程的 Ant 构件文件。依次建立如下文件及文件夹(build可以不用,待会生成文件自动生成):
|WebContent
|src
|build
|classes
|WebContent
|META-INF
|MANIFEST.MF
|WEB-INF
|lib
|classes
|web.xml
|HelloJSP.jsp
|build.xml
读者可以 src 目录下放入在前续例子中开发的 HelloWorld.java 文件,并在 WebContent下建立 HelloJSP.jsp 文件,其内容很简单,就是输出 Hello 信息,代码如下所示:
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>war test for ant</title>
</head>
<body>
Hello JSP!Hello Ant!
</body>
</html>
接下来编写 build.xml 文件,其内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<project name ="WebTest" default ="war" basedir =".">
<property name ="classes" value ="${basedir}/build/classes"/>
<property name ="build" value ="${basedir}/build"/>
<property name ="lib" value ="${basedir}/WebContent/WEB-INF/lib"/>
<!-- 删除build 路径-->
<target name ="clean">
<delete dir ="${build}"/>
</target>
<!-- 建立build/classes 路径,并编译class 文件到build/classes 路径下-->
<target name ="compile" depends ="clean">
<mkdir dir ="${classes}"/>
<javac srcdir ="${basedir}/src" destdir ="${classes}"/>
</target>
<!-- 打war 包-->
<target name ="war" depends ="compile">
<war destfile ="${build}/WebTest.war" webxml ="${basedir}/WebContent/WEB-INF/web.xml">
<!-- 拷贝WebRoot 下除了WEB-INF 和META-INF 的两个文件夹-->
<fileset dir ="${basedir}/WebContent" includes ="**/*.jsp"/>
<!-- 拷贝lib 目录下的jar 包-->
<lib dir ="${lib}"/>
<!-- 拷贝build/classes 下的class 文件-->
<classes dir ="${classes}"/>
</war>
</target>
</project>
在C:\ThinkInJavaCode\WebTest目录下运行ant后,就生成了WebTest.war 文件了,然后可以将其放入Web容器(如Tomcat)的相应目录下(${Tomcata安装目录}\webapps)运行该web项目了。
(技术历程:http://www.cnblogs.com/philander/articles/1782254.html)