Add pom.xml and example code as well as README.md

This commit is contained in:
Michael Hoess 2020-10-10 23:09:47 +02:00
parent 738eaa0b80
commit 1e87216797
5 changed files with 141 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target/

View File

@ -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 `<resources>` and `<plugins>` from this pom.xml to your pom.xml
- Add `<myapp.version>1.0</myapp.version>` to the `<properties>` 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

81
pom.xml Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.mh3000</groupId>
<artifactId>VersionSample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<myapp.version>0.1b0</myapp.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/templates</directory>
<includes>
<include>*.java</include>
</includes>
<filtering>true</filtering>
<targetPath>${project.build.directory}/generated-sources/java/net.mh3000</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.0.2</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
<execution>
<id>validate-the-git-infos</id>
<goals>
<goal>validateRevision</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<failOnNoGitDirectory>true</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>true</failOnUnableToExtractRepoInfo>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<commitIdGenerationMode>full</commitIdGenerationMode>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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}";
}