Skip to content

Commit 5dd41ca

Browse files
committed
Add docs for generating compile time constants
1 parent ad4d170 commit 5dd41ca

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

docs/using-the-plugin-in-more-depth.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Essentially every user can chose between the following alternatives:
55
* use plain resource filtering from maven
66
* use resource filtering from maven Maven in combination with Spring beans
77
* have the plugin generate a `git.properties` inside your artifact
8+
* Generate a Java Source File with Compile Time Constants
89

910
The following should give you a broad overview about the different cases.
1011

@@ -274,8 +275,69 @@ public GitRepositoryState(Properties properties)
274275
}
275276
```
276277

277-
Yet another way to use the plugin
278+
Generate A Java Source File with Compile Time Constants
278279
=================================
279280

280-
Rather than reading properties files at runtime or injecting with spring, you can filter a Java source file directly and place it into `src/main/java` with an ignore, or into generated sources directory within the target directory. This has some minor advantages and disadvantages, but is useful for avoiding runtime injection or lookup from properties files that might get lost during repackaging later if used within a library.
281+
Rather than reading properties files at runtime or injecting with spring, you can filter a Java source file directly into a `generated-sources` directory within the `target` directory. This is useful for avoiding runtime injection and/or lookup from properties files that might get lost during repackaging later if used within a library.
281282

283+
Add the [templating-maven-plugin](https://github.com/mojohaus/templating-maven-plugin) to your pom.xml:
284+
```xml
285+
<plugin>
286+
<groupId>org.codehaus.mojo</groupId>
287+
<artifactId>templating-maven-plugin</artifactId>
288+
<version>3.0.0</version>
289+
<executions>
290+
<execution>
291+
<goals>
292+
<goal>filter-sources</goal>
293+
</goals>
294+
<phase>generate-sources</phase>
295+
</execution>
296+
</executions>
297+
</plugin>
298+
```
299+
300+
Add a template .java source file to `src/main/java-templates`:
301+
```java
302+
package com.example.demo;
303+
304+
public interface Version {
305+
String TAGS = "${git.tags}";
306+
String BRANCH = "${git.branch}";
307+
String DIRTY = "${git.dirty}";
308+
String REMOTE_ORIGIN_URL = "${git.remote.origin.url}";
309+
310+
String COMMIT_ID = "${git.commit.id.full}";
311+
String COMMIT_ID_ABBREV = "${git.commit.id.abbrev}";
312+
String DESCRIBE = "${git.commit.id.describe}";
313+
String DESCRIBE_SHORT = "${git.commit.id.describe-short}";
314+
String COMMIT_USER_NAME = "${git.commit.user.name}";
315+
String COMMIT_USER_EMAIL = "${git.commit.user.email}";
316+
String COMMIT_MESSAGE_FULL = "${git.commit.message.full}";
317+
String COMMIT_MESSAGE_SHORT = "${git.commit.message.short}";
318+
String COMMIT_TIME = "${git.commit.time}";
319+
String CLOSEST_TAG_NAME = "${git.closest.tag.name}";
320+
String CLOSEST_TAG_COMMIT_COUNT = "${git.closest.tag.commit.count}";
321+
322+
String BUILD_USER_NAME = "${git.build.user.name}";
323+
String BUILD_USER_EMAIL = "${git.build.user.email}";
324+
String BUILD_TIME = "${git.build.time}";
325+
String BUILD_HOST = "${git.build.host}";
326+
String BUILD_VERSION = "${git.build.version}";
327+
String BUILD_NUMBER = "${git.build.number}";
328+
String BUILD_NUMBER_UNIQUE = "${git.build.number.unique}";
329+
}
330+
```
331+
Use the same package declaration as your program's entry point, presumably in `src/main/java`.
332+
This example would have a relative path of `src/main/java-templates/com/example/demo/Version.java`.
333+
334+
Use the version info as you would any other constant:
335+
```java
336+
package com.example.demo;
337+
338+
public class Main {
339+
public static void main(String[] args) {
340+
System.out.println("Version: " + Version.COMMIT_ID);
341+
}
342+
}
343+
```

0 commit comments

Comments
 (0)