如何在工件组装后运行shell脚本?

时间:2017-09-18 15:45:18

标签: android gradle

我想运行任务wsUpload,它运行shell脚本,在assembleMinAPI16ProdRelease完成工件组装后将工件上传到公司服务器。

从这开始:

afterEvaluate {
  task wsUpload(type: Exec, dependsOn: assembleMinAPI16ProdRelease) {
    def localPropertiesFile = rootProject.file("local.properties");
    def localProperties = new Properties()
    localProperties.load(new FileInputStream(localPropertiesFile))
    def WORK_EMAIL = localProperties['WORK_EMAIL']
    commandLine '../scripts/ws_upload.sh'
    args = [rootProject.ext.VERSION_CODE, WORK_EMAIL]
    println("wsUpload start ")
    commandLine '../scripts/ws_upload.sh'
    //      args = [rootProject.ext.VERSION_CODE, WORK_EMAIL]
    println("wsUpload end")
  }

  assembleMinAPI16ProdRelease.doLast {
    println("-------------------------------------")
    println("---------------DO LAST---------------")
    println("-------------------------------------")
    crashlyticsUploadDistributionMinAPI16ProdRelease.execute()
  }

}

但它在assembleRelease完成之前运行。 然后我尝试了这个answer

task wsUpload(type: Exec) {
  def localPropertiesFile = rootProject.file("local.properties");
  def localProperties = new Properties()
  localProperties.load(new FileInputStream(localPropertiesFile))
  def WORK_EMAIL = localProperties['WORK_EMAIL']
  commandLine '../scripts/ws_upload.sh'
  args = [rootProject.ext.VERSION_CODE, WORK_EMAIL]
  println("wsUpload start ")
  doLast {
    exec {
      commandLine '../scripts/ws_upload.sh'
      args = [rootProject.ext.VERSION_CODE, WORK_EMAIL]
      println("wsUpload end")
    }
  }
}

afterEvaluate {

  assembleMinAPI16ProdRelease.doLast {
    println("-------------------------------------")
    println("---------------DO LAST---------------")
    println("-------------------------------------")
    crashlyticsUploadDistributionMinAPI16ProdRelease.execute()
  }
  assembleMinAPI16ProdRelease.doLast {
    wsUpload.execute()
  }
}

让我留下了> java.lang.NullPointerException (no error message) 然后我尝试将整个swUpload作为doLast

task wsUpload(type: Exec) << {
  def localPropertiesFile = rootProject.file("local.properties");
  def localProperties = new Properties()
  localProperties.load(new FileInputStream(localPropertiesFile))
  def WORK_EMAIL = localProperties['WORK_EMAIL']
  commandLine '../scripts/ws_upload.sh'
  args = [rootProject.ext.VERSION_CODE, WORK_EMAIL]
  println("wsUpload start ")
  commandLine '../scripts/ws_upload.sh'
  args = [rootProject.ext.VERSION_CODE, WORK_EMAIL]
  println("wsUpload end")
}

得到错误:

Execution failed for task ':app:wsUpload'.
> execCommand == null!

1 个答案:

答案 0 :(得分:3)

首先,您需要以适当的方式定义任务(在gradle中阅读配置 vs 执行),这将是:

task wsUpload(type: Exec) {
  def localPropertiesFile = rootProject.file("local.properties");
  def localProperties = new Properties()
  localProperties.load(new FileInputStream(localPropertiesFile))
  def WORK_EMAIL = localProperties['WORK_EMAIL']
  commandLine '../scripts/ws_upload.sh'
  args [rootProject.ext.VERSION_CODE, WORK_EMAIL]
}

现在您需要在任务之间定义执行顺序依赖关系 - 切记不要直接在任务实例上调用execute()

我认为assembleMinAPI16ProdReleasecrashlyticsUploadDistributionMinAPI16ProdRelease都是动态任务(在评估项目后创建),因此在afterEvaluate块中定义依赖关系将是圣人:

afterEvaluate {
  crashlyticsUploadDistributionMinAPI16ProdRelease.dependsOn assembleMinAPI16ProdRelease
  crashlyticsUploadDistributionMinAPI16ProdRelease.mustRunAfter assembleMinAPI16ProdRelease
  wsUpload.dependsOn crashlyticsUploadDistributionMinAPI16ProdRelease
  wsUpload.mustRunAfter crashlyticsUploadDistributionMinAPI16ProdRelease
}

为何选择mustRunAfterdependsOn?不仅要定义依赖关系,还要定义正确的执行顺序。 现在你应该能够运行gradle wsUpload并且它应该都运行良好。

如果您要运行assembleMinAPI16ProdRelease并上传工件,则应使用finalizedBy

afterEvaluate {
   assembleMinAPI16ProdRelease.finalizedBy crashlyticsUploadDistributionMinAPI16ProdRelease
   crashlyticsUploadDistributionMinAPI16ProdRelease.finalizedBy wsUpload
}

请注意,由于我不是Android开发人员,因此我无法正确测试 - 所以如果它不起作用,请在downvoting之前告诉我们;)