问:如何使用groovy管道将工件保存到Nexus存储库?

问题描述 投票:2回答:3

我的问题是将工件保存到存储库中。特别是,我试图在执行Maven项目的构建管道(通过Jenkins)之后上传到Nexus Repository工件并发布版本。

我想要这样做的唯一方法就是使用Groovy编写的管道,以便与Jenkins集成。

注意:我希望工件版本号始终相同,并且版本号要动态更改(非手动)。

通常是否有命令或代码可以让我这样做?

jenkins continuous-integration repository jenkins-pipeline nexus
3个回答
3
投票

你处于错误的水平,这应该发生在maven。在pom.xml中你需要。 (more here

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

然后在插件部分

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

你应该能够从你的管道做mvn clean deploy

编辑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')
    }
  }

2
投票

还可以使用其他解决方案

我手动执行它并导出了Nexus调用的结果。结果是以下命令。需要将此命令作为Groovy代码插入Jenkins管道内:

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**'    }
  • 在filePath字段中,我们需要插入artifact.jar文件的路径和名称。
  • 在artifactId的字段中,我们需要插入自定义(在此场合中为我的工件)工件id
  • 在tagName字段中,我们需要从Nexus存储库中插入目录的自定义名称

这是一种可以自动完成的解决方案,无需手动更改和编辑。一旦我们在Nexus存储库中创建了目录,就可以毫无问题地执行该目录,而无需更改版本号。

注意:我们还需要在Nexus存储库设置中启用重新部署功能。


1
投票

正如@hakamairi已经说过的那样,不建议将具有相同版本的工件重新上传到Nexus存储库,Maven是基于工件的GAV始终对应于唯一工件的想法而构建的。

但是,如果要允许重新部署,则需要将发布存储库的部署策略设置为“允许重新部署”,然后可以重新部署相同的版本。如果不允许存储库端,则不能这样做。

要部署到Nexus repo,您​​可以使用Nexus Platform PluginNexus Artifact Uploader

© www.soinside.com 2019 - 2024. All rights reserved.