Get git information in your CDI application easily thanks to DeltaSpike and git-commit-id-plugin


Having SCM information in the project is really interesting to dump them at startup for instance.

There are several ways to do it.

One easy is to use templating-maven-plugin and filter a .java with these information but it has several drawbacks:

  • IDE can be lost with these files (not compiling for instance)
  • You link sources and build tool
  • You didn’t solve how to get all the information (need another plugin)

An alternative is to use git-commit-id-plugin. This plugin fill several properties:

git.commit.id.abbrev=170c736
git.commit.user.email=rmannibucau@gmail.com
git.commit.message.full=initial import\n
git.commit.id=170c73611b67fc277e2a493b6a30c8945eb62bc5
git.commit.id.describe-short=170c736
git.commit.message.short=initial import
git.commit.user.name=Romain Manni-Bucau
git.build.user.name=Romain Manni-Bucau
git.commit.id.describe=170c736
git.build.user.email=rmannibucau@gmail.com
git.branch=master
git.commit.time=08.07.2014 @ 21\:10\:47 CEST
git.build.time=08.07.2014 @ 21\:29\:34 CEST
git.remote.origin.url=https\://github.com/rmannibucau/git-info-cdi.git

By default these information are not dumped anywhere. To get it dumped you need to configure a bit the plugin:

<plugin>
  <groupId>pl.project13.maven</groupId>
  <artifactId>git-commit-id-plugin</artifactId>
  <version>2.1.10</version>
  <executions>
    <execution>
      <id>git-info</id>
      <goals>
        <goal>revision</goal>
      </goals>
      <configuration>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>target/classes/git.properties</generateGitPropertiesFilename>
      </configuration>
    </execution>
  </executions>
</plugin>

Generating git.properties in target/classes allows to get it in the classloader of the application which can be very interesting!

If you use CDI you surely use Apache DeltaSpike. If you depend at least on core modules (api and impl) which is likely the case you can just add a single class to make these properties available as injection value:

import org.apache.deltaspike.core.api.config.PropertyFileConfig;

public class GitPropertyFile implements PropertyFileConfig {
    @Override
    public String getPropertyFileName() {
        return "git.properties";
    }
}

Then you can just get git metadata injected as usual:

@Singleton
@Startup
public class GitMetaData {
    @Inject
    @ConfigProperty(name = "git.remote.origin.url")
    private String gitUrl;

    @Inject
    @ConfigProperty(name = "git.commit.id")
    private String gitCommitId;

    @Inject
    @ConfigProperty(name = "git.branch")
    private String gitBranch;

    @PostConstruct
    public void dump() {
        // do dump of metadata
    }
}

Here is a full sample project: https://github.com/rmannibucau/git-info-cdi/blob/master/pom.xml

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s