Skip to content

Commit

Permalink
expose jgitver metadatas as project extension properties
Browse files Browse the repository at this point in the history
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
  • Loading branch information
McFoggy committed Sep 14, 2017
1 parent 8f79de6 commit e5fc7fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/fr/brouillard/oss/gradle/plugins/JGitverPlugin.java
Original file line number Diff line number Diff line change
@@ -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<Project> {
@Override
Expand All @@ -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));
});
});
}
});
}
Expand Down

0 comments on commit e5fc7fe

Please sign in to comment.