Invoking the Ivy ant tasks should be pretty simple. Please see the Ivy junit tests for examples.
If you want an example about how to use the Ivy API directly, you could take a look at the IvyDE sources. The problem is that we did not yet have defined a stable API, so incompatible API changes are possible with new versions. Maarten ----- Original Message ---- From: Ilya Sterin <[EMAIL PROTECTED]> To: [email protected] Sent: Tuesday, September 23, 2008 4:52:08 AM Subject: Re: Embedded ivy Thanks Jim. Yes, using ant from java is very simple. Also, thanks for the resources, I'll take a look at them shortly. One issue with ivy, is that it's not a matter of instantiating an ant taks and executing it, it's way more involved. You can see that from the code snippet I posted which was extracted from the ivy bootstrap code in their external runner. Ilya On Mon, Sep 22, 2008 at 1:46 AM, Jim White <[EMAIL PROTECTED]> wrote: > 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. > > http://www.ifcx.org/wiki/Wings.html > > The code is in Groovy and looks like this: > > http://ifcx.svn.sourceforge.net/viewvc/ifcx/Wings/trunk/OpenOffice/WingsEval.groovy?revision=151&view=markup#l_2383 > > 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') > ant.property(name:'ivy.dep.file', location:ivy_file) > ant_ivy.resolve() > ant_ivy.cachefileset(setid:'cache-files-id') > 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 = > engineSettings.context.contextClassLoader > > // 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: > > import org.apache.tools.ant.Project; > import org.apache.tools.ant.Task; > import org.apache.tools.ant.taskdefs.Echo; > import org.apache.tools.ant.taskdefs.ExecTask; > > import java.io.File; > > public class java2ant > { > public static void main(final String[] args) > { > final Project project = new Project(); > > project.init(); > > final Echo echo = (Echo) project.createTask("echo"); > > echo.setMessage("Hello World"); > echo.setFile(new File("output.txt")); > echo.execute(); > > final ExecTask exec = (ExecTask) project.createTask("exec"); > > exec.setExecutable("ls"); > exec.setOutput(new File("execoutput.txt")); > exec.execute(); > } > } > > That's available here: > > http://pagesmiths.com/ant/ > > An article on the topic: > > http://www.ibm.com/developerworks/websphere/library/techarticles/0502_gawor/0502_gawor.html > > 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): > > http://www.onjava.com/pub/a/onjava/2006/08/09/ant-1-7-using-antlibs.html?page=2 > > Jim > > 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 >> ... > >
