6

i need help using IVY as dependencies manager
my application need to load plug-ins at RUN-TIME
means while the core application is running ,user can request for a new plug-in
and install them ,
i wish to manage all installed plug-ins using the core application DB.

i want to get a pom from my plug-ins server (or some other descriptor),
and ask IVY to tell me what are the dependencies OR let IVY install the plug-in and dependencies , based on the current state of my application.

( what do i have right now ,
1.jar's used by core application
2.jar's used by current installed plug-ins)

i wish for each plug-in to have independent directory
under some folder where my application is located (not a shared one)

the thing is i don't know where to start
i notice that there is no programming API for that

i located one link over the network of someone who try something similar
but look likes the code is not completed , or some variable are missing and i dont know how to complete the rest cause lake of programming documentation
http://www.mail-archive.com/ivy-user@ant.apache.org/msg03228.html (where the variable "art" came for.....)

can you help me please ... Thank you all

UPDATE

ok this what i am trying now . i am getting some a error plus i dont know how to define my archiva server

      IvySettings settings = new IvySettings();

     settings.setDefaultIvyUserDir(new File("D:/programming/eclipse_projects/ivyTest/repo/"));

      settings.setDefaultCache(new File("D:/programming/eclipse_projects/ivyTest/repo/cache/"));
      settings.setDefaultCacheArtifactPattern("[module]/[revision]/[module]-[revision](-[classifier]");

  Ivy ivy = Ivy.newInstance(settings);

  ivy.getLoggerEngine().pushLogger(new DefaultMessageLogger(Message.MSG_VERBOSE));

  ModuleDescriptor md =  PomModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), new File("src/movies.pom").toURL(), true);

  RetrieveOptions retriveOptions = new RetrieveOptions();
  retriveOptions.setUseOrigin(true);
  retriveOptions.setConfs(md.getConfigurationsNames());
  ivy.retrieve(md.getModuleRevisionId(), "lib/[conf]/[artifact].[ext]", etriveOptions);

this is the error i get

:: loading settings :: url = jar:file:/D:/programming/eclipse_projects/ivyTest/ivy-2.2.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: retrieving :: org.jtpc#movies
    checkUpToDate=true
    confs: [default, master, compile, provided, runtime, test, system, sources, javadoc, optional]
java.lang.RuntimeException: problem during retrieve of org.jtpc#movies: java.lang.IllegalStateException: Report file 'D:\programming\eclipse_projects\ivyTest\repo\cache\org.jtpc-movies-default.xml' does not exist.
    at org.apache.ivy.core.retrieve.RetrieveEngine.retrieve(RetrieveEngine.java:206)
    at org.apache.ivy.Ivy.retrieve(Ivy.java:540)
    at Test.main(Test.java:52)
Caused by: java.lang.IllegalStateException: Report file 'D:\programming\eclipse_projects\ivyTest\repo\cache\org.jtpc-movies-default.xml' does not exist.
    at org.apache.ivy.plugins.report.XmlReportParser.parse(XmlReportParser.java:294)
    at org.apache.ivy.core.retrieve.RetrieveEngine.determineArtifactsToCopy(RetrieveEngine.java:288)
    at org.apache.ivy.core.retrieve.RetrieveEngine.retrieve(RetrieveEngine.java:104)
    ... 2 more
shay
  • 1,317
  • 4
  • 23
  • 35

1 Answers1

8

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

Darkyen
  • 109
  • 5
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • 1
    thats a way to do it .. ,but since ivy its a java API why not call ivy methods , and set IVY settings from code , – shay Oct 18 '10 at 23:49
  • I suppose there is no reason why not. I'm sure if you open up the source code you'll discover all of ivy's goodness. Remember Ivy's primary usecase is a plugin for ANT, but it has been successfully embedded in Groovy , Grails and Gradle – Mark O'Connor Oct 19 '10 at 12:12
  • yes, this i know , also i know digging in source code with no api its complicated , maybe someone is know how to – shay Oct 20 '10 at 17:38
  • Use the groovy example I gave you. Don't forget that it's possible to compile groovy into Java bytecode. That's an example for another day – Mark O'Connor Oct 20 '10 at 19:17
  • i used you update see updated querstion , how can i add a proper comment with code ? – shay Oct 20 '10 at 19:21
  • I'm done. Doesn't look like you're going to accept my proposed solution. You've also expanded the scope of your original question to now include archiva configuration..... Can I point out that a far simpler solution would be to download your required jars using HTTP from the Maven repository. – Mark O'Connor Oct 20 '10 at 19:40
  • 1
    i simply don't understand why an opensource API could be so closed , i dont want to use XML's ,and i have no problem downloading my self using HTTP , just want to know what are the dependencies and what to download , why so complicated , i am on the edge for giving up on IVY – shay Oct 23 '10 at 11:04
  • You're being unreasonable. Calling the internal java methods can never be considered an API. Ivy was primarily designed as an ANT plugin. You'll discover that Maven is even less accessible. My parting piece of advice is that Sonatype have released a new programmable API for Maven 3.0. Perhaps that's where you should look. – Mark O'Connor Oct 24 '10 at 12:57
  • Just found an article that details how to invoke ivy from Java. See article update 3 – Mark O'Connor Nov 08 '10 at 14:48