Generating unique builds

As we have seen, we use a SNAPSHOT version to specify that the project is under development. In the course of development, we will create several builds for the project. In many situations, it will be useful to distinguish one such build from another. One could be when we use continuous integration. Another would be when a tester needs to log defects against a build.

It would be nice if there was a way to generate a unique build number to identify a build in the case of SNAPSHOT versions.

How to do it...

  1. Open the project for which you want to have a build number (project-with-build-number).
  2. Add the following plugin configuration:
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
              <execution>
                <phase>validate</phase>
                <goals>
                  <goal>create</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
               <shortRevisionLength>5</shortRevisionLength>
            </configuration>
          </plugin>
  3. Add the following to use the unique build number created:
    <finalName>${project.artifactId}-${project.version}-r${buildNumber}</finalName>
  4. Add the SCM configuration for the project:
    <scm>
         <developerConnection>scm:git:https://bitbucket.org/maruhgar/apache-maven-cookbook</developerConnection>
          <url>https://bitbucket.org/maruhgar/apache-maven-cookbook</url>
      </scm>
  5. Build the project:
    mvn clean package
    
  6. Observe the output:
    [INFO] --- buildnumber-maven-plugin:1.3:create (default) @ project-with-build-nu
    mber ---
    [INFO] ShortRevision tag detected. The value is '5'.
    [INFO] Executing: cmd.exe /X /C "git rev-parse --verify --short=5 HEAD"
    [INFO] Working directory: C:projectsapache-maven-cookbookproject-with-build-n
    umber
    [INFO] Storing buildNumber: 0950d at timestamp: 1421244408851
    [INFO] Storing buildScmBranch: master
    ...
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project-with-build-number --
    -
    [INFO] Building jar: C:projectsapache-maven-cookbookproject-with-build-number
    	argetproject-with-build-number-1.0-SNAPSHOT-r0950d.jar
    

How it works...

The Maven Build Number plugin provides three ways to generate a unique number, namely by using SCM, a sequential build number, or a timestamp.

In the preceding example, we used SCM as it is easy to map the build against the corresponding SCM version. We used git and specified the SCM details in the SCM tag of the pom file.

We also specified to the Maven Build Number plugin to use five characters and create the short revision, as a typical git revision is a long hash value. We also configured the plugin to run during the validation phase of the lifecycle.

We used the generated Build Number in the name of the generated artifact, by appending it along with the version number.

Now, each time a new check-in is done and the build is completed, an artifact with a unique name is generated. Based on the requirement, each such artifact can be archived or traced to a corresponding source.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.34.62