cwz_茶仔 2019-04-23 18:11 采纳率: 50%
浏览 674
已采纳

Gradle构建的JavsWeb应用,如何在代码中获取build.gradle的version

如题。需求要求在登录页面显示web项目的版本号。

百度了很久没找到类似的问题,所以就来提问了~

build.gradle里面

group = 'Tomcat_Server'
version = '0.6.0-Debug'

sourceCompatibility = 1.8

想要在java代码(Controller层)中获取到“0.6.0-Debug”这个东西

  • 写回答

1条回答 默认 最新

  • 起个名特麻烦 2020-08-27 17:33
    关注

    最近我也想获取gradle里的version,思路来自里面注释的第二个连接,我将版本号生成到配置文件,java在读取配置文件就能每次打包程序都能获取最新版本信息

    task('addVersion') {
        //https://my.oschina.net/lifj/blog/749345
        //https://stackoverflow.com/questions/51669358/get-value-from-build-gradle-to-code
        doLast {
            println("task addVersion")
            println("task ${version}")
            def myV = "${version}"
            //append the version here, see example
            //file("src/main/resources/application-build.properties").append("\nversion=jhd\n")
            //file("src/main/resources/application-build.properties").
            def filePath = 'src/main/resources/application-build.properties';
            Properties props = new Properties();
            props.load(new FileInputStream(file(filePath)))
            // props.put("buildtime", new Date().format("dd MMM yyyy hh:mm aa"))
            props.put("build.datetime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm")))
            //这里有个小问题,使用${}引用的变量是GStringImpl 不能传参给方法会出错,转成toString
            props.put("build.version", myV.toString())
            OutputStream out = new FileOutputStream(filePath)
            def comments = '#jhd auto make build info'
            //props.store(out, comments)
            props.store(new OutputStreamWriter(out, "utf-8"), comments)//解决乱码
        }
    }
    bootJar.dependsOn(addVersion)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制