简体   繁体   中英

Fetch Gradle subproject at build time using grgit

How can I have gradle clone a git repository (to a sub-repo of the current one) and build it as a subproject?

Currently I have the following:

settings.gradle:

include 'contrib/dependency/foolib'

build.gradle (shortened):

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'org.ajoberstar:gradle-git:0.7.0'
    }
}

apply plugin: 'com.android.application'

import org.ajoberstar.grgit.*

task clone << {
    File dir = new File('contrib/dependency')
    if(!dir.exists()) {
        def grgit = Grgit.clone(dir: dir, uri: 'https://github.com/someone/dependency.git', refToCheckout: 'dev')
    }
    def grgit = Grgit.open(dir)
    grgit.checkout(branch: 'dev')
    grgit.pull(rebase: false)
}


project.afterEvaluate {
    preBuild.dependsOn clone
}


repositories {
    mavenCentral()
}

dependencies {
    compile project(':contrib/dependency/foolib')
}

android {
    // nothing out of the ordinary here, omitting
}

The subproject has its own build.gradle , which builds fine on its own.

I'd gradually built and tested the script, with the clone operation first (which worked well). When I first ran the script in its full final form, the sub-repo was already/still there, thanks to the previous clone operations, and the build completed smoothly.

However, when I simulated a fresh build by simply deleting contrib , I got an error:

Cannot evaluate module contrib/dependency/foolib : Configuration with name 'default' not found.

Apparently, this is caused by references to the still-to-be-imported subproject. How can I resolve this?

I eventually solved it using git submodules, as @nickb suggested.

build.gradle (shortened):

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

dependencies {
    compile project(':contrib/dependency/foolib')
}

android {
    // nothing out of the ordinary here, omitting
}

(Essentially, I just removed all the grgit stuff but kept the subproject as a dependency.)

Then in my project dir I did:

git submodule add https://github.com/someone/dependency.git contrib/dependency
cd contrib/dependency

# then either (to switch to a different branch):
git checkout -b mybranch origin/mybranch

# or (to check out a particular ref on the current branch):
git checkout deadc0de

# or both of the above (to check out a particular ref on a different branch)

(To update the submodule later, you may need to do git fetch or git pull before checking out the ref you want.)

Be advised, however, that working with submodules in git is not without pitfalls, which can easily destroy changes you made in the submodule or inadvertently revert to an outdated release. To avoid these:

  • Don't commit to the submodule (always commit upstream, then pull from upstream)
  • Be sure to run git submodule update each time your HEAD changes (pull, merge, checkout etc.)

A good article on the pitfalls of git submodules it at: https://codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM