using IVY dependencies manager programmatically

只谈情不闲聊 提交于 2019-11-28 00:16:54
Mark O'Connor

Ivy can be used as a standalone java program:

java -jar ivy.jar -retrieve "lib/[conf]/[artifact].[ext]"

The retrieve pattern can be then used to determine where files are installed, based on the ivy configuration settings

$ find lib -type f
lib/core/commons-lang.jar
lib/plugin1/commons-logging.jar
lib/plugin1/commons-codec.jar
lib/plugin2/commons-logging.jar
lib/plugin2/commons-cli.jar
lib/plugin3/commons-logging.jar

Configurations are used as a collective label or grouping of dependencies. They are similar to Maven scopes but much more flexible:

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>
    <configurations>
        <conf name="core"    description="Core application dependencies"/>
        <conf name="plugin1" description="Plugin 1 dependencies"/>
        <conf name="plugin2" description="Plugin 2 dependencies"/>
        <conf name="plugin3" description="Plugin 3 dependencies"/>
    </configurations>
    <dependencies>
        <dependency org="commons-lang"    name="commons-lang"    rev="2.5"   conf="core->default"/>
        <dependency org="commons-codec"   name="commons-codec"   rev="1.4"   conf="plugin1->default"/>
        <dependency org="commons-cli"     name="commons-cli"     rev="1.2"   conf="plugin2->default"/>
        <dependency org="commons-logging" name="commons-logging" rev="1.1.1" conf="plugin1,plugin2,plugin3->default"/>
    </dependencies>
</ivy-module>

If you only want to download and install one set of jars, into a specified directory you can use the confs parameter:

java -jar ivy.jar -retrieve "plugin1/[artifact].[ext]" -confs plugin1

Finally, if you still want to use a programming API, you could invoke the run method called by the main class

http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/Main.java?view=markup

Update 1

Groovy has built in support for invoking ivy tasks

import groovy.xml.NamespaceBuilder

def ant = new AntBuilder()
def ivy = NamespaceBuilder.newInstance(ant, 'antlib:org.apache.ivy.ant')

ivy.settings(file:"ivysettings.xml")
ivy.retrieve(pattern:"lib/[conf]/[artifact].[ext]")
ivy.report(toDir:'reports', graph:false) 

Update 2

To set the location of your local Maven repository you need to use an ivysettings.xml file.

<ivysettings>
  <settings defaultResolver='nexus' />
  <resolvers>
    <ibiblio name='nexus' root='http://myhost.mydomanin.com:8081/nexus' m2compatible='true' />
  </resolvers>
</ivysettings>

Update 3

Just found an article that details how to invoke Ivy from Java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!