从build.gradle获取价值并编写代码 [英] Get value from `build.gradle` to code

查看:135
本文介绍了从build.gradle获取价值并编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 build.gradle ,其中定义了版本。我需要实现一个版本端点(例如 / version )以获取项目的版本。 build.gradle 中的 version 属性已经存在很长时间了,我无法将其移至 project.properties 文件。如何从Java代码中访问此版本的值?

We have a build.gradle where the version is defined in it. I need to implement a version endpoint ( like /version) to get the version of the project. This version property in build.gradle has been there for a long time, I can't move it to the project.properties file. How can I access this version's values from my Java code?

推荐答案

有很多方法可以将信息植入代码中

There are many ways with which you can "plant" information into your code

首先,您可以使用清单文件,并从中读取文件

First you can use the manifest file, and read from it

jar {
  manifest {
    attributes(
      "lib-version": version
  }
}

一起阅读我自己的Jar清单

另一种选择是在jar任务之前将该信息添加到您的属性文件中

The other option is to add that info the your property files before the jar task

task('addVersion') {
    doLast {
       //append the version here, see example
       file("src/main/resources/props.properties").append("version=$version")
    }
}
jar.dependsOn(addVersion)

这篇关于从build.gradle获取价值并编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆