ant-ivy-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim White <>
Subject Re: Embedded ivy
Date Mon, 22 Sep 2008 05:46:25 GMT
I embed Ivy into IFCX Wings, but I do it via the Ant tasks.  That will 
provide the best compatibility going forward as they will change less 
than the Java API.

The code is in Groovy and looks like this:

def ivy_resolve(WingsEngineSettings engineSettings, def ivy_file, 
URLClassLoader classLoader)
     final WingsContext context = engineSettings.context

     final AntBuilder ant = new AntBuilder(createAntProject(context), 
new Target())

     def ant_ivy = groovy.xml.NamespaceBuilder.newInstance(ant, 
'antlib:org.apache.ivy.ant')'ivy.dep.file', location:ivy_file)
     ant.path { fileset(refid:'cache-files-id') }

     final List jars = ant.path { fileset(refid:'cache-files-id') 
}.collect { it.file.toURI().toURL() }

     if (null == classLoader) classLoader = 

     // Only add JARs that aren't already in the list.
     // TODO: Do duplicate/conflict resolution stuff through Ivy.
     (jars - (classLoader.getURLs() as List)).each { URL jar -> 
classLoader.addURL(jar) }

     return jars

I realize that isn't much use to folks who aren't using Groovy, and 
having your Java example is very helpful.  But using Ant from Java is 
really pretty simple.  A bit a code I wrote to demonstrate that a while 
ago is:



public class java2ant
    public static void main(final String[] args)
       final Project project = new Project();


       final Echo echo = (Echo) project.createTask("echo");

       echo.setMessage("Hello World");
       echo.setFile(new File("output.txt"));

       final ExecTask exec = (ExecTask) project.createTask("exec");

       exec.setOutput(new File("execoutput.txt"));

That's available here:

An article on the topic:

I don't have an example of how to call an Antlib task from Java though. 
  Perhaps this article might help (although a quick scan didn't show 
anything directly applicable):


Ilya Sterin wrote:

> Ok, so I think I figured it out, but still not sure if anything is out
> of the ordinary.  I took bits and pieces of this from the ivy
> Main.main method...
> Ivy ivy = Ivy.newInstance();
>         ivy.configureDefault();
>         File ivyfile = File.createTempFile("ivy", ".xml");
>         ivyfile.deleteOnExit();
>         String[] dep = new String[]{"commons-lang", "commons-lang", "1.0"};
>         DefaultModuleDescriptor md = DefaultModuleDescriptor
>                 .newDefaultInstance(ModuleRevisionId.newInstance(dep[0],
>                         dep[1] + "-caller", "working"));
>         DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md,
>                 ModuleRevisionId.newInstance(dep[0], dep[1], dep[2]),
> false, false, true);
>         md.addDependency(dd);
>         XmlModuleDescriptorWriter.write(md, ivyfile);
>         String[] confs = new String[]{"default"};
>         ResolveOptions resolveOptions = new ResolveOptions().setConfs(confs);
>         ResolveReport report = ivy.resolve(ivyfile.toURL(), resolveOptions);
> It would be nice for someone to document this.  I can imagine many
> systems wanting to use ivy's resolution mechanism outside of the ant
> process.
> Ilya
> ...

View raw message