我的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
任务时才检查属性的方法也是可以接受的。
答案 0 :(得分: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
将被运行并且如果任何声明的变量为空则抛出异常。
答案 1 :(得分:2)
关于您的错误消息,您可能会错过将maven插件应用于build.gradle文件(应用插件:&#39; maven&#39;)。
请参阅:https://discuss.gradle.org/t/configure-mavendeployer-programmatically/16956/2
答案 2 :(得分:0)
我能够部分地根据@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)
}
}
}