Build generation in the Maven project

Asked

Viewed 3,152 times

9

I would like to generate a Java build using Maven, where the generated file name . jar was the same number as the last revision of the SVN repository.

Example: My last build generated has the following numbering: 1.0.0.51. And throughout the month of January there were several commits in the repository and now I want to generate a new build with the latest revision of the repository, which in this case would be 1.0.0.201.

How do I do that? I use continuous Jenkins integration and would like them to be integrated.

I did some research, but I was not successful. Is there any plugin in Maven that I put in the file pom.xml that has this feature?

  • Jenkins already comes with Maven integration in the "basic package". No need to change anything in pom.xml to configure Jenkins.

  • Personally, I think this is a bad idea, since it will break the Maven versioning system.

2 answers

5


There are several plugins to do what you want.

A quick Google search returned to me:

Build Number Maven Plugin

Just set up the plugin:

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
      <execution>
        <phase>validate</phase>
        <goals>
          <goal>create</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <doCheck>true</doCheck>
      <doUpdate>true</doUpdate>
    </configuration>
  </plugin>
</plugins>

That will feed the variable buildNumber.

<build>
   <finalName>${project.artifactId}-${project.version}-r${buildNumber}</finalName>
</build>

Maven SVN Revision Number Plugin

Same thing. Configuration of plugin + prefix:

<plugin>
   <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
   <artifactId>svn-revision-number-maven-plugin</artifactId>
   <version>1.13</version> 
   <executions>
      <execution>
         <goals>
            <goal>revision</goal>
         </goals>
       </execution>
    </executions>
    <configuration>
       <entries>
          <entry>
             <prefix>prefix</prefix>
          </entry>
       </entries>
    </configuration>
</plugin>

And the review number will be available at prefix.revision

<build>
   <finalName>${project.artifactId}-${project.version}-r${prefix.revision}</finalName>
</build>

In both cases it is worth also configuring the SCM:

<scm>
    <connection>scm:svn:https://servidor/projeto/trunk</connection>
    <developerConnection>scm:svn:https://servidor/projeto/trunk</developerConnection>
    <tag>HEAD</tag>
    <url>https://servidor/projeto/trunk</url>
</scm>

Suggestion: as you are taking time to automate the process, it is recommended add the version of build to Manifest. Putting the build number in the Manifest and storing a checksum of the generated artifact you gain confidence about the version of build (just adding the version to the file name is good for cataloging, but it does not guarantee much since the artifact can be renamed).

  • Anthony, it was bad! I read the wrong plugin name.

3

The simplest and "manual form"

The simplest practice to get an artifact with the review of VCS in the file name is to put the desired revision in the version configured in the pom.xml. Although it is a "manual" procedure, it helps to maintain a certain consistency.

The Versions Maven Plugin can help a little in this task.

Using the plugin for releases

There is still the Maven Release Plugin which could be useful depending on what your process is like. It is able to create a tag in SVN and update the poms version.

Changing the "final" name of the artifact in Maven

In Maven, you can set the tag <finalName> in his pom.xml so that, after the build, the final artifact has the name you want. See the documentation here.

By default, Maven adds the pom version as follows:

<finalName>${artifactId}-${version}</finalName>

So you could define any other property, for example:

<finalName>${artifactId}-${revisao_svn}</finalName>

And Jenkins could be configured to pass the parameter ${revisao_svn} for the Maven.

If you use a hook (hook) like the of this page, then you’ll have the revision number on a property.

Finally, simply configure the parameters of the build as explained at this link. According to this answer that is perfectly possible.


Note: currently I do not have an environment to test all this, so unless someone gives a more detailed solution, you will have to unravel the details of the procedure.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.