如何创建fatjar,它不会覆盖meta-inf/services下的条目

unguejic  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(666)

我有以下gradle构建配置:

plugins {
id 'com.github.johnrengelman.shadow' version '1.2.3'
}

group 'abc'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'application'
mainClassName = "abc.Driver"

repositories {
    mavenCentral()
}

dependencies {
    compile (group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.6.0')
}

sourceSets {
    main {
        java {
            srcDir './src'
        }
    }
}

jar {
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': mainClassName
        )
    }
}

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
                'Implementation-Version': version,
                'Main-Class': mainClassName
    }
    baseName = project.name + '-all'
    from { (configurations.compile - configurations.provided).collect

        {
            //println it.getName()
            it.isDirectory() ? it : zipTree(it)
        }

    }
    {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}

我的主要方法是以下代码:

public static void main(String[] args) {
    Iterable<ClientProtocolProvider> frameworkLoader =
            ServiceLoader.load(ClientProtocolProvider.class);
    for(ClientProtocolProvider cpp: frameworkLoader) {
        System.out.println(cpp.toString());
    }
}

当我按预期从ide运行main方法时,我得到以下输出:

org.apache.hadoop.mapred.YarnClientProtocolProvider@4783da3f
org.apache.hadoop.mapred.LocalClientProtocolProvider@300ffa5d

但是当我运行gradle fat jar任务并创建fat jar时,在通过终端运行main方法(java-jar)之后,我得到:

org.apache.hadoop.mapred.LocalClientProtocolProvider@7f31245a

我发现创建fatjar时,meta-inf/services下的条目被合并为所有依赖项,因此我丢失了yarclientprotocolprovider的声明,我需要在代码中进一步声明。
yarclientprotocolprovider在hadoop-mapreduce-client-jobclient.jar中声明
localclientprotocolprovider在hadoop-mapreduce-client-common.jar中声明
有人知道如何创建一个fat jar,它不会合并meta-inf/services下的条目吗?!

mctunoxg

mctunoxg1#

这样就可以了

shadowJar {
    mergeServiceFiles() 
}

相关问题