Gradle에 새 소스 세트를 어떻게 추가합니까?
Gradle 빌드 (버전 1.0)에 통합 테스트를 추가하고 싶습니다. webapp을 localhost에 배포해야하므로 내 일반 테스트와 별도로 실행해야합니다 (해당 webapp 테스트). 테스트는 내 기본 소스 세트에 정의 된 클래스를 사용할 수 있어야합니다. 어떻게해야합니까?
이것은 내가 알아내는 데 시간이 걸리고 온라인 리소스가 좋지 않았습니다. 그래서 내 솔루션을 문서화하고 싶었습니다.
이것은 기본 및 테스트 소스 세트 외에도 intTest 소스 세트가있는 간단한 gradle 빌드 스크립트입니다.
apply plugin: "java"
sourceSets {
// Note that just declaring this sourceset creates two configurations.
intTest {
java {
compileClasspath += main.output
runtimeClasspath += main.output
}
}
}
configurations {
intTestCompile.extendsFrom testCompile
intTestRuntime.extendsFrom testRuntime
}
task intTest(type:Test){
description = "Run integration tests (located in src/intTest/...)."
testClassesDir = project.sourceSets.intTest.output.classesDir
classpath = project.sourceSets.intTest.runtimeClasspath
}
을 사용하지 않고이를 달성 한 방법은 다음과 같습니다 configurations{ }
.
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_6
sourceSets {
integrationTest {
java {
srcDir 'src/integrationtest/java'
}
resources {
srcDir 'src/integrationtest/resources'
}
compileClasspath += sourceSets.main.runtimeClasspath
}
}
task integrationTest(type: Test) {
description = "Runs Integration Tests"
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath += sourceSets.integrationTest.runtimeClasspath
}
다음을 사용하여 테스트 : Gradle 1.4 및 Gradle 1.6
This was once written for Gradle 2.x / 3.x in 2016 and is far outdated!! Please have a look at the documented solutions in Gradle 4 and up
To sum up both old answers (get best and minimum viable of both worlds):
some warm words first:
first, we need to define the
sourceSet
:sourceSets { integrationTest }
next we expand the
sourceSet
fromtest
, therefor we use thetest.runtimeClasspath
(which includes all dependenciess fromtest
ANDtest
itself) as classpath for the derivedsourceSet
:sourceSets { integrationTest { compileClasspath += sourceSets.test.runtimeClasspath runtimeClasspath += sourceSets.test.runtimeClasspath // ***) } }
- note) somehow this redeclaration / extend for
sourceSets.integrationTest.runtimeClasspath
is needed, but should be irrelevant sinceruntimeClasspath
always expandsoutput + runtimeSourceSet
, don't get it
- note) somehow this redeclaration / extend for
we define a dedicated task for just running integration tests:
task integrationTest(type: Test) { }
Configure the
integrationTest
test classes and classpaths use. The defaults from thejava
plugin use thetest
sourceSet
task integrationTest(type: Test) { testClassesDir = sourceSets.integrationTest.output.classesDir classpath = sourceSets.integrationTest.runtimeClasspath }
(optional) auto run after test
integrationTest.dependsOn test
(optional) add dependency from
check
(so it always runs whenbuild
orcheck
are executed)tasks.check.dependsOn(tasks.integrationTest)
(optional) add java,resources to the
sourceSet
to support auto-detection and create these "partials" in your IDE. i.e. IntelliJ IDEA will auto createsourceSet
directories java and resources for each set if it doesn't exist:sourceSets { integrationTest { java resources } }
tl;dr
apply plugin: 'java'
// apply the runtimeClasspath from "test" sourceSet to the new one
// to include any needed assets: test, main, test-dependencies and main-dependencies
sourceSets {
integrationTest {
// not necessary but nice for IDEa's
java
resources
compileClasspath += sourceSets.test.runtimeClasspath
// somehow this redeclaration is needed, but should be irrelevant
// since runtimeClasspath always expands compileClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}
// define custom test task for running integration tests
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
tasks.integrationTest.dependsOn(tasks.test)
referring to:
- gradle java chapter 45.7.1. Source set properties
- gradle java chapter 45.7.3. Some source set examples
Unfortunatly, the example code on github.com/gradle/gradle/subprojects/docs/src/samples/java/customizedLayout/build.gradle or …/gradle/…/withIntegrationTests/build.gradle seems not to handle this or has a different / more complex / for me no clearer solution anyway!
The nebula-facet plugin eliminates the boilerplate:
apply plugin: 'nebula.facet'
facets {
integrationTest {
parentSourceSet = 'test'
}
}
For integration tests specifically, even this is done for you, just apply:
apply plugin: 'nebula.integtest'
The Gradle plugin portal links for each are:
If you're using
- Gradle 5.x, have a look at Documentation Section "Testing Java > Configuring integration tests Example 14 and 15 for details (both for Groovy and Kotlin DSL, either which one you prefer)
- alt: "current" Gradle doc link at 2, but might defer in future, you should have a look at if examples changes)
- for Gradle 4 have a look at ancient version 3 which is close near to what @Spina posted in 2012
To get IntelliJ to recognize custom sourceset as test sources root:
plugin {
idea
}
idea {
module {
testSourceDirs = testSourceDirs + sourceSets["intTest"].allJava.srcDirs
testResourceDirs = testResourceDirs + sourceSets["intTest"].resources.srcDirs
}
}
Here's what works for me as of Gradle 4.0.
sourceSets {
integrationTest {
compileClasspath += sourceSets.test.compileClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}
task integrationTest(type: Test) {
description = "Runs the integration tests."
group = 'verification'
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
As of version 4.0, Gradle now uses separate classes directories for each language in a source set. So if your build script uses sourceSets.integrationTest.output.classesDir
, you'll see the following deprecation warning.
Gradle now uses separate output directories for each JVM language, but this build assumes a single directory for all classes from a source set. This behaviour has been deprecated and is scheduled to be removed in Gradle 5.0
To get rid of this warning, just switch to sourceSets.integrationTest.output.classesDirs
instead. For more information, see the Gradle 4.0 release notes.
참고URL : https://stackoverflow.com/questions/11581419/how-do-i-add-a-new-sourceset-to-gradle
'Programing' 카테고리의 다른 글
동일한 컴퓨터에서 여러 TeamCity 에이전트를 실행하고 계십니까? (0) | 2020.09.24 |
---|---|
git에서 커밋 사이를 앞뒤로 어떻게 이동합니까? (0) | 2020.09.24 |
C ++에서 동일한 클래스에 대해 서로 다른 유형을 정의하는 방법 (0) | 2020.09.24 |
C #에서 Java System.currentTimeMillis ()에 해당 (0) | 2020.09.23 |
중복이없는 난수 만들기 (0) | 2020.09.23 |