groovy hadoop jar with gradle - package not exist

2019-07-22 03:04发布

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条回答
啃猪蹄的小仙女
2楼-- · 2019-07-22 04:03

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'
    }
}
查看更多
登录 后发表回答