Skip to content

Instantly share code, notes, and snippets.

@nblair
Last active October 17, 2016 15:23
Show Gist options
  • Save nblair/23a3910a28fe21308be6397ff0f110c1 to your computer and use it in GitHub Desktop.
Save nblair/23a3910a28fe21308be6397ff0f110c1 to your computer and use it in GitHub Desktop.
Groovy script to help Jenkins identify the version of the artifact it just built.
/**
* Reads the version of the artifact from the MANIFEST.
* Depends on actually setting version in MANIFEST; Gradle snippet below:
<pre>
jar {
manifest {
attributes(
'Implementation-Title': project.name,
'Implementation-Version': project.version
)
}
}
</pre>
*/
import hudson.model.*
import java.util.jar.*
def build = Thread.currentThread().executable
// the path to your manifest file will vary - the example below is for a gradle build that produces a jar
// for maven, it would be something like target/classes/META-INF/MANIFEST.MF
Manifest manifest = new Manifest(
new FileInputStream(
new File("${build.workspace}/build/tmp/jar/MANIFEST.MF")));
def version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION)
println "Artifact version is $version"
build.addAction(
new ParametersAction([
new StringParameterValue("ARTIFACT_VERSION", version),
])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment