2

When project is built with Maven it is possible to get implementation version like this. Is it possible to do that or something with Gradle?

I moved from Maven to Gradle with my app. With Maven build I was able to display application version like Application vX.X.X where X.X.X was retrieved from implementation version. It that possible with Gradle?
Basically I want that version was resolved automatically.

Community
  • 1
  • 1
Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
  • Could you please elaborate on what you want to achieve? – JB Nizet Feb 01 '14 at 14:38
  • Define "implementation version". Show code if you can't define it. – JB Nizet Feb 01 '14 at 14:45
  • Version described in project's `pom.xml` like 1.5.4. And it's retrieval like described in linked question. – Martynas Jurkus Feb 01 '14 at 14:49
  • 3
    So, you would like to write the project name and version in the manifest file of the jar generated by Gradle, is that what you're asking? If so, see http://www.gradle.org/docs/current/userguide/java_plugin.html#N12907 – JB Nizet Feb 01 '14 at 15:02
  • I second the link from @JBNizet. groupid you'll have to create yourself in parent build.gradle or subproject. ArtifactId=name, or something you yourself define. Apart from that I like this presentation regarding Gradle and devops: https://speakerdeck.com/bmuschko/building-a-continuous-delivery-pipeline-with-gradle-and-jenkins – judoole Feb 01 '14 at 19:02
  • If someone would put that up as an answer I would gladly accept it - cause it worked just fine. – Martynas Jurkus Feb 01 '14 at 22:56

2 Answers2

10
jar {
    manifest {
        attributes('Main-Class':             'Your main class',
                   'Implementation-Title':   'Your title',
                   'Implementation-Version': 'You version')
    }
}

or if you use shadowJar

shadowJar {
        manifest {
            attributes 'Main-Class':             'Your main class'
            attributes 'Implementation-Title':   'Your title'
            attributes 'Implementation-Version': 'You version'
        }
    }

Then in the java sources you could use:

getClass().getPackage().getImplementationVersion()
getClass().getPackage().getImplementationTitle()

See more info in the Gradle doc: https://docs.gradle.org/current/userguide/java_plugin.html#sub:manifest

radistao
  • 14,889
  • 11
  • 66
  • 92
-2

As @JB Nizet suggested solution can be found here.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
  • [THIS LINK](https://docs.gradle.org/current/userguide/java_plugin.html#sub:manifest) has an updated anchor that should work, and looks a little more durable. If the anchor breaks, info is in the Manifest subsection under the Jar task. – danwyand Mar 07 '17 at 18:10