简体   繁体   English

使用 Gradle 和 Groovy/Spock 测试 Java 6 库或应用程序

[英]Using Gradle and Groovy/Spock to test a Java 6 lib or app

I have a Java library, built by Gradle 2.4, that will be used by some apps that are Java 6, some that are Java 7, some that are Java 8, and some that are Groovy 2.x.我有一个由 Gradle 2.4 构建的 Java 库,它将被一些 Java 6 的应用程序、一些 Java 7 的应用程序、一些 Java 8 的应用程序和一些 Groovy 2.x 的应用程序使用。 As such, to be as backwards-compatible as possible, I am writing the lib to have both a sourceCompatibility as well as a targetCompatibility of 1.6 :因此,为了尽可能向后兼容,我正在编写 lib 以同时具有sourceCompatibilitytargetCompatibility 1.6

build.gradle
============
apply plugin: 'java'
apply plugin: 'groovy'

sourceCompatibility = '1.6'
targetCompatibility = '1.6'

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile (
        'org.slf4j:slf4j-api:1.7.12'
    )

    testCompile (
        'org.codehaus.groovy:groovy-all:2.4.6'
        ,'org.spockframework:spock-core:1.0-groovy-2.4'
    )
}

However, there's no reason why I shouldn't be able to write my unit tests in Groovy/Spock.但是,我没有理由不能在 Groovy/Spock 中编写单元测试。 So long as Groovy is not part of the main/compile/runtime classpath, then I should be free to write my tests in any JVM language I please!只要 Groovy 不是主/编译/运行时类路径的一部分,那么我就可以自由地用任何我喜欢的 JVM 语言编写我的测试! And I choose Groovy/Spock!我选择 Groovy/Spock!

The problem is, I've never done something like this before.问题是,我以前从未做过这样的事情。 I think I need to add Groovy-All and Spock to the testCompile classpath, however I'm not sure if I need to apply the Groovy plugin or not, as well as any other configs.我需要将 Groovy-All 和 Spock 添加到testCompile类路径中,但是我不确定是否需要应用 Groovy 插件以及任何其他配置。

Again:再次:

  • The main compile/runtime classpath must be Groovy-free and must be compatible with Java 1.6主编译/运行时类路径必须是 Groovy-free 并且必须与 Java 1.6 兼容
  • The test compile/runtime classpath can include anything I like测试编译/运行时类路径可以包含我喜欢的任何内容

Any ideas as to what specific changes I need to make above?关于我需要在上面进行哪些具体更改的任何想法?

If you want Gradle to compile any Groovy, be that main source or just test source, you will want to apply the groovy gradle plugin.如果您希望 Gradle 编译任何Groovy,无论是主源还是测试源,您都需要应用groovy gradle 插件。 That said, as long as you are not building a fat jar (that is, a JAR library that includes dependent libraries within itself so it can run as a standalone application), then you should not have any ties to Groovy on the runtime classpath.也就是说,只要您不构建胖 jar(即,一个 JAR 库本身包含依赖库,因此它可以作为独立应用程序运行),那么您就不应该在运行时类路径上与 Groovy 有任何联系。 You are correct that you will want to add Spock/Groovy to your testCompile dependencies, and as long as you keep them scoped to testCompile, even creating a fat JAR shouldn't include them in your main compile classpath, in the final built artifact, or required to be on the classpath at runtime.您是正确的,您希望将 Spock/Groovy 添加到您的 testCompile 依赖项中,并且只要您将它们的范围限定为 testCompile,即使创建一个胖 JAR 也不应该将它们包含在您的主编译类路径中,在最终构建的工件中,或需要在运行时位于类路径上。

On a related note, in Gradle you also have access to modify the configurations of any and all dependencies.在相关说明中,在 Gradle 中,您还可以修改任何和所有依赖项的配置。 Have a look at the Gradle Dependency Management docs for some more information on that.查看 Gradle Dependency Management文档以获取更多信息。

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

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