如何获取和使用神器的版本

How to get and use the version of artifact

当存在依赖项时,即在 ivy.xml 中定义,例如像这样:

<dependency org="com.company" name="mypackage" rev="6.0.0"/>

并在 build.xml 中,我让常春藤检索它。然后它下载名称中带有版本 ID 的文件。例如。 lib/mypackage-6.0.0.zip

我如何知道我的 ANT 构建中的版本 (6.0.0) 或全名,而不重复 ivy.xml manually/dedundantly 中的版本?

我希望能够做类似的事情:

<unzip src="lib/mypackage-${version}.zip" ...

选项 1

一种方法是使用 artifactpropery task, to set a property with the dependency version, based on a pattern similar to how the retrieve 任务。

<ivy:artifactproperty name="[artifact].ver" value="[revision]"/>

<echo message="Version: ${slf4j-api.ver}"/>

选项 2

我的替代偏好是使用配置并在构建工作区中简单地创建一个文件名没有修改的文件,如下所示:

<ivy:retrieve pattern="target/zips/[artifact].[ext]" conf="zips"/>

这需要一个常春藤文件,该文件具有特殊的 "zips" 配置设置和依赖声明的配置映射:

<dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" conf="zips->default"/>

示例(选项 2)

在此示例中,在 "lib" 目录下设置了 3 个不同的类路径,并且在 zips 目录中填充了一个文件名没有修改的 jar 文件:

├── build.xml
├── ivy.xml
├── lib
│   ├── compile
│   │   └── slf4j-api-1.7.5.jar
│   ├── runtime
│   │   ├── log4j-1.2.17.jar
│   │   ├── slf4j-api-1.7.5.jar
│   │   └── slf4j-log4j12-1.7.5.jar
│   └── test
│       ├── hamcrest-core-1.3.jar
│       ├── junit-4.11.jar
│       ├── log4j-1.2.17.jar
│       ├── slf4j-api-1.7.5.jar
│       └── slf4j-log4j12-1.7.5.jar
└── target
    └── zips
        └── slf4j-api.jar

build.xml

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <target name="resolve">
        <ivy:resolve/>
    </target>

    <target name="build" depends="resolve">
        <!-- Populate the "lib" directory -->
        <ivy:retrieve pattern="lib/[conf]/[artifact]-[revision](-[classifier]).[ext]" conf="compile,runtime,test"/>

        <!-- Populate the "target/zips" directory -->
        <ivy:retrieve pattern="target/zips/[artifact].[ext]" conf="zips"/>
    </target>

</project>

ivy.xml

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
        <conf name="zips"    description="Additional configuration for demo"/>
    </configurations>

    <dependencies>
        <!-- zips + compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" conf="zips,compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.11" conf="test->default"/>
    </dependencies>

</ivy-module>

注:

  • slf4j-api 包含在 "zips" 和 "compile" 配置中。