简体   繁体   English

gradle 创建 jar 不适用于“实现”依赖项

[英]gradle creating jar does not work with 'implementation' depedencies

I am new to gradle and trying to generate a jar from a simple hello world java grpc and below is my build.gradle我是 gradle 的新手,并试图从一个简单的 hello world java grpc和以下生成 jar 是我的 ZC19796236023DCEAZ33

plugins {
    id 'application'
    id 'com.google.protobuf' version '0.8.12'
    id 'idea'
    id 'java'
}

version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    maven { // The google mirror is less flaky than mavenCentral()
        url "https://maven-central.storage-download.googleapis.com/repos/central/data/" }
    mavenCentral()
}

dependencies {
    implementation 'io.grpc:grpc-netty-shaded:1.29.0'
    implementation 'io.grpc:grpc-protobuf:1.29.0'
    implementation 'io.grpc:grpc-stub:1.29.0'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}


protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.11.0"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.29.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

sourceSets {
    main {
        java {
            srcDirs 'build/generated/source/proto/main/grpc'
            srcDirs 'build/generated/source/proto/main/java'
        }
    }
}

startScripts.enabled = false

task helloWorldServer(type: CreateStartScripts) {
    mainClassName = 'com.javagrpc.HelloWorldServer'
    applicationName = 'hello-world-server'
    outputDir = new File(project.buildDir, 'tmp')
    classpath = startScripts.classpath
}

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

distZip.shouldRunAfter(build)

jar {
    manifest {
        attributes 'Main-Class': 'com.examples.javagrpc.HelloWorldServer',
        'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

I started running into issue when I run the task 'gradle jar' it builds a jar inside build/libs when I run the jar its fails with java.lang.NoClassDefFoundError: io/grpc/BindableService I unpacked the jar and didn't find the grpc dependencies inside it. I started running into issue when I run the task 'gradle jar' it builds a jar inside build/libs when I run the jar its fails with java.lang.NoClassDefFoundError: io/grpc/BindableService I unpacked the jar and didn't find里面的grpc依赖。 I tried running the generated files directly我尝试直接运行生成的文件

./build/install/java-grpc/bin/hello-world-server

and it works as expected.它按预期工作。 To resolve the jar issue, I decided to change the above dependencies from implementation to api as below.为了解决 jar 问题,我决定将上述依赖项从implementation更改为api ,如下所示。

dependencies {
    api 'io.grpc:grpc-netty-shaded:1.29.0'
    api 'io.grpc:grpc-protobuf:1.29.0'
    api 'io.grpc:grpc-stub:1.29.0'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Now everything works as expected the dependencies are inside the jar and I can run the jar.现在一切正常,依赖关系在 jar 中,我可以运行 jar。 But I am not sure whether I shall be using api or not in my dependencies since the official example does not uses it?但是我不确定我是否应该在我的依赖项中使用api ,因为官方示例没有使用它? Maybe I am not correctly generating the jar and it can be generated just with dependencies implementation any help or pointers is highly appreciated.也许我没有正确生成 jar,它可以通过依赖项实现生成,任何帮助或指针都非常感谢。

The problem is that the jar task modification you use does not take advantage of the new dependency configurations.问题是您使用的jar任务修改没有利用新的依赖配置。

Instead of collection the dependencies from compile , you really want to collect the dependencies from runtimeClasspath in your uber JAR.而不是从compile收集依赖项,你真的想从你的 uber runtimeClasspath中的 runtimeClasspath 收集依赖项。 After all, in order to run, it will need all dependencies declared in implementation but runtimeOnly as well.毕竟,为了运行,它需要在implementation中声明的所有依赖项,但也需要runtimeOnly See the documentation to better understand the relationship between these configurations.请参阅文档以更好地了解这些配置之间的关系。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM