当前位置:   article > 正文

gradle打包_gradle 8.6 自定义打包

gradle 8.6 自定义打包

1.自定义打包

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 '/'}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

有时可能是多模块构建的项目,上面的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")
	    }
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

2.打源码包

task packSrc(type: Zip) {
	group 'build'
	description "Package src."
	archiveName "src.zip"
	from sourceSets.main.allSource
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/293205
推荐阅读
相关标签
  

闽ICP备14008679号