Skip to content

Instantly share code, notes, and snippets.

@lbruun
Last active April 1, 2025 08:16
Show Gist options
  • Select an option

  • Save lbruun/83b2b46c0092e6b7f1319b2dba1c1c1b to your computer and use it in GitHub Desktop.

Select an option

Save lbruun/83b2b46c0092e6b7f1319b2dba1c1c1b to your computer and use it in GitHub Desktop.

Revisions

  1. lbruun revised this gist Mar 31, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions LibraryInfo.java
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ private LibraryInfo() {
    // ...
    // </properties>
    //
    public static Optional<Instant> buildTime = getInstant("${buildTime}"); // must be in RFC-3339 format
    public static Optional<Instant> buildTime = getInstant("${buildTime}"); // input string must be in RFC-3339 format


    /**
    @@ -53,7 +53,7 @@ private LibraryInfo() {
    * <p>
    * Returns empty if unknown/unavailable.
    */
    public static Optional<Instant> vcsCommitTime = getInstant("${git.commit.time}"); // must be in RFC-3339 format
    public static Optional<Instant> vcsCommitTime = getInstant("${git.commit.time}"); // input string must be in RFC-3339 format

    /**
    * Commit identifier in the version control system (VCS) for the commit
  2. lbruun revised this gist Mar 31, 2025. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion LibraryInfo.java
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,8 @@ private LibraryInfo() {
    * Commit identifier in the version control system (VCS) for the commit
    * from which this library was. A commit identifier is typically an SHA-1 value.
    *
    * <p>Also sometimes referred to as a <i>commit reference</i>.
    * <p>
    * Also sometimes referred to as a <i>commit reference</i>.
    *
    * <p>
    * Returns empty string if unknown/unavailable, never {@code null}.
    @@ -73,6 +74,7 @@ private LibraryInfo() {
    *
    * <p>
    * For example: {@code 'https://github.com/foo/bar/tree/3.0.1'}
    *
    * <p>
    * Returns empty string if unknown/unavailable, never {@code null}.
    */
    @@ -88,17 +90,21 @@ private LibraryInfo() {
    /**
    * Name of license which applies to this library.
    * For example: {@code 'The Apache License, Version 2.0'}.
    *
    * <p>
    * Returns empty string if unknown or no license, never {@code null}.
    *
    * @see #licenseUrl
    */
    public static String licenseName = fixUnresolved("${project.licenses[0].name}");

    /**
    * URL from where the full license text can be retrieved for the license which applies to this library.
    * For example: {@code 'https://www.apache.org/licenses/LICENSE-2.0.txt'}.
    *
    * <p>
    * Returns empty string if unknown or no license, never {@code null}.
    *
    * @see #licenseName
    */
    public static String licenseUrl = fixUnresolved("${project.licenses[0].url}");
  3. lbruun created this gist Mar 31, 2025.
    129 changes: 129 additions & 0 deletions LibraryInfo.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@
    package org.example.serendipity.core;

    import java.time.Instant;
    import java.util.Optional;
    import java.util.regex.Pattern;

    /** Static information about this library. */
    public class LibraryInfo {

    // This class is meant to be used with the Template Maven Plugin
    // (https://www.mojohaus.org/templating-maven-plugin/)
    //
    // The plugin will replace the ${...} values at build time with values from the Maven session.

    private static final Pattern REPLACEMENT_VAR_PATTERN =
    Pattern.compile("\\$\\{[A-Za-z0-9.\\-_\\[\\]]+}");

    private LibraryInfo() {
    }

    /**
    * Version of library.
    *
    * <p>
    * Returns empty string if unknown, never {@code null}.
    */
    public static String version = fixUnresolved("${project.version}");

    /**
    * Time the library was build/compiled.
    *
    * <p>
    * Returns empty if no build time information is available.
    */
    // Note: This comes from 'maven.build.timestamp' but Maven does not make this property available for
    // property substition directly. Instead, we have to create a user property for it ('buildTime').
    // And then use that as an alias.
    //
    // Therefore, in the POM this is needed:
    //
    // <properties>
    // <buildTime>${maven.build.timestamp}</buildTime> <!-- allow 'maven.build.timestamp' to be available for property substitution -->
    // ...
    // </properties>
    //
    public static Optional<Instant> buildTime = getInstant("${buildTime}"); // must be in RFC-3339 format


    /**
    * Time of the commit to version control system (VCS) for the commit
    * from which this library was build.
    *
    * <p>
    * Returns empty if unknown/unavailable.
    */
    public static Optional<Instant> vcsCommitTime = getInstant("${git.commit.time}"); // must be in RFC-3339 format

    /**
    * Commit identifier in the version control system (VCS) for the commit
    * from which this library was. A commit identifier is typically an SHA-1 value.
    *
    * <p>Also sometimes referred to as a <i>commit reference</i>.
    *
    * <p>
    * Returns empty string if unknown/unavailable, never {@code null}.
    */
    public static String vcsCommitId = fixUnresolved("${git.commit.id}");

    /**
    * URL which allows to browse the exact version of the source code
    * that produced this library. This represents a view of the source code
    * at the time this library was build.
    *
    * <p>
    * For example: {@code 'https://github.com/foo/bar/tree/3.0.1'}
    * <p>
    * Returns empty string if unknown/unavailable, never {@code null}.
    */
    public static String vcsUrl = fixUnresolved("${scm.url}");

    /**
    * URL for the home page for the project which produced this library.
    * <p>
    * Returns empty string if unknown, never {@code null}.
    */
    public static String projectUrl = fixUnresolved("${project.url}");

    /**
    * Name of license which applies to this library.
    * For example: {@code 'The Apache License, Version 2.0'}.
    * <p>
    * Returns empty string if unknown or no license, never {@code null}.
    * @see #licenseUrl
    */
    public static String licenseName = fixUnresolved("${project.licenses[0].name}");

    /**
    * URL from where the full license text can be retrieved for the license which applies to this library.
    * For example: {@code 'https://www.apache.org/licenses/LICENSE-2.0.txt'}.
    * <p>
    * Returns empty string if unknown or no license, never {@code null}.
    * @see #licenseName
    */
    public static String licenseUrl = fixUnresolved("${project.licenses[0].url}");


    /**
    * Replaces unresolved placeholders with an empty string.
    *
    * Note that unresolved placeholders typically point to a flaw in your POM or similar.
    * Therefore, you may want to skip this logic and deal with it as a runtime failure
    * instead? Or simply not deal with it?
    */
    private static String fixUnresolved(String str) {
    if (str == null || str.isEmpty()) {
    return str;
    }
    return REPLACEMENT_VAR_PATTERN
    .matcher(str).replaceAll("");
    }

    private static Optional<Instant> getInstant(String str) {
    String s = fixUnresolved(str);
    if (s == null || (s.isEmpty())) {
    return Optional.empty();
    }
    return Optional.of(Instant.parse(s));
    }
    }