Inject gradle.properties Version into an Artifact and Expose it to version Command: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Internal= | =Internal= | ||
* [[ | * [[Gradle_Programming_TODEPLETE#Inject_gradle.properties_Version_into_an_Artifact_and_Expose_it_to_.27version.27_Command|Gradle Programming]] | ||
=Overview= | =Overview= | ||
Line 9: | Line 9: | ||
The steps are: | The steps are: | ||
1. Write the version information, as declared in [[gradle.properties]], into a VERSION file in build/resources/main/VERSION. We need to do it at the right time, so it will not overwritten by a task like "[[Gradle_Java_Plugin#processResources|processResources]]", but it will be available when tests are run. | 1. Write the version information, as declared in [[gradle.properties]], into a VERSION file in build/resources/main/VERSION. We need to do it at the right time, so it will not overwritten by a task like "[[Gradle_Java_Plugin#processResources|processResources]]", but it will be available when tests are run, and also before JAR is build, so it can be included within the JAR. | ||
<syntaxhighlight lang='groovy'> | <syntaxhighlight lang='groovy'> | ||
Line 20: | Line 20: | ||
test { | test { | ||
dependsOn 'stageVersionInformation' | |||
... | |||
} | } | ||
jar { | |||
dependsOn 'stageVersionInformation' | |||
... | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 00:08, 9 November 2020
Internal
Overview
This is a recipe to automatically expose a project version information, normally maintained in gradle.properties, to the runtime, by embedding it into the artifact.
The steps are:
1. Write the version information, as declared in gradle.properties, into a VERSION file in build/resources/main/VERSION. We need to do it at the right time, so it will not overwritten by a task like "processResources", but it will be available when tests are run, and also before JAR is build, so it can be included within the JAR.
task stageVersionInformation {
mustRunAfter 'testClasses'
doFirst {
Files.write(new File(project.buildDir, "resources/main/VERSION").toPath(), version.toString().getBytes());
}
}
test {
dependsOn 'stageVersionInformation'
...
}
jar {
dependsOn 'stageVersionInformation'
...
}
This will cause the VERSION file to be embedded within the JAR, in the root of the JAR.
2. Read it from the Java code:
InputStream is = MyClass.class.getClassLoader().getResourceAsStream(VERSION);
...