我在Java项目中使用Gradle,并且在settings.gradle.kts
个和build.gradle.kts
个文件中使用相同的URL.
URL在两个文件中都是重复的,我想声明它一次,并以Gradle设计的方式在两个文件中使用它(没有黑客攻击).
我试过以下方法:
- 使用项目的extra,但可以找到一种方法在两个文件中使用它,但只能在构建文件中使用它.
- Declare the variable in the top of the
settings.gradle.kts
个 file and then try to access it from the build file - with no success.
我知道我可以使用gradle.properties
文件,但它是每个开发人员的本地文件,我不希望每个人都将它添加到他们的文件中.
我正在努力寻找对球队透明的方式.
现在看起来是这样的:
settings.gradle.kts
个
pluginManagement {
val nexusUsername: String by settings
val nexusPassword: String by settings
repositories {
maven {
credentials {
username = nexusUsername
password = nexusPassword
}
val nexusBaseUrl = System.getenv("NEXUS_BASE_URL") ?: "my-default-url"
url = uri("$nexusBaseUrl/nexus/content/repositories/gradle-plugins/")
}
}
}
build.gradle.kts
个
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
plugins {
java
checkstyle
id("com.github.johnrengelman.shadow") version "7.1.2"
}
subprojects {
val nexusUsername: String by project
val nexusPassword: String by project
repositories {
maven {
name = "NexusPublic"
credentials {
username = nexusUsername
password = nexusPassword
}
val nexusBaseUrl = System.getenv("NEXUS_BASE_URL") ?: "my-default-url"
url = uri("$nexusBaseUrl/nexus/content/groups/public/")
}
}
我希望nexusBaseUrl
被声明一次,并在两个文件中使用.