我怎样才能使uploadArchives依赖于另一项任务? [英] How can I make uploadArchives dependent on another task?

查看:2939
本文介绍了我怎样才能使uploadArchives依赖于另一项任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
        }
      }
    }
  }
}

我希望它依赖于以下任务:

And I want it to be dependent on the following task:

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?")
  }
}

我该如何做到这一点?如果我写下:

How can I achieve this? If I write the following:

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)
        }
      }
    }
  }
}

然后我得到以下错误:

Then I get the following error:

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 任务时才会检查属性的方法也是可以接受的。

BTW, the reason I want to do this is that right now, if I just put the code that checks the properties into the uploadArchives task, then even if I run ./gradlew clean build, it checks the properties (which I don't want to happen on my build server, since it doesn't have permission to actually upload the archives). Thus, a method that would check the properties only when the uploadArchives task is executed would also be acceptable.

推荐答案

我能够根据@ Opal的评论找出部分内容:

I was able to figure it out based in part on @Opal's comment:

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)
    }                                                                           
  }                                                                             
} 

这篇关于我怎样才能使uploadArchives依赖于另一项任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆