gradle根据Git信息生成README文件
task createReadMe{
doLast {
// String str = "${buildDir}/libs/exploded/"+project.getName().toString()+ "-${version}.war"+"/README"
String str = "${buildDir}/libs/"+"/README"
new File(str).createNewFile()
File file= new File(str)
def osm = file.newOutputStream()
String string ="远程地址"+" "+"分支"+" "+"Commit ID"+" "+"User ID"+" "
osm.write(string.getBytes())
for(Project p : getAllprojects()) {
String dir = p.getProjectDir().toString()
String gitBashH ='git --git-dir='+dir+'/.git --work-tree='+dir+'/ log --pretty="format:%H" -1'
String gitBashAn ='git --git-dir='+dir+'/.git --work-tree='+dir+'/ log --pretty="format:%an" -1'
String gitBashUrl ='git --git-dir='+dir+'/.git --work-tree='+dir+'/ remote -v'
String gitBashBranch='git --git-dir='+dir+'/.git --work-tree='+dir+'/ symbolic-ref --short -q HEAD'
String resultH=gitBashH.execute().text.trim()
String resultAn=gitBashAn.execute().text.trim()
String resultUrl=gitBashUrl.execute().text.trim()
String resultBranch=gitBashBranch.execute().text.trim()
String stringResult=resultUrl.split(" ")[0]+" "+resultBranch+" "+resultH+" "+resultAn+" "
println stringResult
osm.write(stringResult.getBytes())
}
osm.close()
}
}
可以在需要的地方生成git等相关信息
如果需有在其他task调取该方法
doFirst { def testTaskReadMe = project.tasks.findByName("createReadMe") testTaskReadMe.execute() }
附上几个链接
https://docs.gradle.org/current/userguide/declaring_repositories.html
https://jimmysong.io/cheatsheets/git-log-format
-