`
hyz301
  • 浏览: 370701 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hadoop学习路线之:ant简介及其使用

阅读更多

因为学习hadoop需要编译hadoop2.5.2eclipse插件,故需要学习下apache ant。本文首先对apache ant做一个简要的介绍,其次,介绍ant的安装及使用,至于对hadoop2.5.2eclipse插件的编译,将在下一篇记录中向大家介绍。

强烈推荐apache ant官方文档:http://ant.apache.org/manual/index.html

这是每一个想入门或者进一步掌握apache ant最好的文档。

Apache Ant 简介

Apache Ant是目前事实上的Java应用的标准build脚本工具。使它大受欢迎的一个主要愿意是它的和灵活,允许程序员创建自己的Task来对Ant进行扩展。

Ant的安装

Apache AntApache基金会下的一个项目,可以在http://ant.apache.org下载到。根据不同的平台下载不同的压缩包,直接解压到安装目录就可以了,不需要安装。解压后,在PATH环境变量中添加Ant的安装目录。

Ant简介

我想大家都听说过很多build工具,如makemaven等。那么为什么我们选择Ant呢?简单说,Ant有以下这两个优点。

使用java开发,并用xml存储build信息,因此是跨平台的。

程序员可以自己扩展Ant。程序员可以自己写java程序来扩展Ant,创建自己的tasks

make这一类的工具是基于操作系统shell的,因此移植性不好。并且Ant也可以通过<exec>标签来实现调用shell,但这样会是以可移植性为代价的。

 Ant使用XML来存储build信息,在xml文件里有很多task的定义,默认使用的文件是build.xml

FirstBuild.xml

通过实例来说明Antbuild.xml文件的结构会更清晰一些,这里使用的build文件是FirstBuild.xml,它实现了创建一个文件夹并拷贝一个文件进入这个文件夹。

首先,要有<project>元素:

 

<!--
A simple build script that creates a directory (dir.name), and copies
a file (file.name) to it.
-->
<project name="MyFirstAntProject" basedir="." default="copyfile">

 

<project>中的name属性标识工程名,basedir指示根目录,default标识默认执行的target。如果运行ant时不指定这些属性,Ant将执行这个target

下一步,定义这个工程所使用的properties

<property name=”dir.name” value=”${basedir}/mydir”/>  
<property name=”file.name” value=”file1.txt”/>  

 

这里定义了两个全局属性,分别是dir.namefile.name。这些属性是可选的,但使用属性会更方便,尤其是便于维护。一种更有效的方式是将这些属性放到一个专门文件里,从而使这个xml文件更加灵活,易于重用。

因为这个build文件很简单,所以没有taskpath的定义。

最后,定义所要执行的targets

<target name=”makedirectory” description=”Create directory mydir”>  
<mkdir dir=”${dir.name}”/>  
</target>  
<target name=”copyfile” depends=”makedirectory” description=”Copy files”>  
<copy file=”${file.name}” todir=”${dir.name}”/>  
</target>  
<target name=”clean” description=”Clean up task”>  
<delete dir=”${dir.name}”/>  
</target  
</project>  

 

注意copyfile中的dependsdenpends属性意味着在执行copyfile之前,makedirectorytarget一定要先执行。

Ant的语法结构一般是:

ant -buildfile <filename> <target-name>  

如果没有使用-fuildfile参数,那么Ant将默认使用build.xml,如果没有build.xml,那么Ant将报错,如下:(这也可以用来验证ant是否成功安装)

$ ant  

Buildfile: build.xml does not exist!  

Build failed  

因为我们的build文件名不是build.xml,因此需要添加-buildfile这个参数。下面是使用-buildfile参数的结果:

$ ant -buildfile FirstBuild.xml  

Buildfile: FirstBuild.xml  

makedirectory:  

[mkdir] Created dir: /home/tomcat/AppendixB/mydir  

copyfile:  

[copy] Copying 1 file to /home/tomcat/AppendixB/mydir  

BUILD SUCCESSFUL  

Total time: 1 second  

在文件中定义的property可以被Ant的参数所覆盖,如下面使用yourdir取代mydir

$ ant -buildfile FirstBuild.xml -Ddir.name=yourdir  

Buildfile: FirstBuild.xml  

makedirectory:  

[mkdir] Created dir: /home/tomcat/AppendixB/yourdir  

copyfile:  

[copy] Copying 1 file to /home/tomcat/AppendixB/yourdir  

BUILD SUCCESSFUL  

Total time: 1 second

一般在build.xml中都会有clean这个target,其作用有些像卸载软件程序:

$ ant -buildfile FirstBuild.xml clean  

Buildfile: FirstBuild.xml  

clean:  

[delete] Deleting directory /home/tomcat/AppendixB/mydir  

BUILD SUCCESSFUL  

Total time: 0 seconds 

使用Ant创建web应用程序

前面的那个Ant任务比较简单,下面介绍一个有些复杂又十分常用的Ant文件:我们要用Ant来构建一个web应用程序。

src放置java文件,webjsp等页面文件和配置文件,dist用来存放生成的war文件,build用来放编译好的servletlib用来放库文,doc用来放生成的javadoc,以及最主要的,build.xml。其中,builddistdoc是使用Ant脚本来生成的。

build.xml

build.xml的第一部分:

<!-- Ant build file for a sample web application -->  

<project name=”SampleWebApplication” default=”compile” basedir=”.”>  

下一部分还是全局可用的properties

<property name=”tomcat.home” value=”/usr/tomcat/apache-tomcat-6 “/>  
<property name=”app.name” value=”sampleWebapp”/>  
<property name=”context.path” value=”/${app.name}”/>  
<property name=”src.home” value=”${basedir}/src”/>  
<property name=”web.home” value=”${basedir}/web”/>  
<property name=”conf.home” value=”${basedir}/conf”/>  
<property name=”lib.home” value=”${basedir}/lib”/>  
<property name=”docs.home” value=”${basedir}/docs”/>  
<property name=”build.home” value=”${basedir}/build”/>  
<property name=”dist.home” value=”${basedir}/dist”/>  
<property name=”war.file” value=”${dist.home}/${app.name}.war”/>  
<!-- Configure properties to access the Tomcat Manager application -->  
<property name=”tomcat.manager.url” value=”http://localhost:8080/manager”/>  
<property name=”tomcat.username” value=”tomcat”/>  
<property name=”tomcat.password” value=”tomcat”/> 

 

clean target,用于删除builddist目录及其所有子目录。

<!-- ====== Clean Target ====== -->  

<target name=”clean”  

description=”Cleanup- deletes everything generated by the ant script”>  

<delete dir=”${build.home}”/>  

<delete dir=”${dist.home}”/>  

<delete dir=”${docs.home}”/>  

</target>

init target用于完成初始化工作,包括创建目录结构,初始化Java CLASSPATHCLASSPATH中包含这个web应用程序使用的所有库文件。下面的代码将servlet-api.jar包进来。

<!-- ====== All initializations: Classpath, directory structure ====== -->  

<target name=”init”>  

<mkdir dir=”${build.home}”/>  

<mkdir dir=”${docs.home}”/>  

<mkdir dir=”${dist.home}”/>  

<!-- Classpath for compiling web application, generating javadocs -->  

<path id=”classpath”>  

<fileset dir=”${lib.home}”>  

<include name=”*.jar”/>  

</fileset>  

<fileset dir=”${tomcat.home}/lib”>  

<include name=”servlet-api.jar”/>  

</fileset>  

</path>  

<property name=”classpath” refid=”classpath”/>  

</target> 

compile target将编译src目录中所有的java文件,生成的class文件将被存放到destdir属性所指的目录中,compile必需是在init完成之后才能执行。如果使用compile命令,则将自动先执行init

<!-- ====== Compilation ====== -->  

<target name=”compile” depends=”init”>  

<echo message=”Classpath set to ${classpath}”/>  

<javac srcdir=”${src.home}”  

destdir=”${build.home}”  

debug=”true”  

classpath=”${classpath}”  

deprecation=”true”>  

</javac>  

<!-- Copy all property files -->  

<copy todir=”${build.home}”>  

<fileset dir=”${conf.home}”/>  

</copy>  

</target> 

dist target将创建一个war文件。

<!-- ====== Create a distributable WAR file ====== -->  

<target name=”dist” depends=”compile”  

description=”Creates the deployable WAR file”>  

<war destfile=”${war.file}”  

webxml=”${web.home}/WEB-INF/web.xml”>  

<fileset dir=”${web.home}” excludes=”**/web.xml” />  

<lib dir=”${lib.home}”/>  

<classes dir=”${build.home}”/>  

</war>  

</target> 

javadoc target将为源码创建javadoc文档。

<!-- ====== Create documentation (javadocs) ====== -->  

<target name=”javadoc” depends=”init” description=”Creates the Javadocs for the  

project”>  

<javadoc sourcepath=”${src.home}”  

packagenames=”com.foobar.*”  

classpath=”${classpath}”  

destdir=”${docs.home}”  

windowtitle=”Javadoc for the Sample Web Application (TM)”>  

</javadoc>  

all target将上面这些任务按在depends中的顺序组织起来。

<!-- ====== All Target ====== -->  

<target name=”all”  

depends=”clean, prepare, compile, dist”  

description=”Builds the web application and war file”/>  

使用Ant管理web应用程序

继续上面的那个build.xml文件,在其中添加下面的这些代码,就可以通过Ant管理这个web应用程序了。

<!-- Configure properties to access the Tomcat Manager application -->  

<property name=”tomcat.manager.url” value=”http://localhost:8080/manager”/>  

<property name=”tomcat.username” value=”tomcat”/>  

<property name=”tomcat.password” value=”tomcat”/>  

<!-- Classpath for Tomcat ant tasks -->  

<path id=”deployer.classpath”>  

<fileset dir=”${tomcat.home}/lib”>  

<include name=”*.jar”/>  

</fileset>  

</path  

...  

<!-- ====== Manage web application ====== -->  

<target name=”deploy” depends=”dist” description=”Deploy web application”>  

<deploy url=”${tomcat.manager.url}” username=”${tomcat.username}”  

password=”${tomcat.password}” path=”${context.path}”  

war=”${war.file}” update=”true” />  

</target>  

<target name=”undeploy” description=”Undeploy web application”>  

<undeploy url=”${tomcat.manager.url}” username=”${tomcat.username}”  

password=”${tomcat.password}” path=”${context.path}”/>  

</target>  

<target name=”start” description=”Start web application”>  

<start url=”${tomcat.manager.url}” username=”${tomcat.username}”  

password=”${tomcat.password}” path=”${context.path}”/>  

</target>  

<target name=”reload” description=”Reload web application”>  

<reload url=”${tomcat.manager.url}” username=”${tomcat.username}”  

password=”${tomcat.password}” path=”${context.path}”/>  

</target>  

<target name=”stop” description=”Stop web application”>  

<stop url=”${tomcat.manager.url}” username=”${tomcat.username}”  

password=”${tomcat.password}” path=”${context.path}”/>  

</target>  

<target name=”list” description=”List all web applications on server”>  

<list url=”${tomcat.manager.url}” username=”${tomcat.username}”  

password=”${tomcat.password}”/>  

</target> 

下载apache-ant-1.9.6

http://ant.apache.org/bindownload.cgi

一、解压ant安装包在E:\HadoopProgram

二、环境变量配置

ANT_HOME

E:\HadoopProgram\apache-ant-1.9.6

CLASSPATH

;%ANT_HOME%\lib;

PATH  ;%ANT_HOME%bin;

三、测试是否安装成功

cmd命令方式下输入:ant -version

 

 

出现问题:

1Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\lib

命令行敲ant命令后提示:“Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\lib”ANT_HOME环境变量已经配置;

解决途径:将“C:\Program Files\Java\jdk1.6.0_16\lib”目录下的tools.jar文件拷贝到“C:\Program Files\Java\jre6\lib”目录下,重新运行命令ant,运行正常,问题解决。

2)在cmd命令中:输入ant,如果输出: Buildfile:build.xml does not exist!

Build failed

说明ant安装成功。

四、运行第一个ant脚本 
编写build.xml文件保存到任意位置。如:E:\workspace
内容如下: 

<?xml version="1.0" encoding="UTF-8"?>

<project name="The test of ant" default="mkdir" basedir=".">

    <target name="mkdir">

         <mkdir dir="E://testMKDir"/>

    </target>

</project>
  运行: 
  Buildfile: ant -f E:\workspace\build.xml

运行结果:
  Buildfile: E:\workspace\build.xml

mkdir:

BUILD SUCCESSFUL

Total time: 0 seconds

检查在目录E下生成testMKDir文件夹

通过IDE来使用ant

目前的Eclipse都集成了ant,本文图示如何在eclipse下使用ant

1.新建Java Project-新建Java文件HelloWorld.java

HelloWorld.java

package example;

public class HelloWorld {

    public static void main(String[] args) {

       System.out.println("Hello World");

    }

}

 

2.在工程根目录下新建build.xml

build.xml

<?xml version="1.0" encoding="utf-8"?>

<project default="main" basedir=".">

    <target name="main" depends="compile, compress" description="Main target">

       <echo>Building the .jar file.</echo>

    </target>

    <target name="compile" description="Compilation target">

       <javac srcdir="${basedir}/src/example" />

    </target>

    <target name="compress" description="Compression target">

       <jar jarfile="HelloWorld.jar" basedir="${basedir}/src/example"includes="*.class" />

    </target>

</project>

此脚本文件内容是编译/src/example下的java文件,并就地生成class文件,将这个class文件打成jar包,HelloWorld.jar

此时工程的目录结构如下图所示:



 
<!--[endif]-->

右键选中HelloAnt工程,选择Properties

选择Builders-New…,选择Ant Build

NameAnt_Builder

Buildfile${workspace_loc:/HelloAnt/build.xml}

Base Directory${workspace_loc:/HelloAnt}

(按“Browse Workspace”选择工程根目录)

Builder面板中钩上Ant_Build,去掉Java Builder,即可编译执行。

每次编译时,右键build.xml,选择Run As-Ant Build

此示例工程编译结果:

Buildfile: D:\dev\Workspaces\J2EE\HelloAnt\build.xml

compile:

compress:

main:

     [echo] Building the .jar file.

BUILD SUCCESSFUL

Total time: 281 milliseconds

问题:ant编译中出现“includeantruntime was not set”警告的问题

执行ant编译时,总会出现如下的警告:
[javac] D:\SnowPad\build.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
虽然不影响编译,但还是解决才安心。其实解决方法也很简单,只需要根据提示在javac任务中添加includeAntRuntime="false"属性即可。例如:
修改前:
    <javac srcdir="${srcDir}" destdir="${binDir}" />
修改后:
    <javac srcdir="${srcDir}" destdir="${binDir}" includeAntRuntime="false" />
注:
1.对于includeAntRuntime属性,官方的解释如下:
    Whether to include the Ant run-time libraries in the classpath; defaults to yes, unless build.sysclasspath is set. It is usually best to set this to false so the script's behavior is not sensitive to the environment in which it is run.
2.此警告在较早的ant版本中可能不会出现,当前用的版本是:Apache Ant(TM) version 1.8.2 compiled on December 20 2010。所以此问题跟ant版本有关。

 参考文章:

http://blog.csdn.net/jubincn/article/details/4897610

http://blog.sina.com.cn/s/blog_62ef85c201016e2n.html

http://zhangjunhd.blog.51cto.com/113473/128317/

 

 

  • 大小: 20.6 KB
  • 大小: 30.4 KB
  • 大小: 6.6 KB
  • 大小: 8.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics