groovy hadoop jar with gradle-package not exist错误

6mzjoqzu  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(325)

我试图用gradle2.12创建一个groovy jar。groovy文件有如下import语句,我将此文件放入 src/main/groovy 前两个导入是java文件,inturn有 org.apache.hadoop 导入语句。我把这两个文件放进去了 src/main/java ```
import StartsWithCountMapper
import StartsWithCountReducer
import org.apache.hadoop.conf.Configured
import org.apache.hadoop.fs.Path
import org.apache.hadoop.io.IntWritable
import org.apache.hadoop.io.LongWritable
import org.apache.hadoop.io.Text
import org.apache.hadoop.mapreduce.Job
import org.apache.hadoop.mapreduce.Mapper
import org.apache.hadoop.mapreduce.Reducer
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat
import org.apache.hadoop.util.Tool
import org.apache.hadoop.util.ToolRunner

下面是我的build.gradle文件(使用这个)

apply plugin: 'groovy'
apply plugin: 'application'
version = '1.0'
mainClassName='CountGroovyJob'

repositories {
mavenCentral()
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.0.0'
compile 'org.apache.hadoop:hadoop-common:2.7.1'

}

task Createjar(type: Jar,dependsOn:[':compileJava',':compileGroovy']) {
from files(sourceSets.main.output.classesDir)
from configurations.runtime.asFileTree.files.collect { zipTree(it) }

manifest {
    attributes 'Main-Class': mainClassName
}

}

我也试着用下面的代码进行编译,把它们放在依赖项中

compile 'org.apache.hadoop:hadoop-client:2.0.0-mr1-cdh4.0.1'
compile 'org.apache.hadoop:hadoop-core:1.2.1'
compile 'org.apache.hadoop:hadoop-hdfs:2.7.1'

但我一直收到这个错误:

/Users/../../../gradle/buildSrc/src/main/java/StartsWithCountMapper.java:5: error: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.IntWritable;
^
/Users/../../../gradle/buildSrc/src/main/java/StartsWithCountMapper.java:6: error: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.LongWritable;
^
/Users/../../../gradle/buildSrc/src/main/java/StartsWithCountMapper.java:7: error: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.Text;

如何用groovy和java文件中的所有import语句编译和创建jar?
编辑以包含buildscrip{}

apply plugin: 'groovy'
apply plugin: 'application'
version = '1.0'
mainClassName='CountGroovyJob'

repositories {
mavenCentral()
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.0.0'
compile 'org.apache.hadoop:hadoop-common:2.7.1'

}

task Createjar(type: Jar,dependsOn:[':compileJava',':compileGroovy']) {
from files(sourceSets.main.output.classesDir)
from configurations.runtime.asFileTree.files.collect { zipTree(it) }

manifest {
    attributes 'Main-Class': mainClassName
}

}

buildscript{
repositories {
mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.0.0'
    compile 'org.apache.hadoop:hadoop-common:2.7.1'
}

}

文件夹结构:

grad-proj
|
+-- build.gradle
|
+-- src
|
+-- main
|
+-- groovy
|
+-- java

33qvvth1

33qvvth11#

import语句所用的代码 buildSrc 而不是 main ,这意味着buildscript依赖项部分需要依赖项。
相反,您将依赖项应用于常规依赖项部分,该部分应用于位于 main 目录。
添加到build.gradle:

buildscript{
    repositories {
        mavenCentral()
    }

    dependencies {
        compile 'org.codehaus.groovy:groovy-all:2.0.0'
        compile 'org.apache.hadoop:hadoop-common:2.7.1'
    }
}

相关问题