0

I have a project which depends on other repos. My goal is to write a gradle task that clones the required repos automatically.

The problem I am having is that when I run my task, gradlew fails because it tries to compile the project dependencies before running my task. Here is what I have:

settings.gradle

include ':app'
include 'testRepo'
project(':testRepo').projectDir = new File('../testRepo')

build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.2'
    classpath 'org.ajoberstar:gradle-git:1.5.1'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    classpath 'com.google.gms:google-services:3.0.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

build.gradle(app)

task clone << {
    Grgit.clone(dir: file('../testRepo'), uri: "https://github.com/fakeAccount/testRepo.git")
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile project(':testRepo')
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.google.android.gms:play-services-maps:8.4.0'
    compile 'com.google.android.gms:play-services-location:8.4.0'
}

When I do ./gradlew clone I get:

"Cannot evaluate module testRepo : Configuration with name 'default' not found"

This is because it tries to compile the testRepo project before cloning from GitHub. If I take out the compile project(':testRepo') it works fine.

Any ideas as to how to make this work?

mkobit
  • 43,979
  • 12
  • 156
  • 150
user3670466
  • 13
  • 1
  • 5
  • is testRepo in your settings.gradle ? – ligi Aug 10 '16 at 22:43
  • 1
    You have a fundamental ordering problem here. The `dependencies` block is part of task configuration, which occurs *before* any tasks are executed. But the thing you're depending on doesn't exist until the task is executed. – Oliver Charlesworth Aug 10 '16 at 22:44
  • @OliverCharlesworth Thanks for pointing that out. I was wondering if a task could be executed before the dependencies block. Do you think the issue you mentioned makes it impossible to acomplish my goal? – user3670466 Aug 11 '16 at 00:36
  • You can't run tasks before configuration ([basically by definition](https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:build_phases)), but you could certainly execute arbitrary Groovy code before anything else gets run. That said, a more conventional way of solving this problem would be to build and publish a Maven/Ivy artifact from testRepo and then depend on it, or to incorporate testReo via a Git submodule or subtree. – Oliver Charlesworth Aug 11 '16 at 11:13
  • @OliverCharlesworth How can I execute groovy code before anything gets run? – user3670466 Aug 11 '16 at 14:31
  • Your entire Gradle file is just Groovy being executed. So put your `Grgit.clone()` statement outside of a task, and it will just execute immediately. – Oliver Charlesworth Aug 11 '16 at 14:38
  • @OliverCharlesworth That worked. But would it possible to put the Grgit.clone() in a separate file, and do something like ./gradlew run externalFile.gradle ? – user3670466 Aug 11 '16 at 15:01

0 Answers0