简体   繁体   English

使用 Gradle 将提供的依赖项添加到测试类路径

[英]Add provided dependency to test classpath using Gradle

I've provided dependency scope configured like below.provided如下配置的依赖项 scope。 My problem is, the provided dependencies are not visible during runtime in tests.我的问题是,提供的依赖项在测试运行时不可见。 How can I configure this to keep the dependencies provided but available on the test classpath?如何配置它以保持provided但在测试类路径上可用的依赖项?

apply plugin: 'java'

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    provided 'com.google.guava:guava:18.0'
    provided 'org.apache.commons:commons-lang3:3.3.2'

    // Tests
    testCompile 'junit:junit:4.11'
    testCompile 'org.assertj:assertj-core:1.7.0'

    // Additional test compile dependencies
    testCompile 'joda-time:joda-time:2.2'
}

One solution is to add the dependency like the joda-time library with testCompile scope, but I don't want to duplicate any entries.一种解决方案是使用testCompile scope 添加依赖项,例如 joda-time 库,但我不想复制任何条目。 I'm sure it can be achieved with proper configuration.我相信它可以通过适当的配置来实现。

Two ways to do this. 有两种方法可以做到这一点。 First, have the testRuntime configuration extend from provided . 首先,将testRuntime配置从provided扩展。

configurations {
    provided
    testRuntime.extendsFrom(provided)
}

Second, you could add the provided configuration to the classpath of your test task. 其次,您可以将provided配置添加到test任务的类路径中。

test {
    classpath += configurations.provided
}

Fixed with one additional line in configurations . configurations固定了另一条线。 Don't know if it's the best and a proper solution but works as intended. 不知道这是否是最佳和适当的解决方案,但按预期工作。

configurations {
    provided
    testCompile.extendsFrom(provided)
}

my case我的情况

withType<Jar> {
    enabled = true
    isZip64 = true
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    archiveFileName.set("$project.jar")

    from(sourceSets.main.get().output)
    dependsOn(configurations.compileClasspath)
    from({
        configurations.compileClasspath.get().filter {
            it.name.endsWith("jar")
        }.map { zipTree(it) }
    }) {
        exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")
    }
}

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

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