我能否在ivy.xml文件中使用属性来避免重复依赖项的版本号?

22

以下是我当前ivy.xml的一部分:

<dependency org="org.springframework" name="org.springframework.core" rev="3.0.2.RELEASE" />
<dependency org="org.springframework" name="org.springframework.context" rev="3.0.2.RELEASE" />
<dependency org="org.springframework" name="org.springframework.jdbc" rev="3.0.2.RELEASE" />
<dependency org="org.springframework" name="org.springframework.beans" rev="3.0.2.RELEASE" />
<dependency org="org.springframework" name="org.springframework.jms" rev="3.0.2.RELEASE" />

这是我希望它看起来的样子:

<dependency org="org.springframework" name="org.springframework.core" rev="${spring.version}" />
<dependency org="org.springframework" name="org.springframework.context" rev="${spring.version}" />
<dependency org="org.springframework" name="org.springframework.jdbc" rev="${spring.version}" />
<dependency org="org.springframework" name="org.springframework.beans" rev="${spring.version}" />
<dependency org="org.springframework" name="org.springframework.jms" rev="${spring.version}" />

这个可能吗?语法是什么?

3个回答

32

最终我使用XML实体来进行替换。这样可以将所有内容保留在同一文件中,而这对我的使用情况非常重要。

<?xml version="1.0"?>
<!DOCTYPE ivy-module [
    <!ENTITY spring.version "3.0.2.RELEASE">
]>
<ivy-module version="2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://incubator.apache.org/ivy/schemas/ivy.xsd">

    <info organisation="org" module="mod"/>

    <dependencies>
        <dependency org="org.springframework" name="org.springframework.core" rev="&spring.version;" />
        <dependency org="org.springframework" name="org.springframework.context" rev="&spring.version;" />
        <dependency org="org.springframework" name="org.springframework.jdbc" rev="&spring.version;" />
        <dependency org="org.springframework" name="org.springframework.beans" rev="&spring.version;" />
        <dependency org="org.springframework" name="org.springframework.jms" rev="&spring.version;" />
    </dependencies>
</ivy-module>

1
XML实体的使用非常出色。确实非常有帮助。 - Viking17

14
语法是正确的,你只需要在某个地方设置 ANT 属性。
例如:
ant -Dspring.version=3.0.2.RELEASE

另一种选择是将属性声明添加到 ivysettings.xml 文件中。

<ivysettings>

    <property name="spring.version" value="3.0.2.RELEASE"/>

    <settings defaultResolver="maven2"/>
    <resolvers>
        <ibiblio name="maven2" m2compatible="true"/>
    </resolvers>
</ivysettings>

2
太酷了!能否在ivy.xml文件中设置属性?这样所有的依赖信息都会在一起。 - Edward Dale
将属性声明放在ivy设置文件中可以实现将依赖信息保持在一起的相同目标。 - Mark O'Connor
2
谢谢你的回答,但我还是选择了我的解决方案(https://dev59.com/n3A75IYBdhLWcg3w_ef1#3091114),因为我想把版本声明放在同一个文件中。 - Edward Dale
这在命令行上运行得很好,但似乎与IntelliJ的IvyIdea插件不兼容。有什么建议吗? - Rich Ashworth
听起来像是ivy插件没有读取到设置文件。我不熟悉那个插件,所以无法提供权威的建议。 - Mark O'Connor

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接