如何仅在需要时读取 Maven 存储库凭据?
How to read Maven repository credentials only when needed?
我有一个 Gradle Kotlin DSL 脚本,可以将一些工件发布到本地 Maven 存储库:
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "my.company"
artifactId = project.name
version = "0.0.1"
from(components["java"])
}
}
repositories {
maven {
url = uri("https://maven.mycompany.com/content/repositories/whatever")
credentials {
username = (read from some file)
password = (read from some file)
}
}
}
}
如您所见,Gradle 将总是 尝试从文件中读取用户名和密码。即使不执行发布任务。
我试图通过将凭据移动到发布任务中的 doFirst
块来修复它,但代码从未执行过:
publishing {
doFirst { // this doesn't compile, doFirst doesn't exist here
}
}
tasks.getByName("publish").doFirst {
// this compiles just fine, but it's never executed
}
tasks.named("publish") {
doFirst {
// this compiles just fine, but it's never executed
}
}
如何设置凭据,使其仅在执行发布任务时发生?
无论是否需要,您总是在配置凭据。您需要有条件地配置凭据。这可以通过多种方式实现。
例如,假设您不想发布快照版本。您的构建可能类似于:
version = "0.0.1-SNAPSHOT"
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
repositories {
maven {
url = uri("https://maven.mycompany.com/content/repositories/whatever")
if (!version.toString().contains("SNAPSHOT")) {
credentials {
username = property("username") as String
password = property("password") as String
}
}
}
}
}
}
注意条件。在 configuration 阶段,Gradle 将评估该条件并跳过 credentials { }
,因为该版本包含 SNAPSHOT
.
如我所说,您可以通过多种方式执行上述操作。但是,最好的方法是使用环境变量:
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
repositories {
maven {
url = uri("https://maven.mycompany.com/content/repositories/whatever")
credentials {
username = System.getenv()["username"]
password = System.getenv()["password"]
}
}
}
}
}
没有任何条件。 null
是凭证的可接受值,在配置阶段无关紧要。重要的是你什么时候发布,什么时候你会得到 NullPointerException
我有一个 Gradle Kotlin DSL 脚本,可以将一些工件发布到本地 Maven 存储库:
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "my.company"
artifactId = project.name
version = "0.0.1"
from(components["java"])
}
}
repositories {
maven {
url = uri("https://maven.mycompany.com/content/repositories/whatever")
credentials {
username = (read from some file)
password = (read from some file)
}
}
}
}
如您所见,Gradle 将总是 尝试从文件中读取用户名和密码。即使不执行发布任务。
我试图通过将凭据移动到发布任务中的 doFirst
块来修复它,但代码从未执行过:
publishing {
doFirst { // this doesn't compile, doFirst doesn't exist here
}
}
tasks.getByName("publish").doFirst {
// this compiles just fine, but it's never executed
}
tasks.named("publish") {
doFirst {
// this compiles just fine, but it's never executed
}
}
如何设置凭据,使其仅在执行发布任务时发生?
无论是否需要,您总是在配置凭据。您需要有条件地配置凭据。这可以通过多种方式实现。
例如,假设您不想发布快照版本。您的构建可能类似于:
version = "0.0.1-SNAPSHOT"
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
repositories {
maven {
url = uri("https://maven.mycompany.com/content/repositories/whatever")
if (!version.toString().contains("SNAPSHOT")) {
credentials {
username = property("username") as String
password = property("password") as String
}
}
}
}
}
}
注意条件。在 configuration 阶段,Gradle 将评估该条件并跳过 credentials { }
,因为该版本包含 SNAPSHOT
.
如我所说,您可以通过多种方式执行上述操作。但是,最好的方法是使用环境变量:
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
repositories {
maven {
url = uri("https://maven.mycompany.com/content/repositories/whatever")
credentials {
username = System.getenv()["username"]
password = System.getenv()["password"]
}
}
}
}
}
没有任何条件。 null
是凭证的可接受值,在配置阶段无关紧要。重要的是你什么时候发布,什么时候你会得到 NullPointerException