簡體   English   中英

gradle 創建 jar 不適用於“實現”依賴項

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

我是 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里面的grpc依賴。 我嘗試直接運行生成的文件

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

它按預期工作。 為了解決 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'
}

現在一切正常,依賴關系在 jar 中,我可以運行 jar。 但是我不確定我是否應該在我的依賴項中使用api ,因為官方示例沒有使用它? 也許我沒有正確生成 jar,它可以通過依賴項實現生成,任何幫助或指針都非常感謝。

問題是您使用的jar任務修改沒有利用新的依賴配置。

而不是從compile收集依賴項,你真的想從你的 uber runtimeClasspath中的 runtimeClasspath 收集依賴項。 畢竟,為了運行,它需要在implementation中聲明的所有依賴項,但也需要runtimeOnly 請參閱文檔以更好地了解這些配置之間的關系。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM