Build build in the Maven project

9

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

  

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

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

I did some research, but I did not succeed. Is there a plugin in maven that I put in the pom.xml file that has this feature?

    
asked by anonymous 22.01.2014 / 21:08

2 answers

5

There are several plugins to do what you want.

A quick Google search returned 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. Plugin configuration + 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 revision number will be available in prefix.revision

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

In both cases it's also worth setting 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>

Tip: Since you're taking time to automate the process, we recommend add the build version to Manifest . Placing the build number in the Manifest and storing a checksum of the generated artifact gives you confidence on the build version (just adding the version to the file name is good for cataloging, but does not guarantee much since the artifact can be renamed).

    
23.01.2014 / 16:22
3

The simplest and "manual" way

The simplest practice for getting an artifact with the VCS revision of the file name is put the desired revision in the version set to pom.xml . Although it is a "manual" procedure, this helps to maintain some consistency.

Versions Maven Plugin can help with this task a little.

Using the releases plugin

There is also the Maven Release Plugin that could be useful depending on how your process. It is able to create a tag in SVN and update the version of the poms.

Changing the "final" name of the artifact in Maven

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

By default, maven adds the pom version as follows:

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

Then you could define some other property, for example:

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

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

If you use a hook like the of this page

a>, then you will have the revision number in a property.

Finally, you just have to configure build parameters as explained this link . According to this answer this is perfectly possible.

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

23.01.2014 / 13:40