How can I make uploadArchives dependent on another task?

心已入冬 提交于 2020-01-02 19:28:39

问题


I have the following in my 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

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.


回答1:


Maybe You can try something like:

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)

At the beginning both properties are read and assigned to two variables. If any of them not exists, simple empty value will be assigned. It doesn't interfere with build flow. Then uploadArchives is declared to depend on checkProperties. If it's invoked checkProperties will be run and throw exception if any of declared variables is empty.




回答2:


Regarding to your error message you might miss to apply the maven plugin to your build.gradle file (apply plugin: 'maven').

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




回答3:


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


来源:https://stackoverflow.com/questions/26344925/how-can-i-make-uploadarchives-dependent-on-another-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!