我的build.gradle
中有以下内容:
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
pom.packaging = "aar"
pom.groupId = project.CORE_GROUP
pom.version = project.CORE_VERSION_NAME
repository(url: "scp://" + project.CORE_MAVEN_URL) {
authentication(userName: project.uploadUsername, privateKey: project.uploadKeyFile)
}
}
}
}
}
我希望它依赖于以下任务:
task checkProperties << {
if (!project.hasProperty('uploadUsername')) {
throw new RuntimeException("Couldn't find uploadUsername property. Did you forget to specify it in ~/.gradle/gradle.properties?")
} else if (!project.hasProperty('uploadKeyFile')) {
throw new RuntimeException("Couldn't find uploadKeyFile property. Did you forget to specify it in ~/.gradle/gradle.properties?")
}
}
我怎样才能做到这一点?如果我写以下内容:
afterEvaluate { project ->
uploadArchives(dependsOn: checkProperties) {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
pom.packaging = "aar"
pom.groupId = project.CORE_GROUP
pom.version = project.CORE_VERSION_NAME
repository(url: "scp://" + project.CORE_MAVEN_URL) {
authentication(userName: project.uploadUsername, privateKey: project.uploadKeyFile)
}
}
}
}
}
然后我收到以下错误:
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/scottjohnson/Source/core-android/core/build.gradle' line: 61
* What went wrong:
A problem occurred configuring project ':core'.
> org.gradle.api.internal.MissingMethodException: Could not find method mavenDeployer() for arguments [build_42edqo477lbj5geoh0e3gdkj7q$_run_closure6_closure9_closure10_closure11@30b8afce] on repository container.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 7.68 secs
顺便说一句,我想这样做的原因是,现在,如果我只是将检查属性的代码放入uploadArchives
任务中,那么即使我运行 ./gradlew clean build
,它也会检查属性(我不想在我的构建服务器上发生,因为它没有实际上传档案的权限(。因此,仅在执行uploadArchives
任务时检查属性的方法也是可以接受的。
关于您的错误消息,您可能会错过将 maven 插件应用于您的 build.gradle 文件(应用插件:"maven"(。
请参阅:https://discuss.gradle.org/t/configure-mavendeployer-programmatically/16956/2
也许你可以尝试这样的事情:
apply plugin: 'java'
def uploadUsername = project.hasProperty('uploadUsername') ? project['uploadUsername'] : ''
def uploadKeyFile = project.hasProperty('uploadKeyFile') ? project['uploadKeyFile'] : ''
uploadArchives { }
task checkProperties << {
if (!uploadUsername) {
throw new RuntimeException("Couldn't find uploadUsername property. Did you forget to specify it in ~/.gradle/gradle.properties?")
} else if (!uploadKeyFile) {
throw new RuntimeException("Couldn't find uploadKeyFile property. Did you forget to specify it in ~/.gradle/gradle.properties?")
}
}
uploadArchives.dependsOn(checkProperties)
一开始,读取这两个属性并将其分配给两个变量。如果其中任何一个不存在,则将分配简单的空值。它不会干扰生成流。然后声明uploadArchives
依赖于checkProperties
。如果调用它checkProperties
将运行,如果任何声明的变量为空,则会引发异常。
我能够部分根据@Opal的评论弄清楚:
def checkProperties() {
if (!project.hasProperty('uploadUsername')) {
throw new RuntimeException("Couldn't find uploadUsername property. Did you forget to specify it in ~/.gradle/gradle.properties?")
} else if (!project.hasProperty('uploadKeyFile')) {
throw new RuntimeException("Couldn't find uploadKeyFile property. Did you forget to specify it in ~/.gradle/gradle.properties?")
}
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
pom.packaging = "aar"
pom.groupId = project.CORE_GROUP
pom.version = project.CORE_VERSION_NAME
repository(url: "scp://" + project.CORE_MAVEN_URL) {
}
}
}
}
// We need to check to make sure the properties are available before we execute
// uploadArchives.
gradle.taskGraph.beforeTask { Task aTask ->
if (aTask == uploadArchives) {
checkProperties()
aTask.repositories.mavenDeployer.repository(url: "scp://" + project.CORE_MAVEN_URL) {
authentication(userName: project.uploadUsername, privateKey: project.uploadKeyFile)
}
}
}