ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache Ivy (Apache Ant subproject) > Programmatic use of Ivy
Date Thu, 22 Oct 2009 13:26:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=IVY&amp;forWysiwyg=true"
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="">Programmatic
use of Ivy</a></h2>
    <h4>Page  <b>added</b> by             <a href="">Jaikiran
    <div class="notificationGreySide">
         <p>This page provides some examples on how to use Ivy APIs within your applications,
to achieve the same functionality that you could have through Ant scripts.</p>

<p>This document does <b>not</b> contain the full set of APIs nor does it
have examples for all the features. This is just a start towards making available some documentation
for using Ivy APIs. From what i have seen in the mailing lists there isn't a central place
where information related to this is available. In fact, i had to browse the Ivy source code
and some mails in the user mailing list to find out which APIs to use.</p>

<p>So let's get to the examples now&#33;</p>

<p><ins>Note that at the time of writing this, i had used Ivy version 2.1.0</ins></p>

<h1><a name="ProgrammaticuseofIvy-GettinganIvyinstance"></a>Getting an Ivy

<p>The first step towards using Ivy within your application, is to create an Ivy instance.
To create an Ivy instance, you would need to create an instance of Ivy settings first. Here's
how you can create the IvySettings instance:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
org.apache.ivy.core.settings.IvySettings ivySettings = <span class="code-keyword">new</span>
<span class="code-comment">// Now let's set the basedir of the ivy settings to some
</span>File baseDir = <span class="code-keyword">new</span> File(<span

<p>Once you have create the Ivy settings, you can now you use it to create an Ivy instance.
Here's how it is done:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// create an ivy instance
</span>org.apache.ivy.Ivy ivy = Ivy.newInstance(ivySettings);
<p>With these 2 steps, you have now created an Ivy instance, which will be required
to do any operations related to Ivy dependency management.</p>

<h1><a name="ProgrammaticuseofIvy-ConfiguringtheIvyinstance"></a>Configuring
the Ivy instance</h1>

<p>After creating the Ivy instance, you will have to configure it to use the correct
settings file. Here's how it can be done:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// That's where my ivy-settings.xml is located
</span>File ivySettingsXmlFile = <span class="code-keyword">new</span> File(<span
<span class="code-comment">// configure ivy to use the settings file
<p>With these steps, you have now configured an Ivy instance to be usable for dependency
management operations.</p>

<h1><a name="ProgrammaticuseofIvy-ResolvingdependenciesusinganIvyinstance"></a>Resolving
dependencies using an Ivy instance</h1>

<p>Next, let's see how we can use this Ivy instance to resolve dependencies. In the
example here, our dependencies are listed in a file named "ivy.xml" which is located at /home/me/ivy/samples/test/ivy.xml.
The contents of ivy.xml are as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;ivy-module version=<span class="code-quote">"2.0"</span>&gt;
 &lt;info organisation=<span class="code-quote">"apache"</span> module=<span
       &lt;dependency org=<span class="code-quote">"commons-lang"</span> name=<span
class="code-quote">"commons-lang"</span> rev=<span class="code-quote">"2.0"</span>/&gt;
       &lt;dependency org=<span class="code-quote">"commons-cli"</span> name=<span
class="code-quote">"commons-cli"</span> rev=<span class="code-quote">"1.0"</span>/&gt;
<p><em>For a detailed explanation of the contents of ivy.xml, please read the
Ivy user manual or the tutorials.</em></p>

<p>Our intention is to feed this dependency file to the ivy instance we created and
ask it to resolve the dependencies. Here's how we do this - Comments have been provided inline
to explain what we are doing:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// file containing the list of dependencies (ex: ivy.xml)
</span>File dependencyFile = <span class="code-keyword">new</span> File(<span
<span class="code-comment">// resolve the dependencies - Ivy returns a report of the
</span>ResolveReport resolveReport = ivy.resolve(dependencyFile);

<span class="code-comment">// check <span class="code-keyword">for</span>
errors (<span class="code-keyword">if</span> any) during resolve
</span><span class="code-keyword">if</span> (resolveReport.hasError())
      List&lt;<span class="code-object">String</span>&gt; problems = resolveReport.getAllProblemMessages();
      <span class="code-keyword">if</span> (problems != <span class="code-keyword">null</span>
&amp;&amp; !problems.isEmpty())    
           <span class="code-object">StringBuffer</span> errorMsgs = <span
class="code-keyword">new</span> <span class="code-object">StringBuffer</span>();
          <span class="code-keyword">for</span> (<span class="code-object">String</span>
problem : problems)
            errorMsgs.append(<span class="code-quote">"\n"</span>);
            <span class="code-object">System</span>.err.println(<span
class="code-quote">"Errors encountered during dependency resolution <span class="code-keyword">for</span>
<span class="code-keyword">package</span> "</span> + pkgCtx + <span class="code-quote">"
            <span class="code-object">System</span>.err.println(errorMsgs);  
<span class="code-keyword">else</span>
{    <span class="code-object">System</span>.out.println(<span class="code-quote">"Dependencies
in file "</span> + dependencyFile + <span class="code-quote">" were successfully
<h1><a name="ProgrammaticuseofIvy-Retrievingtheresolveddependencies"></a>Retrieving
the resolved dependencies</h1>

<p>Now that the dependencies have been resolved, we can use the ivy instance to retrieve
these dependencies</p>

<p><em>To understand the difference between "resolve" and "retrieve" and other
Ivy terminologies, please refer to the Ivy user manual or the tutorials</em></p>

<p>Comments have been provided inline to explain what's going on</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// Now that the dependencies have been resolved, let now
retrieve them
</span><span class="code-comment">// Get the descriptor
</span>org.apache.ivy.core.module.descriptor.ModuleDescriptor md = resolveReport.getModuleDescriptor();
<span class="code-comment">// module revision id of the module whose dependencies were
</span> mRID = md.getModuleRevisionId();

<span class="code-comment">// options that we pass to ivy instance <span class="code-keyword">for</span>
retrieve the dependencies
</span>org.apache.ivy.core.retrieve.RetrieveOptions retrieveOptions = <span class="code-keyword">new</span>

<span class="code-comment">// folder to which we want the dependencies retrieved
</span>File destFolder = <span class="code-keyword">new</span> File(<span

<span class="code-comment">// the Ivy pattern which will be used <span class="code-keyword">for</span>
retrieving the dependencies
</span><span class="code-object">String</span> pattern = destFolder + <span

<span class="code-comment">// <span class="code-keyword">default</span>
logging option

<span class="code-comment">// retrieve them!
</span><span class="code-object">int</span> packagesRetrieved = ivy.retrieve(mRID,
pattern, retrieveOptions);

<span class="code-object">System</span>.out.println(<span class="code-quote">"Retrieved
"</span> + packagesRetrieved + <span class="code-quote">" dependencies"</span>);

<p>That's it&#33; With these steps you have now successfully resolved and retrieved
the dependencies of a module through Ivy APIs.</p>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>
       <a href="">View
       <a href=";showCommentArea=true#addcomment">Add

View raw message