2017-07-18 28 views
1

如何将git克隆到git存储库(到当前子仓库的子仓库)并将其构建为子项目?使用grgit在构建时抓取Gradle子项目

目前,我有以下几点:

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. 

显然,这是至仍需待进口子项目引用造成的。我该如何解决这个问题?

+1

你为什么不使用[git的子模块(https://git-scm.com/book/en/v2/Git - 工具 - 子模块),而不是? – nickb

+0

如果我理解正确,git子模块不提供版本之间的链接(例如,根项目的#badcafe取决于子项目的#600df00d),我可以通过检查我想要的参考文件来控制它。 – user149408

+1

不是,您可以将子模块指向特定的提交。请参阅:https:// stackoverflow。com/questions/10914022/how-to-check-out-specific-version-of-submodule-using-git-submodule – nickb

回答

0

我最终使用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 fetchgit pull检查你想要的参考。)

然而,建议,在git子模块工作不是没有陷阱,它可以轻松销毁您在子模块中所做的更改或无意中恢复到过时的版本。为了避免这些:

  • 不要提交到子模块(始终致力于上游,然后从上游拉)
  • 一定要每次运行的水头变化(拉,合并,校验等)git submodule update

上的git的陷阱一篇好文章子模块它在:https://codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/

相关问题