2

my jars are installed remotely by ssh, it is important that I know which version was installed. This is not accomplished via my current use of version numbers in the pom.xml since this number is manually updated. what I want to be able to do and cant find a decent plugin or one clear enought to tell me. Jenkins should build rev a build number in the pom file (e.g. 1.0.3. or 1.0.3- or 1.0.3_ or simply an additional attribute called build number) It would be good to have this build number as part of the jar name so that it is easily distinguished etc. etc.) Any ideas?

3 Answers 3

1

Starting with Maven 3.2.1 you can define properties in your version things like this: ${revision}, ${changelist}, and ${sha1}.

  <groupId>com.soebes.examples.j2ee</groupId>
  <artifactId>parent</artifactId>
  <version>1.0.4-${revision}-SNAPSHOT</version>

This is one solution.

You can also put all the needed information into a MANIFEST.MF file so you are always able to exactly say which version is installed. There you can either svn revision number or git sha1 and supplemental things like the build number of CI solution.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>${maven-jar-plugin.version}</version>
  <configuration>
    <archive>
      <addMavenDescriptor>true</addMavenDescriptor>
      <index>true</index>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
      </manifest>
      <manifestEntries>
        <artifactId>${project.artifactId}</artifactId>
        <groupId>${project.groupId}</groupId>
        <version>${project.version}</version>
        <revision>${svn.revision}</revision>
        <build-number>${BUILD_NUMBER}</build-number>
        <build-id>${BUILD_ID}</build-id>
        <build-time>${maven.build.timestamp}</build-time>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>
Sign up to request clarification or add additional context in comments.

5 Comments

Nice solution, I didn't know that is possible to use properties within the version number of the artifact.
this sees the simple approach, going to try it.. but I see you hardcoded a version... I will like for this to increment automatically as well.. 1.0.4 <--- I use github.. I see an option for svn will this work with github? and thanks
ok I have try this and Im getting "null" project-1.0.0.null.jar I made sure I had maven 3.2.1
You need to use the buildnumber-maven-plugin to extract the information like sha1 from git instead of svn.revision.
I was using your first option above with maven 3.2.1 not the second option.. was trying to keep it simple as much as possible.
1

This post describes a way to use the Jenkins internal build number in a Maven project. But there are some adaptations necessary to match your requirements.

  1. Define a property for the build number within the pom.xml
<properties>
    <build.number></build.number>
</properties>

The property should have an empty value default value. If build number is not specified (as Java system property), the name of the jar will be as normally ${artifactId}-${version}.jar e.g. myartifact-0.0.1-SNAPSHOT.jar

  1. Specify the final name of the build
<build>
  <finalName>${artifactId}-${version}${build.number}</finalName>
</build>

The final name will now include the build number as suffix. For example: myartifact-0.0.1-SNAPSHOT-153.jar (Please note, that the hyphen before the build number must be part of the system property - see next step)

  1. Use a Jenkins environment variable in the Maven command to set the system property build.number
 mvn clean install -Dbuild.number=-${BUILD_NUMBER}

Please note, that the hyphen before the build number!

3 Comments

The finalName is only for the artifacts in the target folder and does not help if you do an mvn install or mvn deploy.
I think this is exactly what the author of the post wants: "It would be good to have this build number as part of the jar name so that it is easily distinguished etc. etc.) Any ideas?" Since the jars are installed remotely via SSH, I guess that are the jars of the target folder. Or not?
Check my comment at the beginning of my answer (Maven 3.2.1 and properties). Those will work for install/deploy as well.
0

Although, Maven was not designed with this feature as one should not consider every build commited by VCS, because of stability of the build to release. But there is a work around with use of plugins. I will not suggest to version builds with build number but to use this practice with releases.

Addition in POM file:

Step 1: configure SCM as follows

'<scm>
<connection>scm:git/svn:repositary location</connection>
<url>similar</url>
</scm>
--
--
<build>
--
--
<plugins>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
        <version>1.9.2</version>
        <configuration>
          <connectionType>connection</connectionType>
        </configuration>
      </plugin>'

**Step 2: Use BuildNumber Plugin **

<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>
            <configuration>

        <format>{0,date,yyyy-MM-dd_HH-MM}</format>
          <items>
            <item>timestamp</item>
          </items>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

Step 3:

<finalName>webapp-demo-${buildNumber}</finalName>

${buildNumber} can be timestamp or changeset/revision. It totally depends on your need. Tutorial

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.