diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/README.md b/README.md index e69de29..fdb8119 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,20 @@ +### Sample on how to generate a comprehensive version info including Git-commit-ids and build-times. + +Versioninfo is generated in the output-jar and also as Java-class for comsumption e.g. +in user-output. + +- Ensure your project is in a Git-repositiory (with at least one commit) +- Put `BuildVersion.java` in a new folder `templates` under the `src/main“-directory and modify + as needed. +- Add `` and `` from this pom.xml to your pom.xml +- Add `1.0` to the `` section. This will be used + as main "end-user" version . +- Run `mvn resources:resources` to generate the final buildversion +- Now you can add something like `System.out.println(BuildVersion.BUILD);` to your code + to show the version to the user. + +With this also a git.properties file will be included in the output-JAR. + +Note: In `BuildVersion.java` don't put `@`chars before a template-field, otherwise it won't be processed correctly! + +See [git-commit-id-plugin](https://github.com/git-commit-id/git-commit-id-maven-plugin/blob/master/maven/docs/using-the-plugin.md) for more diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..07ea630 --- /dev/null +++ b/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + net.mh3000 + VersionSample + 1.0-SNAPSHOT + jar + + + UTF-8 + 15 + 15 + 0.1b0 + + + + + src/main/templates + + *.java + + true + ${project.build.directory}/generated-sources/java/net.mh3000 + + + + + + pl.project13.maven + git-commit-id-plugin + 4.0.2 + + + get-the-git-infos + + revision + + initialize + + + validate-the-git-infos + + validateRevision + + package + + + + + ${project.basedir}/.git + true + true + true + full + false + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + + add-source + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources + + + + + + + + \ No newline at end of file diff --git a/src/main/java/net/mh3000/versionsample/VersionSample.java b/src/main/java/net/mh3000/versionsample/VersionSample.java new file mode 100644 index 0000000..89036f0 --- /dev/null +++ b/src/main/java/net/mh3000/versionsample/VersionSample.java @@ -0,0 +1,15 @@ +package net.mh3000.versionsample; +import net.mh3000.BuildVersion; + +/** + * + * @author mh + */ +public class VersionSample { + + public static void main(String[] args) { + System.out.println("Hello word"); + System.out.println("Build : "+ BuildVersion.BUILD); + System.out.println("Version: "+ BuildVersion.VERSION); + } +} diff --git a/src/main/templates/BuildVersion.java b/src/main/templates/BuildVersion.java new file mode 100644 index 0000000..12c5bb4 --- /dev/null +++ b/src/main/templates/BuildVersion.java @@ -0,0 +1,24 @@ +package net.mh3000; + +public final class BuildVersion { + + + + public static String BUILD = + "${myapp.version}@" + + "${git.commit.id.abbrev}@" + + "${git.build.user.name}@" + + "${git.build.host}/" + + "${git.build.time}"; + + public static String VERSION = "false".equals("${git.dirty}") ? + "${myapp.version}@" + + "${git.commit.id.abbrev}/" + + "${git.build.time}" + : + "${myapp.version}@" + + "${git.commit.id.abbrev}(drty)/" + + "${git.build.time}"; + + +} \ No newline at end of file