Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle project version outside build.gradle at runtime

Tags:

java

maven

gradle

I would like to obtain a Gradle project version in my Java code.

In Maven, it can be done this way:

version.properties:

version=${project.version}    

which resolves to:

version=1.0-SNAPSHOT    

in jar/war if we add this to pom.xml:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

Is there any convenient way to achieve similar result in Gradle? Or should I manually write a task that produces a property file with the project version and then read it in my code?

like image 900
trzeciakp Avatar asked Jan 09 '23 06:01

trzeciakp


1 Answers

src/main/resources/version.properties:

 version=${projectVersion}

build.gradle:

apply plugin: "java"

processResources {
    expand projectVersion: project.version
} 
like image 173
Peter Niederwieser Avatar answered Jan 18 '23 21:01

Peter Niederwieser