2

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.

jwir3
  • 6,019
  • 5
  • 47
  • 92
  • 1
    Why You invoke `UploadArchives` right after `projectEvaluate` is fired? Shouldn't it be independent task? – Opal Oct 13 '14 at 16:54
  • @Opal I do that because if I don't enclose it in that closure, then I get an undefined property exception: `> Could not find property 'uploadUsername' on project ':core'.` – jwir3 Oct 13 '14 at 16:56

3 Answers3

3

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

true-mt
  • 347
  • 3
  • 12
2

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.

Opal
  • 81,889
  • 28
  • 189
  • 210
  • I get an exception if I add `(dependsOn: checkProperties)` to the end of the `uploadArchives` task. – jwir3 Oct 13 '14 at 18:37
  • So, this still isn't quite right because `checkProperties` will still be run during evaluation (i.e. when I run `./gradlew clean build` and I don't have the variables defined, it throws the exception. I edited my own answer above to indicate a method that worked for me. – jwir3 Oct 13 '14 at 20:46
  • That's not possible. `checkProperties` will be run during build not during configuration phase. Updated answer once again, with working sample. – Opal Oct 13 '14 at 21:25
  • I've been able to get a slightly modified version the updated version of this answer (10/13/14 edit) to work for me on a similar problem. What changes I had to make were tailoring to the specifics of my problem, not the solution's general form. – John Heinnickel Sep 04 '15 at 09:44
  • If checkProperties causes an error during "gradlew clean build", my first guess would be that it omitted shortcut operator (<<) seen in the 10/13/14 edits (possibly earlier--I only saw 10/13/14 content) The same code with the shortcut operator omitted is still a valid gradle build file with crucially different semantics. With "<<" operator, block execution is conditional, depending on whether or not "checkProperties" is in task dependency graph, as desired. Without "<<" operator, block becomes an unconditional initialization block for "checkProperties" task. – John Heinnickel Sep 04 '15 at 10:02
0

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)
    }                                                                           
  }                                                                             
} 
jwir3
  • 6,019
  • 5
  • 47
  • 92
  • So, apparently this isn't working as well as I thought... it still gives me an error if I try to run `./gradlew clean build` - that is, even if `uploadArchives` isn't being run, it checks for the properties. – jwir3 Oct 13 '14 at 18:37
  • It will because the whole code is run at the configuration phase of build. Not execution. – Opal Oct 13 '14 at 19:18