Fork me on GitHub

Using the Maven Injection Plugin

  1. Add the plugin to your Maven build
  2. Configure injections

    For usage details see the inject goal description.

    Hint: Inject into accessor methods instead of class attributes or constants to avoid surprises (compiler inlining etc.) Hint: A typical singleton could use a Java Enum, see BuildInfo example.

  3. Hook plugin into lifecycle after compilation

    The plugin runs after compilation in the *process-classes* phase.

  4. Add build info plugin for injecting the SCM version and branch, as an example
    <build>
      ...
      <plugins>
        <plugin>
          <groupId>de.m3y.maven</groupId> <!-- (1) Add the plugin to your Maven build -->
          <artifactId>inject-maven-plugin</artifactId>
          <version>1.7</version>
          <configuration> <!-- (2) Configure injections -->
            <injections>
              <injection>
                <value>1.7</value> <!-- Inject Maven project version -->
                <pointCuts>
                  <pointCut>de.m3y.maven.inject.it.ExampleInjection.getVersion</pointCut> <!-- ... into accessor -->
                  <pointCut>...</pointCut> <!-- Could do multi injection -->
                </pointCuts>
              </injection>
              <injection>
                <value>${maven.build.timestamp}</value>
                <pointCut>de.m3y.maven.inject.it.ExampleInjection.getBuildTimeStamp</pointCut>
              </injection>
              <injection>
                <value>${buildNumber}</value> <!-- Provided by buildnumber-maven-plugin -->
                <pointCut>de.m3y.maven.inject.it.ExampleInjection.getScmVersion</pointCut>
              </injection>
              <injection>
                <value>${scmBranch}</value> <!-- Provided by buildnumber-maven-plugin -->
                <pointCut>de.m3y.maven.inject.it.ExampleInjection.getScmBranch</pointCut>
              </injection>
            </injections>
          </configuration>
          <executions>
            <execution> <!-- (3) Hook plugin into lifecycle after compilation-->
              <phase>process-classes</phase>
              <goals>
                <goal>inject</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin> <!-- (4) Required for SCM properties buildNumber and scmBranch
                          See https://github.com/mojohaus/mojo-parent -->
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>buildnumber-maven-plugin</artifactId>
          <version>1.4</version>
          <executions>
            <execution>
              <phase>validate</phase>
              <goals>
                <goal>create</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <doCheck>false</doCheck>
            <doUpdate>false</doUpdate>
          </configuration>
        </plugin>
      </plugins>
      ...
    </build>