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

备忘:生成自己的build.xml,jar包
参考:
http://www.roseindia.net/tutorials/ant/Howtogeneratebuildfile.shtml 主要

http://techtracer.com/2007/04/16/the-great-ant-tutorial-a-great-jump-start 参照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="jar" name="PscNew">
    <property environment="env"/>
    <property name="target" value="1.6"/>
    <property name="source" value="1.6"/>
    <property name="dir.src" value="src"/>
    <property name="dir.build" value="build"/>
    <property name="dir.dest" value="dest"/>


    <path id="PscNew.classpath">
        <pathelement location="../../../src/hadoop-0.20.2/lib/junit-3.8.1.jar"/>
       ...(中间还有很多)
        <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}">
            <classpath refid="PscNew.classpath"/>
        </javac>
    </target>

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

        </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里.