首页 > 解决方案 > 有没有办法从 kotlin 项目的 build.gradle 获取版本?

问题描述

我想在我在 build.gradle 中设置的项目中获取我的项目版本。有什么方法可以在我的项目中获取版本,因为我需要在我的软件中显示版本并用于比较更新信息,这样我每次发布更新时都不需要更改两次。有什么办法可以做到吗?

group 'ProjectName.group'
version "ProjectVersion" 

标签: intellij-ideakotlinbuild.gradle

解决方案


您通常通过加载属性文件并配置 gradle 来过滤processResources任务中的属性文件来做到这一点。

例子:

构建.gradle:

version = '1.5.0'

processResources {
    def properties = ['version': project.version]
    inputs.properties(properties)
    filesMatching('version.properties') {
        expand(properties)
    }
}

版本属性:

version=${version}

应用程序.java:

public static void main(String[] args) throws IOException {
    Properties properties = new Properties();
    properties.load(App.class.getResourceAsStream("/version.properties"));
    System.out.println(properties.getProperty("version"));
}

推荐阅读