Yarh
Yarh

Reputation: 4627

How to run shell script after artifact assemble?

I want to run task wsUpload, which runs shell script to upload artifact to company server after assembleMinAPI16ProdRelease finishes artifact assemble.

Started From this:

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

}

But it runs before assembleRelease finishes. Then I tried as in this 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()
  }
}

that left me with > java.lang.NullPointerException (no error message) Then I tried to make whole swUpload as 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")
}

And got error:

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

Upvotes: 1

Views: 254

Answers (1)

Opal
Opal

Reputation: 84854

First of all you need to define a task in a proper way (read about configuration vs execution in gradle) which will be:

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

Now you need to define execution order dependencies between tasks - remember not to call execute() on a task instance directly never!

I suppose that both assembleMinAPI16ProdRelease and crashlyticsUploadDistributionMinAPI16ProdRelease are dynamic tasks (created after project is evaluated) so it will be sage to define dependencies in afterEvaluate block:

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

Why both mustRunAfter and dependsOn? To define not only dependencies but proper execution order as well. Now you should be able to run gradle wsUpload and it should all work well.

If you want to run assembleMinAPI16ProdRelease and have artifacts uploaded then finalizedBy should be used:

afterEvaluate {
   assembleMinAPI16ProdRelease.finalizedBy crashlyticsUploadDistributionMinAPI16ProdRelease
   crashlyticsUploadDistributionMinAPI16ProdRelease.finalizedBy wsUpload
}

Note that since I'm not android developer I can't test it properly - so if it doesn't work, let me know before downvoting ;)

Upvotes: 3

Related Questions