Developers, Developers, Developers! Maksim Sorokin IT Blog

18Aug/100

Providing Build Information Automatically for Every Maven Project

This article describes a possibility to automatically inject properties file with build information into any Maven module.

Sometimes you need to provide version and build information in "About" dialog of the application. You can easily create a simple properties file in your resources folder and apply filtering on it, where build version and timestamp would be automatically replaced on each maven build. But what to do when you have dozen of such project? Is there a way to do this automatically? Indeed, there is.

The idea is simple. We start by creating a Maven "template" project which would just hold a general build properties file. Then we create a parent project which would know how to intervene into a build cycle and inject build properties file. Then we simply inherit this parent project in all other projects which require such automatic build numbering injection.

As an example we will only cover project version filtering. Build timestamp can be added easily.

We start by creating the Maven "template" project, which would hold the build properties file. We create a following build.properties file in src/main/resources:

project.version=${project.version}

Now, we need to create a parent project, which would know how to intervene into a build lifecycle. But first we need to find out when the resource are being filtered in Maven lifecycle. In Official Maven Documentation in "Default Lifecycle" section we see, that this is happening during process-sources goal. So we need to inject build.properties just before that.

In our parent project pom we define the following:

...
  <build>
    ...
    <plugins>
      ...

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>unpack</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>company</groupId>
                  <artifactId>TEMPLATE-PROJECT-ARTIFACT-NAME</artifactId>
                  <version>1.0.0-SNAPSHOT</version>
                  <includes>build.properties</includes>
                  <outputDirectory>${project.build.directory}/buildInfo</outputDirectory>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

    <resources>
	  ...
      <resource>
        <directory>target/buildInfo</directory>
        <filtering>true</filtering>
      </resource>
	  ...
    </resources>
  </build>
...

What it does, is intervenes into build lifecycle in generate-resources phase, just before when resources are filtered. It extracts build.properties file from Maven "template" project into buildInfo folder. Then this build.properties file is being filtered and
included into deliverable.
That is it! If this parent project is inherited in other projects, build.properties file will be automatically filtered and added during Maven build.

Furthermore, we can create another project, which would know how to parse this build file. And include this parsing project as a dependency where it is needed. So we would not need to write the same build properties file parsing logic again and again.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


Trackbacks are disabled.