2017-07-18 103 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/

相關問題