其他分享
首页 > 其他分享> > Gradle生成Git 分支等信息 READEME

Gradle生成Git 分支等信息 READEME

作者:互联网

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 ="远程地址"+"\t"+"分支"+"\t"+"Commit ID"+"\t"+"User ID"+"\n"
        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 resultH=gitBashH.execute().text.trim()
            String resultAn=gitBashAn.execute().text.trim()
            String resultUrl=gitBashURl.execute().text.trim()
            String stringResult=resultUrl.split("\n")[0]+"\t"+p.getName()+"\t"+resultH+"\t"+resultAn+"\n"
            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

 

标签:execute,git,String,READEME,Gradle,--,Git,osm,dir
来源: https://www.cnblogs.com/onexixi/p/14243924.html