MissingPropertyException: 由于未知属性而导致 Gradle 任务失败

huangapple go评论60阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月19日 15:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定