簡體   English   中英

使用grgit在構建時獲取Gradle子項目

[英]Fetch Gradle subproject at build time using grgit

如何讓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.

顯然,這是由於引用仍要導入的子項目引起的。 我該如何解決?

我最終使用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才能簽出所需的ref。)

但是,請注意,在git中使用子模塊並非沒有陷阱,這會輕易破壞您在子模塊中所做的更改或無意中恢復到過時的發行版。 為了避免這些:

  • 不要提交到子模塊(始終向上游提交,然后從上游拉出)
  • 確保每次您的HEAD更改(拉,合並,簽出等)時運行git submodule update

一篇關於git子模塊陷阱的好文章, 網址為: https : //codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM