【问题标题】:Jenkins - Execute shell commands with wildcardsJenkins - 使用通配符执行 shell 命令
【发布时间】:2016-08-08 07:16:22
【问题描述】:

我试图让 Jenkins 执行一个 shell 命令,但仍然允许使用通配符。以下是我想要做的参考:

mvn deploy:deploy-file -Dpackaging=jar -DrepositoryId=snapshots -Durl=http://nexus.example.net/content/repositories/snapshots -DpomFile=Spigot/Spigot-Server/pom.xml -Dfile=Spigot/Spigot-Server/target/spigot-*.jar

我需要能够通过上述命令部署此 jar,因为该项目的 git 存储库不归我所有或操作,因此我需要能够将其直接部署到我自己的 Nexus 实例。为了确保它支持所有可能的已编译 jar 版本,我必须使用通配符。不幸的是,当 Jenkins 尝试执行该命令时,它按字面意思使用通配符。我真的不知道如何解决这个问题,我将不胜感激您能提供的任何帮助。谢谢!

【问题讨论】:

    标签: java shell maven jenkins


    【解决方案1】:

    如果它是一个简单的 .jar 文件,试试这个:

    mvn deploy:deploy-file -Dpackaging=jar -DrepositoryId=snapshots -Durl=http://nexus.example.net/content/repositories/snapshots -DpomFile=Spigot/Spigot-Server/pom.xml -Dfile=$(find Spigot/Spigot-Server/target/ -name 'spigot-*.jar')
    

    如果是多个文件,那就有点复杂了:

    ma​​ven deploy-file 参数 filesclassifierstypes 用于部署多个same (groupId, artifactId, version) 下的工件 - 例如 .jar-sources.jar

    即使对于该用例,语法也有些违反直觉 - 您必须使用 file=file1.jar 作为第一个工件,然后使用 files=file1 -sources.jar,file1-test-sources.zip,.. 用于 rest,同时使用 classifier/classifiers(和 packaging/types ) 以相同的方式(位置)指定您上传的每个工件的分类器/类型。

    如果您的用例是上传不同版本的工件,您将需要为每个版本调用一次 maven 部署文件

    您也可以考虑一些替代方案:

    1. (取决于工件的数量以及新工件的出现频率)- 手动上传这些工件到 Nexus

    2. 让您的 Nexus 代理另一个 Nexus 存储库,为这些工件提供服务。

    【讨论】:

    • 非常感谢!作为记录,我只是试图部署一个文件。
    【解决方案2】:

    如果您的意思是只想直接传递 *,只需使用单引号来避免 shell 应用通配符:

    mvn deploy:deploy-file [...] '-Dfile=Spigot/Spigot-Server/target/spigot-*.jar'
    

    【讨论】:

    • 我实际上想要相反的结果。它是问题所在。
    • @mattrick:啊。这可能值得澄清。您可能需要在多个命令中执行此操作...不确定。
    • @JonSkeet,FWIW,帖子确实声明“它确实需要通配符”:-) 有趣的是,如果该命令在 Jenkins 之外的 bash 中运行,shell 确实会在执行命令之前尝试 glob除非遵循您的报价建议。不知道为什么 Jenkins shell 不遵循相同的行为。
    • @tony19:我确实被描述弄糊涂了,所以我怀疑其他人可能也是。无论如何,我会尽可能删除我的答案。
    【解决方案3】:

    如果您尝试部署 多个 文件,我认为问题是 不是 使用 Jenkins 或 bash 命令,而是使用 Maven Deploy Plugin

    文档状态

    file  File    -   File to be deployed.
    User property is: file.
    

    和 如果您要部署额外的工件,请使用

    files     String  -   A comma separated list of files for each of the
                        extra side artifacts to deploy. If there is a 
                        mis-match in the number of entries in types or
                        classifiers, then an error will be raised. 
    User property is: files.
    

    因此,如果您使用额外的filestypesclassifies 参数来明确指定要部署的额外文件会更好,例如:

    ... -Dfile=Spigot/Spigot-Server/target/main-spigot.jar \
    -Dfiles=$(ls -1d Spigot/Spigot-Server/target/spigot-*.jar | paste -sd ,) -Dtypes=... -Dclassifiers=... 
    

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 2012-08-25
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 2017-07-06
      • 1970-01-01
      相关资源
      最近更新 更多