当工件名称与模块名称不同时,使用 Grab 下载 Ivy 依赖 Jar

Posted

技术标签:

【中文标题】当工件名称与模块名称不同时,使用 Grab 下载 Ivy 依赖 Jar【英文标题】:Use Grab to Download Ivy Dependency Jar when Artifact name is not the same as Module name 【发布时间】:2019-11-19 08:22:03 【问题描述】:

在 Jenkins groovy 脚本中,我尝试使用以下内容下载依赖项:

@Grab(group='myorg', module='SuiteCreator', version='1.16.1', conf='jar', transitive=false)
import myorg.myorgAPI

我有一个 /home/jenkins/.groovy/grapeConfig.xml 文件,其中包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<ivy-settings>
    <settings defaultResolver="downloadGrapes"/>
    <resolvers>
        <chain name="downloadGrapes">
            <sftp user="admin" userPassword="pw" host="ivy.myorg.com" name="myrepository" checkmodified="true">
                <ivy pattern="/data/ivy/repo/[organisation]/[module]/[branch]/[revision]/ivy-[revision].xml"/>
                <artifact pattern="/data/ivy/repo/[organisation]/[module]/[branch]/[revision]/[artifact]-[revision].[ext]"/>
            </sftp>
        </chain>
    </resolvers>
</ivy-settings>

我试图抓取的模块的 ivy-1.16.1.xml

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="1.0">
    <info organisation="myorg" module="SuiteCreator" branch="master" revision="1.16.1" status="release" publication="20190417105814"/>
    <configurations>
        <conf name="jar" description="Distribution jar"/>
    </configurations>

    <publications>
        <artifact name="myorg-suitecreator" type="jar" ext="jar" conf="jar"/>
    </publications>
</ivy-module>

所以我只是想抓住神器:myorg-suitecreator-1.16.1.jar

当我在 Jenkins 中运行我的 groovy 脚本时,我收到以下错误:

2019.07.09 18:06:15 org.codehaus.groovy.control.MultipleCompilationErrorsException: 启动失败: 2019.07.09 18:06:15 转换过程中出现一般错误:抓葡萄时出错——【下载失败: myorg#SuiteCreator#master;1.16.1!SuiteCreator.jar] 2019.07.09 18:06:15 2019.07.09 18:06:15 java.lang.RuntimeException: 抓葡萄时出错——[下载失败:myorg#SuiteCreator#master;1.16.1!SuiteCreator.jar] 2019.07.09 18:06:15 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

从错误看来,Grape 假设 Ivy Artifact Name 与 Module 名称相同。 /ivy-module/publications/artifact/@name 处的 ivy-1.16.1.xml 工件名称被定义为 myorg-suitecreator 但是 Grab 似乎正在尝试下载:SuiteCreator.jar

grapeConfig.xml 中的工件模式是:

<artifact pattern="/data/ivy/repo/[organisation]/[module]/[branch]/[revision]/[artifact]-[revision].[ext]"/>

所以我要抓取的文件实际上是:/data/ivy/repo/myorg/SuiteCreator/1.16.1/myorg-suitecreator-1.16.1.jar

是否有人对如何使其工作有任何建议(或者 Grab 是否可以从 Ivy 下载具有与模块名称不同的工件名称的工件?)。

【问题讨论】:

【参考方案1】:

我放弃了尝试使用 Grab 来实现这一目标。我发现 Grab 的另一个限制是它不允许指定您希望检索的工件的 branch。我意识到在 ma​​ster 分支或单个发布分支上没有发布可能不是最佳实践,但我们在开发环境中确实有这个要求。

相反,我只是在 Jenkins 中使用 Invoke Ant 构建步骤来检索我的 Ivy 工件。我们已经在我们的开发过程中使用了 ANT,所以这并不困难。

ANT build.xml 脚本与我希望运行的 Groovy 脚本位于同一个 git 存储库中。 retrieve-suite-creator 目标只是一个ivy-retrieve

<target name="retrieve-suite-creator" depends="clean, install-ivy">
    <ivy:retrieve conf="suite-creator" type="jar" pattern="$build.dir/[artifact].[ext]" log="$ivy.resolve.log" settingsRef="$ivy.build.settings"/>
</target>

使用我的 ivy.xml(再次在与 groovy 脚本相同的存储库中):

<ivy-module version="1.0">
    <info organisation="myorg" module="MyAutomation" status="integration" branch="$ivy.branch"/> 
    <configurations>
        <conf name="suite-creator" description="Configuration for Suite Creator"/>
    </configurations>
    <dependencies> 
        <dependency org="myorg" name="SuiteCreator" branch="mybranch" rev="1.16.1" conf="suite-creator->suite-creator" changing="true"/>
    </dependencies>
</ivy-module>

我必须将 suite-creator ivy 配置添加到 SuiteCreator 模块的 ivy.xml(在单独的 SuiteCreator Git 存储库中)。我无法使用现有的 jar 配置,因为它还下载了我不需要的所有传递依赖项。

<ivy-module version="1.0">
    <info organisation="myorg" module="SuiteCreator" status="integration" branch="$ivy.branch"/>
    <configurations>
        <!-- Build configurations -->
        <conf name="build" description="Classes used in jar compilation"/>
        <conf name="jar" description="Distribution jar"/>
        <conf name="suite-creator" description="Just the myorg-suitecreator.jar"/>
    </configurations>

    <publications>
        <artifact name="myorg-suitecreator" type="jar" ext="jar" conf="jar,suite-creator"/>
    </publications>

    <dependencies>
    ...
    </dependencies>
</ivy-module>

最后在 Invoke Ant 构建步骤之后在我的 Jenkins 工作中,我有一个 Execute Groovy Script 构建步骤,我必须将下载的 jar 添加到我的 类路径。

【讨论】:

以上是关于当工件名称与模块名称不同时,使用 Grab 下载 Ivy 依赖 Jar的主要内容,如果未能解决你的问题,请参考以下文章

如何在SBT中发布之前检查工件是否存在

Spring - 当列名与模型中的名称不同时,本机查询与休眠混合

使用 Castle ActiveRecord,当两个类名称相同但名称空间不同时,我会收到 NHibernate DuplicateMappingException

控制 jar 工件的 Maven 最终名称

Laravel 验证:唯一,当名称相同,但其他字段不同时

使用 gradle 发布到 maven 时删除附加到工件名称的默认 date.time