赞
踩
bootJar { File dir = file("build/") if (dir.isDirectory()) { dir.eachFileRecurse { file -> def name = file.getName() if(file.isFile() && name.startsWith('version')) { file.delete() } } } String fileName = "build/version.info" doFirst { File out = file(fileName) out.withWriter('UTF-8'){ writer -> writer.write("提交记录: " + "\r\n" + 'git log --oneline -5'.execute().text.trim() + "\r\n\r\n") writer.write("打包分支: " + 'git rev-parse --abbrev-ref HEAD'.execute().text.trim() + "\r\n") writer.write("当前用户: " + 'git config user.email'.execute().text.trim() + "\r\n") writer.write("打包时间: " + new Date().format('yyyy-MM-dd HH:mm:ss') + "\r\n") } } from ("build/version.info") {into '/'} }
有时可能是多模块构建的项目,上面的task可能会无法打印git信息,需要加上当前的项目目录,(好像是无法识别git目录)
bootJar { // 设置bootJar的扩展加载功能 File dir = file("build/") if (dir.isDirectory()) { dir.eachFileRecurse { file -> def name = file.getName() if(file.isFile() && name.startsWith('version')) { file.delete() } } } def currentDirectory = project.projectDir String fileName = "build/version" +".txt" def gitBranch = 'git -C '+project.projectDir+' rev-parse --abbrev-ref HEAD' def gitUser = 'git -C '+project.projectDir+' config user.email' def gitLogCommand = ["git", "-C", project.projectDir.toString(), "log", "--oneline", "-5"] def logs="" try { Process process = new ProcessBuilder(gitLogCommand) .directory(currentDirectory) .redirectErrorStream(true) .start() InputStream inputStream = process.inputStream BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, 'UTF-8')) reader.eachLine { line -> logs=logs+line+'\n' } reader.close() process.waitFor() } catch (IOException | InterruptedException e) { throw new GradleException("Error executing Git command: ${e.message}", e) } doFirst { File out = file(fileName) out.withWriter('UTF-8'){ writer -> writer.write("java版本信息:" + System.getProperty("java.version") + "\r\n") writer.write("系统版本信息:" + System.getProperty("os.name") + "\r\n") writer.write("提交记录: " + "\r\n" + logs+ "\r\n\r\n") writer.write("打包分支: " + gitBranch.execute().text.trim() + "\r\n") writer.write("当前用户: " + gitUser.execute().text.trim() + "\r\n") writer.write("打包时间:" + new Date().format('yyyy-MM-dd HH:mm:ss') + "\r\n") } } }
task packSrc(type: Zip) {
group 'build'
description "Package src."
archiveName "src.zip"
from sourceSets.main.allSource
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。