Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get version attribute from a gradle build to be included in runtime Swing application

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.

like image 322
Domenic D. Avatar asked Oct 08 '15 15:10

Domenic D.


People also ask

How do I find my build Gradle version?

In Android Studio, go to File > Project Structure. Then select the "project" tab on the left. Your Gradle version will be displayed here.


2 Answers

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 } 
like image 61
Craig Trader Avatar answered Sep 28 '22 17:09

Craig Trader


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.

like image 32
Artur Biesiadowski Avatar answered Sep 28 '22 16:09

Artur Biesiadowski