groovy hadoop jar with gradle - package not exist error

青春壹個敷衍的年華 提交于 2019-12-10 23:59:37

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/36039514/groovy-hadoop-jar-with-gradle-package-not-exist-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!