Inject gradle.properties Version into an Artifact and Expose it to version Command: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Gradle Programming =Overview= This is a recipe...")
 
Line 23: Line 23:
     ...
     ...
}
}
</syntaxhighlight>
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:
<syntaxhighlight lang='java'>
InputStream is = MyClass.class.getClassLoader().getResourceAsStream(VERSION);
...
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:42, 22 March 2019

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.

task stageVersionInformation {
    mustRunAfter 'testClasses'
    doFirst {
        Files.write(new File(project.buildDir, "resources/main/VERSION").toPath(), version.toString().getBytes());
    }
}

test {
    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);
...