【问题标题】:My artifactory upload stage to nexus fails whenever i change my pom version in pom file每当我在 pom 文件中更改我的 pom 版本时,我到 nexus 的人工上传阶段都会失败
【发布时间】:2019-06-14 21:48:00
【问题描述】:

我正在使用声明性管道,每当我更改我的 pom 版本时,我的人工上传阶段到 nexus 都会失败。例如:如果我的快照版本是0.3,则我的构建成功。如果在我的 pom 文件中将我的快照版本更改为 0.4 并尝试创建相同的构建,则构建在上传工件阶段失败。所以我的问题是每次我们必须更新 jenkins 文件和 pom 文件以使构建正常工作?是否有一种声明方式可以让版本自行递增?

stage('Upload Artifacts') {
    steps {
        nexusArtifactUploader artifacts: [[artifactId: 'com.lfx', classifier: 'debug', file: 'C:/Program Files (x86)/Jenkins/workspace/Pipeline/target/common-0.4-SNAPSHOT.jar', type: 'jar']], credentialsId: 'f97e3ef5-19ca-4903-b2c5-74a7821062de', groupId: 'LLL', nexusUrl: 'localhost:8081/', nexusVersion: 'nexus3', protocol: 'http', repository: 'LLLTEST', version: '0.4-SNAPSHOT'
    }
}

错误:

java.io.IOException: common-0.3-SNAPSHOT.jar 文件不存在 在 sp.sd.nexusartifactuploader.steps.NexusArtifactUploaderStep$Execution.run(NexusArtifactUploaderStep.java:242) 在 sp.sd.nexusartifactuploader.steps.NexusArtifactUploaderStep$Execution.run(NexusArtifactUploaderStep.java:217) 在 org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47) 在 hudson.security.ACL.impersonate(ACL.java:290) 在 org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44) 在 java.util.concurrent.Executors$RunnableAdapter.call(未知来源) 在 java.util.concurrent.FutureTask.run(未知来源) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(未知来源) 在 java.lang.Thread.run(未知来源) 完成:失败

【问题讨论】:

    标签: jenkins jenkins-pipeline nexus


    【解决方案1】:

    您的构建失败,因为您为 nexusArtifactUploader 文件参数指定了硬编码值(版本参数也是如此):

    file: 'C:/Program Files (x86)/Jenkins/workspace/Pipeline/target/common-0.4-SNAPSHOT.jar'
    
    

    您需要改用动态版本,例如this 示例:

    nexusArtifactUploader(
        nexusVersion: 'nexus3',
        protocol: 'http',
        nexusUrl: 'my.nexus.address',
        groupId: 'com.example',
        version: version,
        repository: 'RepositoryName',
        credentialsId: 'CredentialsId',
        artifacts: [
            [artifactId: projectName,
             classifier: '',
             file: 'my-service-' + version + '.jar',
             type: 'jar']
        ]
     )
    

    您也可以使用Nexus Platform Plugin,例如查看this 答案。 ${pom.version} 在那里使用,它是从 pom.xml 文件中读取的。

    所以你不需要手动增加或传递版本,只需使用命令:

    def pom = readMavenPom file: 'pom.xml'
    

    然后你可以像这样指定你的文件:

    "target/${pom.artifactId}-${pom.version}.${pom.packaging}"
    

    此外,还有另一种自动更改工件版本的方法 - 通过在 Jenkins 管道中将其指定为 ${env.BUILD_NUMBER},然后将其作为参数传递给 mvn 命令。例如,就像在this 答案中一样。

    【讨论】:

    • 感谢 biruk1230 的建议。我按照您的建议修改了我的 jenkinsfile。但我仍然觉得这种方式也是硬编码的。因为我正在声明像 def version = '0.3-SNAPSHOT' 这样的变量。这意味着只有 0.3 版本的快照将被推送到工件阶段,如果将版本更改为 0.4,我认为它不会起作用(因为版本是硬编码的)。我正在寻找一种动态的方式来更改我的版本。我还没有尝试您建议从 pom 文件中读取的第二种方法。将检查并更新
    • 是的,您可以使用我提供的命令pom.xml 读取pom.xml 来动态更改您的版本:def pom = readMavenPom file: 'pom.xml'。它也适用于 Nexus Artifact UploaderNexus Platform 插件。此外,还有另一种指定工件版本的方法 - 将其传递给 Jenkinsfile 中的mvn 命令。详情见this答案。
    • 我收到此错误:java.lang.IllegalArgumentException: Nexus Configuration localhost:8081/ not found。在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 在 java.lang.reflect.Constructor.newInstance(Unknown Source)在 org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    • 我的 jenkins 文件阶段脚本 { def pom = readMavenPom 文件:'pom.xml' nexusPublisher nexusInstanceId:'localhost:8081/',\ nexusRepositoryId:'LLLTEST',\包:[[$class: 'MavenPackage', \mavenAssetList: [[classifier: '', extension: '', filePath: "target/${pom.artifactId}-${pom.version}.${pom.packaging}"]], \mavenCoordinate : [artifactId:"${pom.artifactId}",\groupId:"${pom.groupId}",\packaging:"${pom.packaging}",\version:"${pom.version}"]] ] }
    • 由于您已经配置了nexusArtifactUploader 构建,请尝试使用它而不是nexusPublisher,但在nexusArtifactUploader 命令之前提供def pom = readMavenPom file: 'pom.xml' 命令。但是,如果您想使用 Nexus Publisher 插件,您需要在Manage Jenkins -> Configure System 页面中添加 Nexus Repository Manager Server,并在那里指定您服务器的唯一字符串 ID。查看详情here
    猜你喜欢
    • 2020-02-17
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2012-02-03
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    相关资源
    最近更新 更多