且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

groovy hadoop jar与gradle - 包不存在错误

更新时间:2023-01-15 19:24:18

buildSrc 而不是 main 中的代码中,这意味着依赖关系是构建脚本依赖关系部分所必需的。



相反,您将依赖关系应用于适用于 main 目录中的源的通用依赖关系部分。 / p>

添加到你的build.gradle:

  buildscript {
存储库{
mavenCentral()
}

依赖关系{
compile'org.codehaus.groovy:groovy-all:2.0.0'
compile 'org.apache.hadoop:hadoop-common:2.7.1'
}
}


I was trying to create a groovy jar with Gradle 2.12 . The groovy file has import statements as below and I put this file in src/main/groovy

The first two import are java files, which inturn have org.apache.hadoop imports statements. I put these two files in 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

And below is my build.gradle file (using this)

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
    }
}

I have tried to compile with below also, by putting them in dependencies

     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'

but I keep on receiving this error:

/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;

how can I compile and create a jar with all those import statements in groovy and java files?

EDIT to include 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'
    }
}

folder structure :

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

The import statements are in code that is in buildSrc and not main, which means that the dependencies are required by the buildscript dependencies section.

Instead you are applying dependencies to the general dependencies section which applies to source which lives in the main directory.

Add to your build.gradle:

buildscript{
    repositories {
        mavenCentral()
    }

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