我如何让gradle克隆git存储库(到当前库的子回购)并将其构建为子项目?
目前,我有以下几点:
settings.gradle:
include 'contrib/dependency/foolib'build.gradle (简称):
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
}子项目有自己的build.gradle,它自己构建得很好。
我逐渐构建并测试了脚本,首先使用了克隆操作(它运行得很好)。当我第一次以完整的最终形式运行脚本时,由于之前的克隆操作,子回购已经/仍然存在,并且构建工作顺利完成。
然而,当我简单地删除contrib来模拟一个新的构建时,我得到了一个错误:
Cannot evaluate module contrib/dependency/foolib : Configuration with name 'default' not found.显然,这是由于对尚未导入的子项目的引用造成的。我怎么解决这个问题?
发布于 2017-07-23 15:34:21
我最终使用git子模块解决了这个问题,就像@nickb建议的那样。
build.gradle (简称):
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
}(本质上,我只是删除了所有的grgit内容,但将子项目保留为依赖项。)
然后,在我的项目中,我做到了:
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)(为了稍后更新子模块,您可能需要在签出所需的引用之前执行git fetch或git pull操作。)
但是,请注意,在git中使用子模块并非没有缺陷,这很容易破坏您在子模块中所做的更改,或者无意中恢复到过时的版本。为避免这些情况:
git submodule update (拉、合并、签出等)。一篇很好的文章,介绍git子模块的缺陷:https://codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/
https://stackoverflow.com/questions/45174392
复制相似问题