使用Spock测试集创建一个Groovy可执行jar,以便执行

时间:2015-07-21 14:56:46

标签: groovy jar gradle executable-jar spock

我想用两个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,但我不知道是否以及如何使用它来实现我的目标。

2 个答案:

答案 0 :(得分:4)

获胜者是:

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

答案 1 :(得分:0)

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

这是我发现gradle 4.9可以可靠地工作的原因。基本方法是使用:

  1. gradle应用程序插件可创建具有所有testRuntimeClasspath依赖项和运行spock测试的shell脚本的单个tarfile
  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。注意,您将需要提供凭据才能上传工件以回购。最好在〜/ .gradle / gradle.properties中定义它们(在我的示例中为NEXUS_USERNAME和NEXUS_PASSWORD),或通过gradle命令行上的-P选项指定它们。