Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access version of my project in build.gradle from a Java class

Tags:

java

gradle

I'm quite new to Gradle so the answer might be simple, so I apologize if the answer is simple: I have a testing tool that needs to fetch it's version and compare it to the version of the application it is testing. However , the version of my tool is in my build.graddle as

 version '1.0' 

I tried different way to access it ( such as) :

task generateSources {
File outDir
outDir = file("$buildDir/classes/test/tests/Version.java")
doFirst {
    outDir.exists() || outDir.mkdirs()
    new File(outDir).write("public class Version { public static final String VERSION = \"$project.version\"; }")
 }
}
compileJava.dependsOn generateSources
compileJava.source generateSources.outputs.files, sourceSets.main.java

I found this piece of code to output the version to another java file, but I fail to see how I'd be able to retrieve that info afterwards ( I mean, my tests are defined in src and I would need to point to a file that doesn't exist at compilation -- correct me if I'm wrong here).

Any idea on how I could accomplish this task?

like image 878
MmMm Avatar asked Dec 08 '16 20:12

MmMm


People also ask

How do I find my Gradle project version?

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

How do I mention Java version in build Gradle?

Right click on the deploy or any other task and select "Open Gradle Run Configuration..." Then navigate to "Java Home" and paste your desired java path. Please note that, bin will be added by the gradle task itself.

How do I check my Java version in Gradle?

Gradle doesn't expose any specific API for this but you can simply check the 'java. version' system property from the settings. gradle file. We actually do, it's ['JavaVersion'](http://www.gradle.org/docs/current/javadoc/org/gradle/api/JavaVersion.html).


1 Answers

First of all, you are trying to create java source file in your build/classes (it should contain compiled classes, not sources) directory, but you have to do it in your sources, otherwise it won't be compiled. And if you need this new class to be vailable not for tests, then use src/main/java, not src/test/java/

But anyway, I suppose for your case it's much easier to use some properties file for that and replace some token within it during build. That will allow you to make some static logic to get this property value and use it yet before running the build. So all you need is:

1- to have some properties file in your resources src/main/resources (for example app.properties), where should version variable be stored, with it's value like APP_VERSION_TOKEN

version=%APP_VERSION_TOKEN%

2- configure you Gradle processResources to replace tokens, something like this:

processResources {
    filesMatching('**/app.properties') {
        filter {
            it.replace('%APP_VERSION_TOKEN%', version)
        }
    }
}

3- make some method to read this file and return the value of the property and use it where you need.

And that's all. For unit tests you can have another file with the same name under src/test/resource with the unchanging value you need for testing.

like image 179
Stanislav Avatar answered Oct 17 '22 05:10

Stanislav