我正在尝试在 Java 项目的 Gradle 构建脚本中包含类路径依赖项。我有 gradle.properties
文件,其中列出了两个变量:nexusUsername
和 nexusPassword
。这两个在项目根存储库中的使用方式与下面的构建脚本存储库相同,并且工作正常 - 依赖项已加载。
但是,当像这样配置构建脚本时:
buildscript {
repositories {
maven {
credentials {
username nexusUsername
password nexusPassword
}
url 'https://edited'
}
}
dependencies {
classpath 'edited'
}
}
我收到这样的错误:
Could not GET 'https://edited.pom'. Received status code 403 from server: Forbidden
通过身份验证访问浏览器给出的 URL。
据我所知,到目前为止,buildscript 在构建的一开始就被评估,因此,属性可能还没有加载?如果这是真的,如何加载它们?
最佳答案
解决方法如果您想使用替代属性文件,例如 local.properties、gradle-local.properties:
1°) 在项目 rootdir 中,添加新文件“add-custom-properties.gradle”:
Properties props = new Properties()
props.load(new FileInputStream("$rootDir/gradle-local.properties"))
props.each { prop ->
ext.set(prop.key, prop.value)
}
如果需要,您可以向之前的脚本添加逻辑。
2°) 编辑/创建你的 gradle-local.properties
repoUrl=https://a-url...
repoUser=a-user
repoPass=a-pass
3°)在 build.gradle、settings.xml、...(在 buildscript{...} 内):
buildscript {
apply from: "$rootDir/add-custom-properties.gradle"
repositories {
maven {
url = "${repoUrl}"
credentials {
username = "${repoUser}"
password = "${repoPass}"
}
}
}
关于java - buildscript 中的 gradle.properties 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42134426/