-2

How to upload artifacts to nexus with auto generating the version by using jenkins shell? I am using this script and I want to generate the version automatically when artifact uploading.

 mvn deploy:deploy-file \
          -DgroupId= \
          -DartifactId= \
          -Dversion=  \
          -DgeneratePom= \
          -Dpackaging= \
          -DrepositoryId= \
          -Durl= \
          -Dfile= 
Janith
  • 217
  • 1
  • 6
  • 15
  • how your target version should be like ? can it be based on $BUILD_NUMBER variable ? – Reddysekhar Gaduputi Oct 26 '18 at 05:18
  • target version is 0.0.1-SNAPSHOT.jar. Can I take it from pom.xml? I am new to these things. 0.0.1-SNAPSHOT – Janith Oct 26 '18 at 08:13
  • yes, you can get it from pom.xml using the maven help plugin. run it in jenkins execute shell before deploy and it will return the version, and use it in deploy `mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version` – Reddysekhar Gaduputi Oct 26 '18 at 09:22
  • `mvn --quite org.apache.maven.plugins:maven-help-plugin:3.0.0:evaluate -Dexpression=project.version` – Reddysekhar Gaduputi Oct 26 '18 at 09:30
  • `mvn help:evaluate -Dexpression=project.version | grep -e '^[^\[]'` Given script works fine and this also works. Thank you for your support – Janith Oct 26 '18 at 10:53

1 Answers1

1

You can use ${env.BUILD_NUMBER} variable in your Jenkins pipeline to specify the artifact version.

To do this there are few options:

  1. If you want to publish artifacts to Nexus during mvn deploy, then you can use ${revision} parameter in pom.xml file. To do this, you need to put this variable into <version> tag. E.g., <version>1.0-${revision}</version> and then provide it to the maven command in Jenkins pipeline: mvn deploy -Drevision=${env.BUILD_NUMBER}. See this answer for details.
  2. Also you can use Nexus Jenkins Plugin to upload artifacts to nexus and specify the artifact version using ${env.BUILD_NUMBER} variable. See this for details.
biruk1230
  • 3,042
  • 4
  • 16
  • 29