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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示