本文介绍了如何创建不覆盖META-INF / services下条目的fat jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 插件{
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()
}

依赖{
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

}
}

任务fatJar(类型:Jar){
manifest {
attributes'Implementation-Title :'Gradle Jar文件示例',
'Implementation-Version':version,
'Main-Class':mainClassName
}
baseName = project.name +'-all'
from {(configurations.compile - configurations.provided).collect

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

}
{
不包括META-INF / *。SF
不包括META-INF / * .DSA
不包含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());




$ b当我按照预期从IDE运行主方法时获得以下输出:

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

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

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

我发现当创建fat jar时, META-INF /服务合并为所有的依赖关系,因此我失去了我的代码中需要进一步使用的 YarnClientProtocolProvider 的声明。
$ b >在 hadoop-mapreduce-client-jobclient.jar中声明了> YarnClientProtocolProvider 声明了LocalClientProtocolProvider b
$ b

hadoop-mapreduce-client-common.jar



有没有人知道如何创建一个不会在META-INF / services下合并条目的胖jar ?!

解决方案

这应该做的工作

  shadowJar {
mergeServiceFiles()
}


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?!

解决方案

This should do the work

shadowJar {
    mergeServiceFiles()
}

这篇关于如何创建不覆盖META-INF / services下条目的fat jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:42