【问题标题】:Create a Groovy executable jar with Spock test set as to be executed创建一个带有 Spock 测试集的 Groovy 可执行 jar 以执行
【发布时间】:2015-07-21 14:56:45
【问题描述】:

我想用两个 groovy 文件创建 jar,AppLogic.groovy 由两个少数 groovy 类和另一个文件组成,AppSpec 有 Spock 测试套件,我想执行这个 Spock 类(设置为可执行文件)。如何创建具有所有依赖项的此类 jar?我在这里找到了 jUnit 类似的东西:how to export (JUnit) test suite as executable jar 但无法适应我的需要。

我使用 gradle 进行构建,这是我的 build.gradle 文件:

group 'someGroup'
version '1.0'

apply plugin: 'groovy'
apply plugin: 'java'
apply plugin:'application'

sourceCompatibility = 1.7

repositories {

//some repos here

maven { url "http://repo.maven.apache.org/maven2" }
}

dependencies {
//some dependencies here
}

我四处浏览并找到了 SpockRuntime,但我不知道是否以及如何使用它来实现我的目标。

【问题讨论】:

    标签: groovy jar gradle executable-jar spock


    【解决方案1】:

    获胜者是:

    static void main(String[] args) {
        EmbeddedSpecRunner embeddedSpecRunner = new EmbeddedSpecRunner()
        embeddedSpecRunner.runClass(MySpec)
    }
    

    【讨论】:

    • 任何适用于 JUnit + Groovy 的解决方案都应该适用于 Spock。
    • 其实没错,但是将 JUnit 解决方案和 Spock 混合在一起在我看来并没有这个优雅,毕竟它是额外的依赖。
    • 使用EmbeddedSpecRunner 可以满足您的需要,但无论如何Spock 都需要JUnit。
    • 我也有类似的情况。这是否意味着我必须在 groovy 规范文件的源代码中添加 EmbeddedSpecRunner(如上面列出的获胜者)?我有许多 XXXXTestSpec.groovy 文件,所以为每个类添加 embeddedSpecRunner.runClass(XXXXTestSpec) ?
    • 见我上面的回答。它是可靠的,不需要使用内部 spock 类 EmbeddedSpecRunner。
    【解决方案2】:

    我不建议使用 spock 实现中的 EmbeddedSpecRunner,如接受的答案中所述。

    这是我发现在 gradle 4.9 中可靠工作的方法。基本方法是使用:

    1. 用于创建单个 tar 文件的 gradle 应用程序插件,其中包含所有 testRuntimeClasspath 依赖项和运行 spock 测试的 shell 脚本
    2. gradle maven-publish 插件,用于将 tar 文件作为工件发布到您的 maven 存储库(在我的例子中是 nexus)

    build.gradle 文件如下所示:

    apply plugin: 'java'
    apply plugin: 'groovy'
    apply plugin: 'maven-publish'
    apply plugin: 'application'
    
    mainClassName = 'org.junit.runner.JUnitCore' // The junit 4 test runner class
    applicationName = 'run-tests-cli'  // Feel free to change
    
    repositories {
        ...
    }
    
    dependencies {
        ...
    
        testImplementation "org.codehaus.groovy:groovy-all:${groovyVersion}"
        testImplementation "org.spockframework:spock-core:${spockVersion}"
    
    }
    
    // Package compiled spock / junit tests to <artifact>-test-<version>.jar
    task testJar(type: Jar) {
        classifier = 'tests'
        from sourceSets.test.output.classesDirs
    }
    
    // Copy all testRuntimeClasspath dependencies to libs folder
    task copyToLibs(type: Copy) {
        from configurations.testRuntimeClasspath
        into "$buildDir/libs"
    }
    
    // Make sure test jar is copied
    copyToLibs.dependsOn('testJar')
    
    // Make sure platform-specific shell scripts are created after copyToLibs
    startScripts.dependsOn(copyToLibs)
    
    // Configure what goes into the tar / zip distribution file created by gradle distribution plugin assembleDist task
    distributions {
        main {
            contents {
                // Include test jar
                from(testJar) {
                    into "lib"
                }
                // Include all dependencies from testRuntimeClasspath
                from(copyToLibs) {
                    into "lib"
                }
            }
        }
    }
    
    startScripts {
        // Ensure ethat all testRuntimeClasspath dependencies are in classpath used by shell scripts
        classpath = project.tasks['testJar'].outputs.files + project.configurations.testRuntimeClasspath
    }
    
    publishing {
        repositories {
            maven {
                def releasesRepoUrl = "https://nexus.yourcompany.com/repository/maven-releases/"
                def snapshotsRepoUrl = "https://nexus.yourcompany.com/repository/maven-snapshots/"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                credentials {
                    username = rootProject.getProperty('NEXUS_USERNAME')
                    password = rootProject.getProperty('NEXUS_PASSWORD')
                }
            }
        }
        publications {
            maven(MavenPublication) {
                groupId = 'com.yourgroupId'
                version = "${rootProject.getVersion()}"
            }
            TestJar(MavenPublication) {
                artifact(testJar)
            }
            RunTestsCliTar(MavenPublication) {
                artifact(distTar)
                artifactId "${applicationName}"
            }
        }
    }
    

    现在您可以执行以下操作:

    • 在不运行测试任务的情况下构建项目(包括 tar 文件):gradle -x test clean build
    • 要发布项目生成的工件(包括 tar 文件到 maven repo - 在我的例子中是 nexus):gradlew -x test publish。请注意,您需要提供凭据才能将工件上传到 repo。在 ~/.gradle/gradle.properties 中定义它们(在我的示例中为 NEXUS_USERNAME,NEXUS_PASSWORD)或通过 gradle 命令行上的 -P 选项指定它们是一种很好的做法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      相关资源
      最近更新 更多