英文:
MissingPropertyException: Gradle Task Fails Due to Unknown Property
问题
我遇到了以下错误:
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'integrationTest' for SourceSet container of type org.gradle.api.internal.tasks.DefaultSourceSetContainer.
这是我的 build.gradle
文件。我已经定义了一个 integrationTest
任务。
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.8'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.app.api'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'junit:junit:4.13.1'
}
tasks.register('integrationTest', Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
tasks.withType(Jar).configureEach {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Main-Class"] = 'com.app.api.ApiApplication'
}
configurations["compileClasspath"].each { file ->
from(zipTree(file.absoluteFile))
}
}
如何让 integrationTest
属性在 Gradle 中被识别?
英文:
I am encountering the following error:
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'integrationTest' for SourceSet container of type org.gradle.api.internal.tasks.DefaultSourceSetContainer.
This is my build.gradle
file. I have a integrationTest
task defined already.
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.8'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.app.api'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'junit:junit:4.13.1'
}
tasks.register('integrationTest', Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
tasks.withType(Jar).configureEach {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Main-Class"] = 'com.app.api.ApiApplication'
}
configurations["compileClasspath"].each { file ->
from(zipTree(file.absoluteFile))
}
}
How can one make integrationTest
property be known to gradle?
答案1
得分: 1
Using the ext
block (which allows you to define custom properties and methods that can be accessed from anywhere in your build file), I can add the custom property integrationTest
ext {
integrationTest = project.hasProperty('integrationTest')
}
And I added a check in the integrationTest
task:
tasks.register('integrationTest', Test) {
if (integrationTest) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
}
Now when I add the system property via the terminal:
./gradlew integrationTest -DintegrationTest=true
The property's value will be set to true
.
英文:
Using the ext
block (which allows you to define custom properties and methods that can be accessed from anywhere in your build file), I can add the custom property integrationTest
ext {
integrationTest = project.hasProperty('integrationTest')
}
And I added a check in the integrationTest
task:
tasks.register('integrationTest', Test) {
if (integrationTest) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
}
Now when I add the system property via the terminal:
./gradlew integrationTest DintegrationTest=true
The property's value will be set to true
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论