备忘:生成自己的build.xml,jar包

参考:
[url]http://www.roseindia.net/tutorials/ant/Howtogeneratebuildfile.shtml[/url] 主要

[url]http://techtracer.com/2007/04/16/the-great-ant-tutorial-a-great-jump-start[/url] 参照classpath,javac中要用到

目标是可以用bin/hadoop 命令在终端执行jar包中某个class的main函数,不用在eclipse中跑。

1.使用eclipse中File-->Export--->General-->Ant Buildfiles生成一个初始的build.xml主要是用到其生成的classpath,否则自己写太麻烦了。

2.参考上面的第一个链接,自己写一个:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
<project basedir="." default=[color=red]"jar"[/color] name="PscNew">
<property environment="env"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
[color=red] <property name="dir.src" value="src"/>
<property name="dir.build" value="build"/>
<property name="dir.dest" value="dest"/>[/color]

<path id="PscNew.classpath">
<pathelement location="../../../src/hadoop-0.20.2/lib/junit-3.8.1.jar"/>
[color=red]...(中间还有很多)[/color]
<pathelement location="lib/zookeeper-3.2.2.jar"/>
</path>

<target name="clean" description="Removing the all generated files.">
<delete dir="${dir.build}"/>
<delete dir="${dir.dest}"/>
</target>

<target name="prepare" depends="clean">
<mkdir dir="${dir.build}"/>
<mkdir dir="${dir.dest}"/>
<mkdir dir="${dir.src}"/>
</target>

<target name="compile" depends="prepare" description="Compilation of all source code.">
<javac srcdir="${dir.src}" destdir="${dir.build}">
[color=red] <classpath refid="PscNew.classpath"/>[/color]
</javac>
</target>

<target name="jar" depends="compile" description="Generates PscNew.jar file in to the 'dest' directory.">
<jar jarfile="${dir.dest}/[color=red]psc.jar[/color]" basedir="${dir.build}">
[color=red]<manifest>
<attribute name="Main-Class" value="clusterText/MainDriver"/>[/color]
</manifest>
</jar>
</target>
</project>

3.上面的注意几点:
(1)project 中default="jar"说明假如你用ant命令的话(就在终端执行ant,则会默认执行target:jar,当然你也可以用ant jar)
(2)我的classpath(PscNew.classpath)是用eclipse生成的,不用自己去写,eclipse中生成的其他东西还是删掉吧,没用
(3)javac中要用到classpath
(4)设置Main-Class:MainDriver是一个类(这里不用跟.class/.java)clusterText包在src下。设置Main-Class后比如你执行bin/hadoop jar psc.jar的话它就会自动执行Main-Class的main函数
(5)主要设置好src的路径就好了,它下面的包的路径不用理

4.在终端cd到那个目录:输入ant回车,就生成需要的包${dir.dest}/psc.jar

5.bin/hadoop jar /home/gushui/workspaces/newpscworkspace/PscNew/dest/psc.jar 执行 (根据自己jar的路径设置)这时会自动执行Main-Class的main函数

6.上面执行的命令可能说某个jar中的类没有找到。是因为没有设置自己写的工程的lib里那些jar的classpath的原因。很naive的解决方法:我把工程用到的包放到$HADOOP_HOME/lib里了,因为执行bin/hadoop的命令的时候会把lib里的这些jar包设置到classpath里。ok,再执行一下命令,ok pass。当然你也可以自己手动把这些jar包设置到classpath里.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值