Linux下Hadoop Eclipse插件编译安装

Linux下Hadoop Eclipse插件编译安装

刚刚开始研究Hadoop,在写Map Reduce程序的时候觉得用vim不够用,于是想在Eclipse下面配置一下相关的环境。
按照Hadoop in Action的说法,本来想用Hadoop Studio,但是发现现在只有30天的试用版,于是找了一下,发现Hadoop本身有带Eclipse的插件,于是觉得可以用一下。 不过又发现Hadoop的Eclipse插件现在没有二进制版直接提供,只能自己编译。不过要注意,一定要下载Hadoop的src包,不然是不带源代码的。
编译部署中间碰到了一点问题,记录下来。


首先,讲一下系统环境,Linux的发行版是Linux Mint 14,Hadoop版本是1.1.2。
我的hadoop安装在/home/kinuxroot/apps/hadoop-1.1.2下面(hadoop的根路径),所以源代码的目录是hadoop根路径下的src/contrib/eclipse-plugin。
Eclipse安装路径是/home/kinuxroot/apps/eclipse。

接下来我们来讲一下具体如何编译。
我们进入hadoop根路径下的src/contrib/eclipse-plugin,然后修改一下build.xml。
具体做如下修改:


1.先要加入eclipse的设定和hadoop的版本设定,我的eclipse安装在/home/kinuxroot/apps/eclipse下,所以我们要修改为:

<property name="eclipse.home" location="/home/kinuxroot/apps/eclipse"/>
<property name="version" value="1.1.2"/>

这一步,location中的路径请大家根据各自的实际路径进行修改。


2.我们需要引用hadoop的一些包,但是默认的classpath没有这些包(我们没有从头编译)。所以需要修改classpath
定位<path id="classpath">,加入:

<fileset dir="${hadoop.root}">
    <include name="**/*.jar" />
</fileset>

 

3.代码中使用了一些遗留功能,所以我们要修改deprecation的设定。
打开hadoop根路径下面的src/contrib/build-contrib.xml,定位

<property name="javac.deprecation" value="off"/>

然后修改成

<property name="javac.deprecation" value="on"/>

 

4.修改includeantruntime设置。定位compile的target,修改javac的设置,加入一个选项
     includeantruntime="on"
也就是将javac修改成

<javac
     encoding="${build.encoding}"
     srcdir="${src.dir}"
     includes="**/*.java"
     destdir="${build.classes}"
     debug="${javac.debug}"
     deprecation="${javac.deprecation}"
     includeantruntime="on">
     <classpath refid="classpath"/>
</javac>

 

5.jar打包的时候需要hadoop的一些jar文件,但是我们没有编译生成它,所以我们需要修改一下jar这个target。

另外,有几个jar是我们需要用到,而build.xml里面没有自动包含的,如果不包含它们,Eclipse连接Hadoop会出现failure to login错误,其实就是找不到类
找到

<copy file="${hadoop.root}/build/hadoop-core-${version}.jar" tofile="${build.dir}/lib/hadoop-core.jar" verbose="true"/>
<copy file="${hadoop.root}/build/ivy/lib/Hadoop/common/commons-cli-${commons-cli.version}.jar"  todir="${build.dir}/lib" verbose="true"/>

我们修改成

<copy file="${hadoop.root}/hadoop-core-${version}.jar" tofile="${build.dir}/lib/hadoop-core.jar" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-cli-${commons-cli.version}.jar"  tofile="${build.dir}/lib/commons-cli.jar" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-configuration-1.6.jar"  tofile="${build.dir}/lib/commons-configuration.jar" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-httpclient-3.0.1.jar"  tofile="${build.dir}/lib/commons-httpclient.jar" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-lang-2.4.jar"  tofile="${build.dir}/lib/commons-lang.jar" verbose="true"/>
<copy file="${hadoop.root}/lib/jackson-core-asl-1.8.8.jar"  tofile="${build.dir}/lib/jackson-core-asl.jar" verbose="true"/>
<copy file="${hadoop.root}/lib/jackson-mapper-asl-1.8.8.jar"  tofile="${build.dir}/lib/jackson-mapper-asl.jar" verbose="true"/>

6.但是这样,我们的jar文件还是不会自动部署到eclipse中,你可以手动复制,但是我们希望ant帮我们自动部署进去。
我们新建一个target,用来部署:

<target name="deploy" depends="jar" unless="skip.contrib">
<copy file="${build.dir}/hadoop-${name}-${version}.jar"  todir="${eclipse.home}/plugins" verbose="true"/>
</target>

然后修改project的默认target,也就是将project修改成:

<project default="deploy" name="eclipse-plugin">

7.接下来一步我们要修改Hadoop根目录下的src/contrib/eclipse-plugin/META-INFO/MANIFEST.MF,修改这个jar的classpath。

找到这个文件的Bundle-ClassPath这一行,然后,修改成

Bundle-ClassPath: classes/,lib/commons-cli.jar,lib/commons-httpclient.jar,lib/hadoop-core.jar,lib/jackson-mapper-asl.jar,lib/commons-configuration.jar,lib/commons-lang.jar,lib/jackson-core-asl.jar

8.执行ant,代码就会被编译,插件会被自动安装到eclipse的plugins目录中,打开eclipse就可以使用了(如果没有安装ant,
请去apache观望下载ant的二进制编译版)。另外编译时会自动联网下载需要的包,所以请保证网络通畅。

最后,我把我的build.xml贴在下面,大家可以参考:

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

<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<project default="deploy" name="eclipse-plugin">

  <import file="../build-contrib.xml"/>

  <property name="eclipse.home" location="/home/kinuxroot/apps/eclipse"/>
  <property name="version" value="1.1.2"/>

  <path id="eclipse-sdk-jars">
    <fileset dir="${eclipse.home}/plugins/">
      <include name="org.eclipse.ui*.jar"/>
      <include name="org.eclipse.jdt*.jar"/>
      <include name="org.eclipse.core*.jar"/>
      <include name="org.eclipse.equinox*.jar"/>
      <include name="org.eclipse.debug*.jar"/>
      <include name="org.eclipse.osgi*.jar"/>
      <include name="org.eclipse.swt*.jar"/>
      <include name="org.eclipse.jface*.jar"/>

      <include name="org.eclipse.team.cvs.ssh2*.jar"/>
      <include name="com.jcraft.jsch*.jar"/>
    </fileset> 
  </path>

  <!-- Override classpath to include Eclipse SDK jars -->
  <path id="classpath">
    <pathelement location="${build.classes}"/>
    <pathelement location="${hadoop.root}/build/classes"/>
    <fileset dir="${hadoop.root}">
        <include name="**/*.jar" />
    </fileset>
    <path refid="eclipse-sdk-jars"/>
  </path>

  <!-- Skip building if eclipse.home is unset. -->
  <target name="check-contrib" unless="eclipse.home">
    <property name="skip.contrib" value="yes"/>
    <echo message="eclipse.home unset: skipping eclipse plugin"/>
  </target>

 <target name="compile" depends="init, ivy-retrieve-common" unless="skip.contrib">
    <echo message="contrib: ${name}"/>
    <javac
     encoding="${build.encoding}"
     srcdir="${src.dir}"
     includes="**/*.java"
     destdir="${build.classes}"
     debug="${javac.debug}"
     deprecation="${javac.deprecation}"
     includeantruntime="on">
     <classpath refid="classpath"/>
    </javac>
  </target>

  <!-- Override jar target to specify manifest -->
  <target name="jar" depends="compile" unless="skip.contrib">
    <mkdir dir="${build.dir}/lib"/>
    <copy file="${hadoop.root}/hadoop-core-${version}.jar" tofile="${build.dir}/lib/hadoop-core.jar" verbose="true"/>
    <copy file="${hadoop.root}/lib/commons-cli-${commons-cli.version}.jar"  tofile="${build.dir}/lib/commons-cli.jar" verbose="true"/>
    <copy file="${hadoop.root}/lib/commons-configuration-1.6.jar"  tofile="${build.dir}/lib/commons-configuration.jar" verbose="true"/>
    <copy file="${hadoop.root}/lib/commons-httpclient-3.0.1.jar"  tofile="${build.dir}/lib/commons-httpclient.jar" verbose="true"/>
    <copy file="${hadoop.root}/lib/commons-lang-2.4.jar"  tofile="${build.dir}/lib/commons-lang.jar" verbose="true"/>
    <copy file="${hadoop.root}/lib/jackson-core-asl-1.8.8.jar"  tofile="${build.dir}/lib/jackson-core-asl.jar" verbose="true"/>
    <copy file="${hadoop.root}/lib/jackson-mapper-asl-1.8.8.jar"  tofile="${build.dir}/lib/jackson-mapper-asl.jar" verbose="true"/>
    <echo message="${build.dir}"/>
    <echo message="${root}"/>
    <jar
      jarfile="${build.dir}/hadoop-${name}-${version}.jar"
      manifest="${root}/META-INF/MANIFEST.MF">
      <fileset dir="${build.dir}" includes="classes/ lib/"/>
      <fileset dir="${root}" includes="resources/ plugin.xml"/>
    </jar>
  </target>

  <target name="deploy" depends="jar" unless="skip.contrib">
    <copy file="${build.dir}/hadoop-${name}-${version}.jar"  todir="${eclipse.home}/plugins" verbose="true"/>
  </target>

</project>

 

posted on 2013-05-06 17:34  kinuxroot  阅读(8281)  评论(4编辑  收藏  举报