how to create fat jar which does not overwrite entries under META-INF/services

喜欢而已 提交于 2019-12-07 21:57:00

问题


I have the following gradle build config:

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
}

The main method that I have is just the following piece of code:

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

When I run the main method from IDE as expected I get the following output:

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

But when I run the gradle fat jar task and I create the fat jar, after running the main method using (java -jar) through terminal I just get:

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

I found that when fat jar is created, the entries under META-INF/services are merged for all dependencies and hence I lose the declaration for YarnClientProtocolProvider which I need further in my code.

YarnClientProtocolProvider is declared in hadoop-mapreduce-client-jobclient.jar

LocalClientProtocolProvider is declared in hadoop-mapreduce-client-common.jar

Does any body know how to create a fat jar which does not merges entries under META-INF/services?!


回答1:


This should do the work

shadowJar {
    mergeServiceFiles() 
}


来源:https://stackoverflow.com/questions/44595145/how-to-create-fat-jar-which-does-not-overwrite-entries-under-meta-inf-services

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