繁体   English   中英

如何使uploadArchives依赖于另一个任务?

[英]How can I make uploadArchives dependent on another task?

我的build.gradle有以下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 ,它./gradlew clean build检查属性(我不会想要在我的构建服务器上进行,因为它没有实际上传档案的权限。 因此,仅在执行uploadArchives任务时才检查属性的方法也是可以接受的。

也许您可以尝试以下方法:

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将运行并在任何声明的变量为空的情况下引发异常。

关于您的错误消息,您可能会错过将Maven插件应用于build.gradle文件(应用插件:“ maven”)。

参见: https : //discuss.gradle.org/t/configure-mavendeployer-programmatically/16956/2

我能够部分基于@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)
    }                                                                           
  }                                                                             
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM