Java 如何从 gradle 构建中获取版本属性以包含在运行时 Swing 应用程序中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33020069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get version attribute from a gradle build to be included in runtime Swing application
提问by Domenic D.
I have a simple parent project with modules/applications within it. My build tool of choice is gradle. The parent build.gradle
is defined below.
我有一个简单的父项目,其中包含模块/应用程序。我选择的构建工具是 gradle。父级build.gradle
定义如下。
apply plugin: 'groovy'
dependencies {
compile gradleApi()
compile localGroovy()
}
allprojects {
repositories {
mavenCentral()
}
version "0.1.0-SNAPSHOT"
}
What I would like to do is utilize the version attribute (0.1.0-SNAPSHOT) within my swing application. Specifically, I'd like it to display in the titlebar of the main JFrame. I expect to be able to do something like this.setTitle("My Application - v." + ???.version);
我想要做的是在我的 Swing 应用程序中使用 version 属性 (0.1.0-SNAPSHOT)。具体来说,我希望它显示在主 JFrame 的标题栏中。我希望能够做类似的事情this.setTitle("My Application - v." + ???.version);
The application is a plain java project, but I'm not opposed to adding groovy support it it will help.
该应用程序是一个普通的 java 项目,但我不反对添加 groovy 支持它会有所帮助。
采纳答案by Craig Trader
I like creating a properties file during the build. Here's a way to do that from Gradle directly:
我喜欢在构建过程中创建一个属性文件。这是直接从 Gradle 执行此操作的方法:
task createProperties(dependsOn: processResources) {
doLast {
new File("$buildDir/resources/main/version.properties").withWriter { w ->
Properties p = new Properties()
p['version'] = project.version.toString()
p.store w, null
}
}
}
classes {
dependsOn createProperties
}
回答by Artur Biesiadowski
You can always use brute force as somebody suggested and generate properties file during build. More elegant answer, which works only partially would be to use
您始终可以按照某人的建议使用蛮力并在构建期间生成属性文件。更优雅的答案,仅部分有效是使用
getClass().getPackage().getImplementationVersion()
Problem is that this will work only if you run your application from generated jar - if you run it directly from IDE/expanded classes, getPackage above will return null. It is good enough for many cases - just display 'DEVELOPMENT' if you run from IDE(geting null package) and will work for actual client deployments.
问题是只有当您从生成的 jar 运行您的应用程序时,这才有效 - 如果您直接从 IDE/扩展类运行它,上面的 getPackage 将返回 null。它在许多情况下已经足够了 - 如果您从 IDE 运行(获取空包)并且将适用于实际的客户端部署,则只需显示“DEVELOPMENT”。
回答by Opal
Better idea is to keep the project version in gradle.properties
file. All the properties from this file will be automatically loaded and can be used in build.gradle
script.
更好的主意是将项目版本保存在gradle.properties
文件中。此文件中的所有属性将自动加载并可在build.gradle
脚本中使用。
Then if you need the version in your swing application, add a version.properties
file under src/main/resources
folder and filter this file during application build, hereis a post that shows how it should be done.
然后,如果您需要在 Swing 应用程序中使用该版本,请version.properties
在src/main/resources
文件夹下添加一个文件并在应用程序构建期间过滤此文件,这里有一篇文章展示了它应该如何完成。
version.properties
will be included in the final jar, hence can be read and via ClassLoader
and properties from this file can be displayed in application.
version.properties
将包含在最终的 jar 中,因此可以读取,ClassLoader
并且可以在应用程序中显示此文件中的via和属性。
回答by Topera
Simpler and updated solution of @Craig Trader (ready for Gradle 4.0/5.0)
@Craig Trader 的更简单和更新的解决方案(为 Gradle 4.0/5.0 做好准备)
task createProperties {
doLast {
def version = project.version.toString()
def file = new File("$buildDir/resources/main/version.txt")
file.write(version)
}
}
war {
dependsOn createProperties
}