From e5fc7fe1c8570017741c4f21b5fece0933ed4112 Mon Sep 17 00:00:00 2001 From: Matthieu Brouillard Date: Thu, 14 Sep 2017 11:09:38 +0200 Subject: [PATCH] expose jgitver metadatas as project extension properties jgitver metadatas are accessible using the syntax 'project.ext.metadata' where metadata is a jgitver metadata name in lowercase for example 'project.ext.git_sha1_full' fixes #6 --- README.md | 27 +++++++++++++++---- .../oss/gradle/plugins/JGitverPlugin.java | 17 ++++++++++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6de75e1..ecd5714 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,9 @@ Finally have a look at the [configuration](#configuration) paragraph to have ful ### Tasks -#### Since 0.2.0 +#### Version -The plugin automatically registers a task `version` which you can call to print out the calculated version of your project: +Since 0.2.0 the plugin automatically registers a task `version` which you can call to print out the calculated version of your project: ``` $ ./gradlew version @@ -61,9 +61,7 @@ BUILD SUCCESSFUL Total time: 5.769 secs ``` -#### Before 0.2.0 - -In order to know the current version of your project, just print out the version in a task looking like the following: +Before 0.2.0, in order to know the current version of your project, just print out the version in a task looking like the following: ``` task version { @@ -121,6 +119,25 @@ The plugin used [jgitver](https://github.com/McFoggy/jgitver) with the following - _useDistance_: `true` - _useGitCommitId_: `false` +### Metadatas + +Since `0.3.0`, jgitver [Metadatas](https://github.com/jgitver/jgitver/blob/master/src/main/java/fr/brouillard/oss/jgitver/metadata/Metadatas.java#L25) are exposed via gradle extension properties using the Metadata name in lower case. + +For example, one could enhance it's jar Manifest with the git commit id using: + +``` +apply plugin: 'java' +apply plugin: 'fr.brouillard.oss.gradle.jgitver' + +jar { + doFirst { + manifest { + manifest.attributes 'X-GIT-SHA1': "$project.ext.git_sha1_full" + } + } +} +``` + ## Local build & sample test project - `$ ./gradlew install` will install the current version inside the local maven repository diff --git a/src/main/java/fr/brouillard/oss/gradle/plugins/JGitverPlugin.java b/src/main/java/fr/brouillard/oss/gradle/plugins/JGitverPlugin.java index c4f251f..1d32f73 100644 --- a/src/main/java/fr/brouillard/oss/gradle/plugins/JGitverPlugin.java +++ b/src/main/java/fr/brouillard/oss/gradle/plugins/JGitverPlugin.java @@ -1,10 +1,14 @@ package fr.brouillard.oss.gradle.plugins; +import java.util.Arrays; +import java.util.Locale; + import org.gradle.api.Action; import org.gradle.api.Plugin; import org.gradle.api.Project; import fr.brouillard.oss.jgitver.GitVersionCalculator; +import fr.brouillard.oss.jgitver.metadata.Metadatas; public class JGitverPlugin implements Plugin { @Override @@ -23,16 +27,25 @@ public void execute(Project evaluatedProject) { jgitverConfiguration = new JGitverPluginExtension(); } - String gitCalculatedVersion = GitVersionCalculator.location(project.getRootDir()) + GitVersionCalculator versionCalculator = GitVersionCalculator.location(project.getRootDir()) .setMavenLike(jgitverConfiguration.mavenLike) .setAutoIncrementPatch(jgitverConfiguration.autoIncrementPatch) .setUseDistance(jgitverConfiguration.useDistance) .setUseGitCommitId(jgitverConfiguration.useGitCommitID) .setGitCommitIdLength(jgitverConfiguration.gitCommitIDLength) - .setNonQualifierBranches(jgitverConfiguration.nonQualifierBranches).getVersion(); + .setNonQualifierBranches(jgitverConfiguration.nonQualifierBranches); + String gitCalculatedVersion = versionCalculator.getVersion(); project.setVersion(gitCalculatedVersion); project.getAllprojects().forEach(subproject -> subproject.setVersion(gitCalculatedVersion)); + + Arrays.asList(Metadatas.values()).forEach(metadata -> { + versionCalculator.meta(metadata).ifPresent(metadataValue -> { + project.getExtensions().getExtraProperties().set(metadata.name().toLowerCase(Locale.ENGLISH), metadataValue); + project.getAllprojects().forEach( + subproject -> subproject.getExtensions().getExtraProperties().set(metadata.name().toLowerCase(Locale.ENGLISH), metadataValue)); + }); + }); } }); }