需求: 期望在项目中引入其他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()
}