gradlejarversionnebula

WIthin nebula/gradle, how can I inject the version being released into the jar being published?


We have a tool that runs from the command line. One of the commands is -version.

Before we converted to the nebula release plugin, the version was in the gradle.properties file, and as part of the build we copied it from there to a src/main/resources/version.txt file, that was later read by the tool to output the version.

But now the version is never in a file that's checked into git. Instead, it is only known during the nebula release process.

We want to obtain that version during the nebula release process and inject it into the jar that nebula is about to publish. For example, it could be added to the manifest.

We've tried to figure out how to do this, but don't see any examples online, and nothing about it in the documentation.


Solution

  • Simply create a task that caches the version that is dynamically inferred by Nebula.

    Since you originally copied/created src/main/resources/version.txt, we'll use that that model our task.

    Assuming a simple/standard Java project, using the Kotlin DSL:

    val cacheNebulaVersion by tasks.registering {
        mustRunAfter(tasks.named("release"))
        doLast {
            val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer
            sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).output.resourcesDir?.let {
    
                // If there are not existing resources in your project then you must create
                // the resources dir otherwise a FileNotFoundException will be thrown.
                if (!it.exists()) {
                    it.mkdirs()
                }
    
                File(it, "version.txt").printWriter().use { out ->
                    out.println(project.version)
                }
            }
        }
    }
    

    When I invoke ./gradlew clean build snapshot cacheNebulaVersion, the version produced by Nebula is cached/created at src/main/resources/version.txt in the build output. The task above does not bundle it with the jar.

    Hopefully that gives you an idea what to do.

    enter image description here