Android Gradle 自动化拉取 submodule

这篇博客介绍了两种在项目中引入并更新其他Git库中指定文件的方法。方案一是通过Git子模块,包括配置sparse-checkout来拉取特定文件,适用于本地编译;方案二是通过脚本直接拉取,适用于CI环境中。文中提供了详细的脚本示例,涉及git init、remote add、sparse-checkout等操作。
摘要由CSDN通过智能技术生成

需求: 期望在项目中引入其他git库, 并且需要指定拉取的文件, 可以应用在ci中

方案一 子模块方式
在项目中引入子模块, 子模块的增删自行google,下面的脚本可以在本地编译前 自动拉取最新的指定文件, ci中需要在docker配置ssh且sparse-checkout貌似不生效

task protoSync {
    def dirAdsys = new File("app\\xxx\\xxx")
    if(dirAdsys.exists()) dirAdsys.deleteDir()

    def protoConfig =
            "xxx/xxx/xxx/xxx1.java\n" +
             "xxx/xxx/xxx/xxx2.java\n"

    def protoSparseFile = new File('.git\\modules\\app\\xxx\\info\\sparse-checkout')
    protoSparseFile.write(protoConfig)

    'git config core.sparsecheckout true'.execute()
    'git submodule foreach git pull'.execute()
    'git submodule foreach git reset HEAD --hard'.execute()

    Thread.sleep(5_000)
}

protoSync.doLast {generateReleaseProto}

build.dependsOn protoSync

方案二 脚本自动拉取指定库的指定文件
脚本中的路径自己更换

task protoSync {
	//删除原来的引入库文件
    def dirAdsys = new File("xxx\\xxx")
    if(dirAdsys.exists()) dirAdsys.deleteDir()
    //创建文件夹
    dirAdsys.mkdir()
    //初始化git
    'git init xxx\\xxx'.execute().waitFor()
    //添加远程库
    'git --git-dir=xxx\\xxx\\.git --work-tree=xxx/xxx remote add origin git@git.xxx.git'.execute().waitFor()
    //开启配置可指定文件
    'git --git-dir=xxx\\xxx\\.git --work-tree=xxx/xxx config core.sparsecheckout true'.execute().waitFor()
    //写入需要指定的文件
    def protoConfig =
            "xxx/xxx/xxx/xxx.java\n" +
                    "xxx/xxx/xxx/xxx.java\n" 
    def protoSparseFile = new File('xxx\\xxx\\.git\\info\\sparse-checkout')
    protoSparseFile.write(protoConfig)
    //拉取代码
    'git --git-dir=app\\adsys-interface\\.git --work-tree=app/adsys-interface pull origin master'.execute().waitFor()
    //删除git, 否则ci会当成子项目
    def dirAdsysGit = new File("app\\adsys-interface\\.git")
    if(dirAdsysGit.exists()) dirAdsysGit.deleteDir()
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值