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

I am new to Gradle, so the answer may be simple, so I apologize if the answer is simple: I have a testing tool that I need to extract from it and compare it with the testing application version. However, the version of my tool is in my build.graddle file as

 version '1.0' 

I tried another way to access it (for example):

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 don’t see how I can get this information later (I mean, my tests are defined in src, and I will need to point to a file that does not exist at compilation - correct me if I am wrong here).

Any idea on how I could accomplish this task?

+4
1

, java build/classes ( , ), , . , , src/main/java, src/test/java/

, , . , . , , , :

1- src/main/resources (, app.properties), version, , APP_VERSION_TOKEN

version=%APP_VERSION_TOKEN%

2- Gradle processResources , :

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

3- , .

. src/test/resource , .

+4

Source: https://habr.com/ru/post/1663270/


All Articles