ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Ivy (Apache Ant subproject) > Programmatic use of Ivy
Date Thu, 22 Oct 2009 13:26:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=IVY&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/IVY/Programmatic+use+of+Ivy">Programmatic
use of Ivy</a></h2>
    <h4>Page  <b>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~jaikiran">Jaikiran
Pai</a>
    </h4>
         <br/>
    <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
instance</h1>

<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>
IvySettings();
<span class="code-comment">// Now let's set the basedir of the ivy settings to some
location
</span>File baseDir = <span class="code-keyword">new</span> File(<span
class="code-quote">"/home/me/ivy"</span>);ivySettings.setBaseDir(baseDir);
</pre>
</div></div>


<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);
</pre>
</div></div>
<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
class="code-quote">"/home/me/ivy/conf/ivy-settings.xml"</span>);
<span class="code-comment">// configure ivy to use the settings file
</span>ivy.configure(ivySettingsXmlFile);
</pre>
</div></div>
<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
class="code-quote">"hello-ivy"</span>/&gt;
 &lt;dependencies&gt;
       &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;
 &lt;/dependencies&gt;
&lt;/ivy-module&gt;
</pre>
</div></div>
<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
class="code-quote">"/home/me/ivy/samples/test/ivy.xml"</span>);
<span class="code-comment">// resolve the dependencies - Ivy returns a report of the
resolution
</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(problem);
            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>);
            <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
resolved"</span>);
}
</pre>
</div></div>
<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
resolved
</span>org.apache.ivy.core.module.id.ModuleRevisionId 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>
RetrieveOptions();

<span class="code-comment">// folder to which we want the dependencies retrieved
</span>File destFolder = <span class="code-keyword">new</span> File(<span
class="code-quote">"/home/me/ivy/samples/test/dependencies"</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
class="code-quote">"/[organization]/[module]/[type]/[artifact]-[revision].[ext]"</span>;
retrieveOptions.setDestIvyPattern(pattern);

<span class="code-comment">// <span class="code-keyword">default</span>
logging option
</span>retrieveOptions.setLog(LogOptions.LOG_DEFAULT);

<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>);


</pre>
</div></div>
<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>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="http://cwiki.apache.org/confluence/display/IVY/Programmatic+use+of+Ivy">View
Online</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/IVY/Programmatic+use+of+Ivy?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message