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.
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);
The application is a plain java project, but I'm not opposed to adding groovy support it it will help.
In Android Studio, go to File > Project Structure. Then select the "project" tab on the left. Your Gradle version will be displayed here.
I like creating a properties file during the build. Here's a way to do that from Gradle directly:
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 }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With