3

My question is about saving artifacts into a repository. Especially, I am trying to upload into the Nexus Repository artifacts and release versions after the execution of a build pipeline for a Maven project (through Jenkins).

The only way that I want to do so, is just by using a pipeline written in Groovy so to integrate with Jenkins.

Note: I want the artifact version number to be always the same and the version number to change dynamically (not manually).

Is there a command or code generally which enables me to do that?

dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34

3 Answers3

4

You are on the wrong level, this should happen in maven. In pom.xml you need. (more here)

<distributionManagement>
   <snapshotRepository>
      <id>nexus-snapshots</id>
      <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
   </snapshotRepository>
</distributionManagement>

and then in the plugins section

<plugin>
   <artifactId>maven-deploy-plugin</artifactId>
   <version>2.8.2</version>
   <executions>
      <execution>
         <id>default-deploy</id>
         <phase>deploy</phase>
         <goals>
            <goal>deploy</goal>
         </goals>
      </execution>
   </executions>
</plugin>

and you should be able to just do mvn clean deploy from your pipeline.

EDIT There is another way with Nexus Artifact Uploader plugin

  nexusArtifactUploader {
    nexusVersion('nexus2')
    protocol('http')
    nexusUrl('localhost:8080/nexus')
    groupId('sp.sd')
    version("2.4.${env.BUILD_NUMBER}")
    repository('NexusArtifactUploader')
    credentialsId('44620c50-1589-4617-a677-7563985e46e1')
    artifact {
        artifactId('nexus-artifact-uploader')
        type('jar')
        classifier('debug')
        file('nexus-artifact-uploader.jar')
    }
    artifact {
        artifactId('nexus-artifact-uploader')
        type('hpi')
        classifier('debug')
        file('nexus-artifact-uploader.hpi')
    }
  }
hakamairi
  • 4,464
  • 4
  • 30
  • 53
  • Hmm the thing is that I want Jenkins to execute this action. I don't want it from Maven. There must be a solution or no? Your solution is working but not for my issue. I am trying to solve it 3 days now... – dpapadopoulos Jan 22 '19 at 07:28
  • I just tried it. I can upload versions but I can't do this for artifacts. I mean that Nexus is accepting the new version that I am uploading, but does not accept the artifact version (which is the same number every time). The solution is near I think. – dpapadopoulos Jan 22 '19 at 07:41
  • 1
    You should increase the version number from it on every artifact upload. – hakamairi Jan 22 '19 at 07:42
  • 1
    Updated my answer with version("2.4.${env.BUILD_NUMBER}"), you really need to take care of that yourself. That's one of the ways. Also we are very far from the original question now. – hakamairi Jan 22 '19 at 08:06
  • I edited my question then so not to go out of topic. Your new answer is working too but here is still this "2.4" ahead. I will wait to see if there is a way to read the number of the version from a file and to pull it inside a bracket. – dpapadopoulos Jan 22 '19 at 08:09
  • 1
    I think we are done here, the original question was answered, you are just looking for someone to code for you. I'm going to report that. – hakamairi Jan 22 '19 at 08:12
  • Feel free to do this. I am not waiting for someone to give the whole solution. But I wrote that I want it in Groovy (not theoretically). When you want to do something automatically you must not include manual processes. If the question is not going to be answered with an automatic way then I am not going to close it. – dpapadopoulos Jan 22 '19 at 08:16
  • 1
    I tried my solution again but this time was having an issue. Yours was the best one for the new task. My solution was working just for the previous issue that I had. Sorry for not making your answer as the best days ago. I fixed it now :) Once sorrier. I had no idea. I also edited the question so to be clear and for others. Hope that meets the requirements now. – dpapadopoulos Feb 20 '19 at 13:28
  • 1
    It would be unfair if I wouldn't. ;) – dpapadopoulos Feb 20 '19 at 13:38
3

As @hakamairi already said, it is not recommended to re-upload artifacts with the same version to Nexus repository, Maven is built around the idea that an artifact's GAV always corresponds to a unique artifact.

However, if you want to allow re-deployment, you need to set the deployment policy of a release repository to "allow redeploy", then you can redeploy the same version. You cannot do that without allowing on repository side.

And for deploying to Nexus repo, you can use either Nexus Platform Plugin or Nexus Artifact Uploader.

biruk1230
  • 3,042
  • 4
  • 16
  • 29
  • Redeployment of artifact is a common process. Many IT companies do so. Though I thank you for your answer. I will upload in some days another issue and I will mention you to see what I am talking about. Thanks @biruk1230 – dpapadopoulos Jan 22 '19 at 11:22
  • 1
    Yes, I know that some companies do re-deployment, but it still not recommended :) As I said, try to enable "allow redeploy" in your Nexus repository server configuration. – biruk1230 Jan 22 '19 at 11:30
  • You are wright! I did this before the implementation of the code. I will edit my answer for those who will see it in the future. Thanks once more @biruk1230 – dpapadopoulos Jan 22 '19 at 12:30
  • The re-deploy setting I had already enabled it. I just forgot to insert it as an answer @biruk1230. You remembered it to me. :) – dpapadopoulos Jan 22 '19 at 12:46
2

ADDITIONAL SOLUTION THAT ALSO WORKS

I executed it manually and I exported the result of Nexus call. The result was the following command. This command need to be inserted inside the Jenkins pipeline as a Groovy code:

nexusPublisher nexusInstanceId: 'nexus', nexusRepositoryId: 'maven-play-ground', packages: [[$class: 'MavenPackage', mavenAssetList: [[classifier: '', extension: '', filePath: '**PATH_NAME_OF_THE_ARTIFACT**.jar']], mavenCoordinate: [artifactId: '**YOUR_CUSTOM_ARTIFACT_ID**', groupId: 'maven-play-ground', packaging: 'jar', version: '1.0']]], tagName: '**NAME_OF_THE_FILE_IN_THE_REPOSITORY**'    }
  • In the field of filePath we need to insert the path and the name of the artifact.jar file.
  • In the field of artifactId we need to insert the custom (in this occasion for mine artifact) artifact id
  • In the field of tagName we need to insert the custom name of the directory from inside the Nexus Repository

This is a solution that can be done automatically without manual changes and edits. Once we have created the directory in Nexus repository this is going to be executed without any issue and without the need of changing the version number.

Note: also we need to enable re-deploy feature from inside the Nexus Repository settings.

dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34