与grpc-java的bindableService问题

时间:2017-03-01 19:39:31

标签: java gradle grpc grpc-java

我正在尝试使用grpc-java v1.1.2(下面的build.gradle部分)但是当我尝试为示例应用程序运行fat jar时,它会抛出下面给出的异常。编译应用程序时,我没有看到任何问题。

build.gradle parts:

    apply plugin: 'com.google.protobuf'

    buildscript {
        repositories {
            mavenCentral()
            mavenLocal()
        }
        dependencies {
            // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
            // gradle versions
            classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
        }
    }


    def grpcVersion = '1.1.2'   //''1.2.0-SNAPSHOT' // CURRENT_GRPC_VERSION

    dependencies {
        compile "io.grpc:grpc-netty:${grpcVersion}"
        compile "io.grpc:grpc-protobuf:${grpcVersion}"
        compile "io.grpc:grpc-stub:${grpcVersion}"
        compile 'me.grapebaba:hyperledger-java-client:0.1.3'
}


    protobuf {
        protoc {
            artifact = 'com.google.protobuf:protoc:3.2.0'
        }
        plugins {
            grpc {
                artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
            }
        }
        generateProtoTasks {
            all()*.plugins {
                grpc {
                    // To generate deprecated interfaces and static bindService method,
                    // turn the enable_deprecated option to true below:
                    option 'enable_deprecated=false'
                }
            }
        }
    }

    idea {
        module {
            // Not using generatedSourceDirs because of
            // https://discuss.gradle.org/t/support-for-intellij-2016/15294/8
            sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
            sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
        }
    }


    jar {
        manifest {
               attributes(
                    'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                    'Main-Class': 'com.test.io.grpc.HelloWorldServer'
            )
        }
    }



    task helloWorldServer(type: CreateStartScripts) {
        mainClassName = 'io.grpc.mgcs.HelloWorldServer'
        applicationName = 'hello-world-server'
        outputDir = new File(project.buildDir, 'tmp')
        classpath = jar.outputs.files + project.configurations.runtime
    }

    task helloWorldClient(type: CreateStartScripts) {
        mainClassName = 'io.grpc.mgcs.HelloWorldClient'
            applicationName = 'hello-world-client'
            outputDir = new File(project.buildDir, 'tmp')
            classpath = jar.outputs.files + project.configurations.runtime
    }

    applicationDistribution.into('bin') {
        from(helloWorldServer)
        from(helloWorldClient)
        fileMode = 0755
    }

例外

Caused by: java.lang.ClassNotFoundException: io.grpc.BindableService
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

2 个答案:

答案 0 :(得分:0)

我通过使用shadow插件来创建胖罐来解决这个问题。

    buildscript {
        repositories {
            mavenCentral()
            mavenLocal()
        }
        dependencies {
            classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
            classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
            classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
        }
    }

    plugins {
        id 'java'
        id 'application'
        id 'idea'
        id 'com.github.johnrengelman.shadow' version '1.2.4'
        id "net.ltgt.errorprone" version '0.0.9'
    }


    shadowJar {
        baseName = 'shadow'
        classifier = null
        version = null
    }

jar {
    manifest {
        attributes(
                'Class-Path': configurations.runtime.files.collect {"$it.name"}.join(' '),
                'Main-Class': 'com.abc.test'
        )
    }
}

运行以下命令:

gradle shadowJar

shadowJar文件在build / libs目录中创建,该目录可以运行:

java -jar shadowJar

答案 1 :(得分:0)

对我来说,问题是jar不包含依赖的lib类。我必须进行此更改才能解决它:

jar {
    ...
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

这只是创建胖子罐的另一种方法:)