在尝试创建胖jar时,shadowJar会遗漏很多依赖项 . 我从工作的pom.xml创建了一个build.gradle文件 . Maven制造了一个没有问题的胖 jar . 阅读Gradle有关构建胖 jar 的信息,shadowJar插件似乎是推荐的方法 . 但是,我的Gradle fat jar出现以下运行时错误:

Jul 19, 2018 2:35:10 PM restclients.RestStatus2Streams writeToStream
SEVERE: Error occurred while instantiating com.me.streams.impl.producer.MarlinProducerV10.
==> java.lang.NoClassDefFoundError: org/apache/hadoop/fs/PathId.

显然,我的远方 jar 里缺少一些东西 . 果然,org / apache / hadoop / fs / PathId和依赖项的整个目录都丢失了 . 虽然pom.xml没有明确地包含'org.apache.hadoop:hadoop-common'依赖项,但我明确地将该工件添加为我的build.gradle文件的依赖项,但仍然没有任何乐趣 .

回答几个问题:

  • 存储库中是否有hadoop-common工件?是 .

  • 是存储库中工件的命名版本吗?是

  • 类PathId是工件的's in the repository? A ' jar xf'表示的工件,"Yes."

由于Maven胖 jar 工作得很好并且不受干扰,Maven胖 jar 显示了PathId类(以及Gradle胖 jar 中缺少的其他类),那么我的build.gradle文件和/或shadowJar插件和/或Gradle有问题 .

build.gradle 文件:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'com.github.johnrengelman.shadow'

group = 'com.me.ps'
version = me.version

sourceCompatibility = 1.8
targetCompatibility = 1.8

defaultTasks 'shadowJar'

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.3'
    }
}

repositories {
     [...]
}

dependencies {
    // Explicityly added to in hopes of being included in the fat jar
    compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.7.0-me-1803'

    // dependencies listed in the pom.xml
    compile group: 'com.me.ojai', name: 'me-ojai-driver', version: '6.0.1-me'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.6.5'
    compile group: 'org.apache.kafka', name: 'connect-json', version: '1.0.1-me-1803-streams-6.0.1'
    compile group: 'org.apache.spark', name: 'spark-core_2.11', version: '2.2.1'
    compile group: 'org.apache.spark', name: 'spark-streaming_2.11', version: '2.2.1-me-1803'
    compile group: 'org.apache.spark', name: 'spark-streaming-kafka-0-10_2.11', version: '2.2.1-me-1803'
    compile group: 'org.apache.kafka', name: 'kafka-clients', version: '1.0.1-me-1803-streams-6.0.1'

    // dependencies not in the pom.xml, but added to make the gradle build work
    compile group: 'xerces', name: 'xercesImpl', version: '2.11.0'
    compile group: 'org.json', name: 'json', version: '20171018'
    compile group: 'com.google.guava', name: 'guava', version: '19.0'

}

shadowJar {
    zip64     true
    baseName 'me-edge'
}

什么是我的build.gradle文件出错?

  • Gradle 4.9

  • 操作系统:Linux 3.10.0-514.el7.x86_64 amd64

  • JVM:1.8.0_151(Oracle Corporation 25.151-b12)