适用于 Git 或 TFS 的 Ivy 自定义解析器

发布于 2025-01-06 00:55:51 字数 293 浏览 0 评论 0原文

我对 Ivy 相当陌生,所以也许有一种我无法在文档中找到的直接方法,或者我正在寻找的方法是不可能的,但这里是。我希望能够指定源代码位于使用不同协议的本地和/或远程服务器上的依赖关系。

具体来说,我有一些项目依赖项存储在本地网络 TFS 服务器上,其他项目依赖项存储在远程 Git 服务器上(更准确地说是 www.github.com)。 com)。是否可以让 Ivy 下载源代码并构建一个 jar 文件,然后将其用作依赖项?如果是这样,怎么办?

I'm fairly new to Ivy, so perhaps there's a straight forward way that I'm not able to find in the documentation or what I'm looking for is not possible, but here goes. I want to be able to specify dependencies where the source code is located on local and/or remote servers which use different protocols.

Specifically, I have some project dependencies that are stored on a local network TFS server and additional project dependencies stored on a remote Git server (more precisely www.github.com). Is it possible to have Ivy download the source code and build a jar file which would then be used as the dependency? If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2025-01-13 00:55:51

我会回应 @dbyrne 的答案,即 ivy 旨在管理二进制预编译依赖项。
您最好使用像 Nexus 这样的本地存储库管理器来存储项目的第 3 方依赖项。

然而......

从技术上来说,ivy 使用 打包解析器。这个非常聪明的解析器旨在获取第 3 方 zip 或 tar 存档,然后使用 ANT 提取所需的工件。

下面是我的示例,它从 github leachim6 存储库 下载“hello world”源代码并进行编译立即放入名为“hello-world.jar”的罐子中。

文件

项目文件

|-- build.xml
|-- ivysettings.xml
|-- ivy.xml
`-- repository
    `-- leachim6
        `-- hello-world
            `-- 1.0
                `-- packager.xml

ivy.xml

<ivy-module version="2.0">
    <info organisation="com.demo" module="packager_demo"/>
    <dependencies>
        <dependency org="leachim6" name="hello-world" rev="1.0"/>
    </dependencies>
</ivy-module>

声明对“hello-world”工件 1.0 版的依赖关系。在这个级别,ivy 预计会从某种第三方存储库中获取 jar。

ivysettings.xml

<ivysettings>
    <settings defaultResolver="central"/>
    <resolvers>
        <ibiblio name="central" m2compatible="true"/>
        <packager name="packager" buildRoot="${user.home}/.ivy2/packager/build" resourceCache="${user.home}/.ivy2/packager/cache" preserveBuildDirectories="false" restricted="false">
            <ivy pattern="file:///${ivy.settings.dir}/repository/[organisation]/[module]/[revision]/ivy.xml"/>
            <artifact pattern="file:///${ivy.settings.dir}/repository/[organisation]/[module]/[revision]/packager.xml"/>
        </packager>
    </resolvers>
    <modules>
        <module organisation="leachim6" name="hello-world" resolver="packager"/>
    </modules>
</ivysettings>

这是我们定义要使用的存储库(或 ivy 中的解析器)的地方。默认情况下,ivy 从 Maven Central 检索,但是,我们另外指定应使用打包器解析器检索“hello-world”模块。

打包器属性需要进一步解释:

  • resourceCache :这是存储下载的存档的位置。
  • buildRoot :这是对下载的存档进行解压和操作的地方。
  • restricted :通常不允许打包程序运行javac和delete等ANT命令。如果打包程序文件位于远程位置,则被认为是危险的。

packager.xml

<packager-module version="1.0">
    <property name="name" value="${ivy.packager.module}"/>
    <property name="version" value="${ivy.packager.revision}"/>

    <resource dest="archive" url="https://github.com/leachim6/hello-world/tarball/master" sha1="7f0e2836d1e8dc6130cca68d29b6f86027b22983" type="tar.gz"/>

    <build>
        <mkdir dir="classes"/>
        <javac srcdir="archive/leachim6-hello-world-38f6567/j" includes="*.java" destdir="classes"/>
        <jar destfile="artifacts/jars/${name}.jar" basedir="classes"/>
    </build>
</packager-module>

这里是我们放置创建“hello-world.jar”工件的 ANT 脚本逻辑的位置。

该文件用于生成 ANT 脚本,该脚本下载远程工件(使用其校验和以确保安全)并提取或在我们的示例中编译将返回到 ivy 任务的工件。

最终说明:

  • 这是一个人为的示例。在现实生活中,您会发现编译逻辑通常比简单调用 javac 更复杂。
  • 一旦 github 项目发布另一个版本,该示例就会崩溃。 (没有发布标签作为此示例的基础)

I would echo @dbyrne's answer that ivy is designed to manage binary pre-compiled dependencies.
You are best served in having a local repository manager like Nexus to store your project's 3rd party dependencies.

However....

It is technically possible for ivy to download and compile dependencies, using the packager resolver. This very clever resolver is designed to take a 3rd party zip or tar archive and then use ANT to extract out the required artifact.

Below is my example which downloads the "hello world" source from the github leachim6 repository and compiles it on the fly into a jar called "hello-world.jar".

Files

Project files

|-- build.xml
|-- ivysettings.xml
|-- ivy.xml
`-- repository
    `-- leachim6
        `-- hello-world
            `-- 1.0
                `-- packager.xml

ivy.xml

<ivy-module version="2.0">
    <info organisation="com.demo" module="packager_demo"/>
    <dependencies>
        <dependency org="leachim6" name="hello-world" rev="1.0"/>
    </dependencies>
</ivy-module>

Declares a dependency against version 1.0 of the "hello-world" artifact. At this level ivy would be expected to fetch a jar from some sort of 3rd party repository.

ivysettings.xml

<ivysettings>
    <settings defaultResolver="central"/>
    <resolvers>
        <ibiblio name="central" m2compatible="true"/>
        <packager name="packager" buildRoot="${user.home}/.ivy2/packager/build" resourceCache="${user.home}/.ivy2/packager/cache" preserveBuildDirectories="false" restricted="false">
            <ivy pattern="file:///${ivy.settings.dir}/repository/[organisation]/[module]/[revision]/ivy.xml"/>
            <artifact pattern="file:///${ivy.settings.dir}/repository/[organisation]/[module]/[revision]/packager.xml"/>
        </packager>
    </resolvers>
    <modules>
        <module organisation="leachim6" name="hello-world" resolver="packager"/>
    </modules>
</ivysettings>

This is where we define the repositories (or resolvers in ivy-speak) to be used. By default ivy retrieves from Maven Central, however, we've additionally specified that the "hello-world" module should instead be retrieved using a packager resolver.

The packager attributes need some further explanation:

  • resourceCache : This is where the downloaded archive is stored.
  • buildRoot : This is where the downloaded archive is decompressed and acted upon.
  • restricted : Normally a packager is not allowed to run ANT commands like javac and delete. Considered dangerous if the packager file was located on a remote location.

packager.xml

<packager-module version="1.0">
    <property name="name" value="${ivy.packager.module}"/>
    <property name="version" value="${ivy.packager.revision}"/>

    <resource dest="archive" url="https://github.com/leachim6/hello-world/tarball/master" sha1="7f0e2836d1e8dc6130cca68d29b6f86027b22983" type="tar.gz"/>

    <build>
        <mkdir dir="classes"/>
        <javac srcdir="archive/leachim6-hello-world-38f6567/j" includes="*.java" destdir="classes"/>
        <jar destfile="artifacts/jars/${name}.jar" basedir="classes"/>
    </build>
</packager-module>

Here is where we place the ANT script logic that creates the "hello-world.jar" artifact.

This file is used to generate an ANT script that downloads the remote artifact (using it's checksum for security) and extracts out or in our case compiles the artifact that will be returned to the ivy task.

Final notes:

  • This is a contrived example. In real life, you'll discover the compile logic is normally more complicated than a simple call to javac.
  • This sample will break as soon as the github project releases another version. (There were no release tags to base this sample on)
枫林﹌晚霞¤ 2025-01-13 00:55:51

这要么是不可能的,要么需要严重的黑客攻击。 Ivy 旨在下载已编译的依赖项。使用任何现代构建管理工具(Maven、Ivy、Gradle 等),尽可能遵守约定总是更好。你离正路越远,最终给自己带来的痛苦就越多。

为什么需要动态下载并编译代码?如果您只想将源代码用于调试目的,这个问题 可能有帮助。如果原因是因为您总是想要检查到 VCS 中的任何内容的绝对前沿版本,那么您最好设置某种连续构建来为您生成工件。

This is either impossible or would require serious hacks. Ivy is designed to download dependencies which have already been compiled. With any of the modern build management tools (Maven, Ivy, Gradle, etc.) you are always better off sticking to convention when possible. The farther you stray from the well-beaten path, the more pain you are going to cause yourself in the end.

Why do you need to download and compile the code dynamically? If you just want to have the source code available for debugging purposes, this question may help. If the reason is because you always want the absolute bleeding edge version of whatever is checked into your VCS, you would be better off setting up some sort of continuous build which produces your artifacts for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文