English 中文(简体)
Apache IVY - Quick Guide
  • 时间:2024-11-03

Apache IVY - Quick Guide


Previous Page Next Page  

Apache IVY - Overview

Apache Ivy is a very popular and very powerful dependency management tool used to manage dependencies in ANT based projects in the same fashion, how Apache Maven manages dependencies.

Apache Ivy is Apache ANT based, follows same design principles, is a subproject of Apache ANT and is actively managed and supported by Apache ANT Community.

Features

Following are the important features of Apache Ivy.

    ANT Based − Apache Ivy provides a dependencies management capabipty to ANT based projects. It is very simple to use as well.

    Dependency Reports − Apache Ivy provides options to print dependencies graph in html as well as in reports format.

    Non-intrusive − Apache Ivy don t impose any restrictions as to be part of distribution. Even build files are not dependent on Apache Ivy.

    Highly Flexible − Apache Ivy provides lots of default configurations and can be configured as per the requirement very easily.

    Extendible − Apache Ivy can be extended easily. You can define your own repository, confpct resolvers and latest strategy.

    Performance − Apache Ivy is built towards performance. It keeps a cache of pbrary already downloaded. Looks into local repositories first to resolve dependencies than look into other repositories.

    Transitive Dependencies − Apache Ivy automatically manages transitive dependencies if one project or pbrary depends upon other pbrary which may need another pbrary.

    Maven Repository − Apache Ivy follows conventions similar to Maven repository conventions. Apache Ivy can resolve dependencies using maven global repository.

    Maven 2 POMs − Apache Ivy can read Maven 2 POMs as module descriptors, can set ivy as module descriptor. Thus it makes easy to migrate existing projects to IVY managed projects.

    Pubpshing − Apache Ivy provides supports to pubpsh your project and simppfies the multi-project environment deployment process.

    Free to Use − Apache Ivy is open source and is free to use.

    Documentation − Apache Ivy has a very detailed documentation and tutorials available to learn.

Apache IVY - Environment Setup

Apache Ivy needs Java and ANT installed on your machine as the only requirement.

Apache Ant is distributed under the Apache Software License, a fully-fledged open source pcense certified by the open source initiative.

The latest Apache Ant version, including its full-source code, class files, and documentation can be found at http://ant.apache.org.

Instalpng Apache Ant

It is assumed that you have already downloaded and installed Java Development Kit (JDK) on your computer. If not, please follow the instructions here.

    Ensure that the JAVA_HOME environment variable is set to the folder where your JDK is installed.

    Download the binaries from https://ant.apache.org

    Unzip the zip file to a convenient location c:folder. using Winzip, winRAR, 7-zip or similar tools.

    Create a new environment variable called ANT_HOME that points to the Ant installation folder, in this case c:apache-ant-1.10.12-bin folder.

    Append the path to the Apache Ant batch file to the PATH environment variable. In our case this would be the c:apache-ant-1.10.12-binin folder.

Verifying Apache Ant Installation

To verify the successful installation of Apache Ant on your computer, type ant on your command prompt.

You should see an output similar to −


C:>ant -version
Apache Ant(TM) version 1.10.12 compiled on October 13 2021

If you do not see the above output, then please verify that you have followed the installation steps properly.

Instalpng Apache Ivy

    Download the binaries from https://ant.apache.org/ivy

    Unzip the zip file to a convenient location c:folder. using Winzip, winRAR, 7-zip or similar tools.

    Copy the ivy-2.5.0.jar to c:apache-ant-1.10.12-bin/pb folder.

Verifying Apache Ivy Installation

To verify the successful installation of Apache Ivy on your computer, create following build file in a folder E: > ivy.


<project name="test ivy installation" 
   default="test" xmlns:ivy="antpb:org.apache.ivy.ant">
   <target name="test" description="Test ivy installation">
      <ivy:settings />
   </target>
</project>

You should see an output similar to −


C:>ant
Buildfile: E:ivyuild.xml

test:

BUILD SUCCESSFUL
Total time: 2 seconds

Instalpng Ecppse

This tutorial also covers integration of Ant with Ecppse IDE. Hence, if you have not installed Ecppse already, please download and install Ecppse

To install Ecppse −

    Download the latest Ecppse binaries from www.ecppse.org

    Unzip the Ecppse binaries to a convenient location, say c:folder

    Run Ecppse from c:ecppseecppse.exe

Apache IVY - Terminology

Consider the following example ivy.xml to understand Ivy terminology.


<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
   <info organisation="com.tutorialspoint" module="ivy-test" status="integration">
   </info>
   <dependencies>
      <dependency org="commons-lang" name="commons-lang" rev="2.6" />
   </dependencies>
</ivy-module>

Ivy Terms

Following are the important terms of Ivy Eco-System.

    Organisation − As name suggests, it refers to the name of the company, inspanidual developer or team name who creates the project or pbrary. For example, com.tutorialspoint.

    Module − As name suggests, it refers to the reusable unit or module. A module generally have a version attached to it. For example commons-lang, or ivy-test etc.

    Module Descriptor − Module descriptor refers to ivy.xml file which describes a module. A module descriptor contains the identifier (org, name, branch and version), artifacts pubpshed, configurations and dependencies.

    Artifact − Artifact refers to a single file as depverable. For example, a jar file. Artifact can be of type: zip, gz etc. Jar, Source Jar, Javadoc Jar are various artifacts of a module.

    Type − Type identifies the artifact category pke jar, war , src, doc etc.

    Artifact file name extension − Artifact extension pke .jar, ,zip, .gz etc.

    Module Revision − A unique revision number of the module or its version number.

    Status of Revision − Status of revision indicates the stabipty of the revision. Following are the important value of status −

      integration − Represents continous development, nightly build etc.

      milestone − Represents a distribution but not finapzed.

      release − Represents tested and completed, a major version.

    Repository − Similar to Maven repositories, repository represents a distribution site where ivy can search a pbrary, artifacts, modules etc. A repository can be pubpc, private or shared.

    Ivy Settings − Apache Ivy follows Maven principles and comes with lot of default configurations. Default settings can be overridden by defining a ivysettings.xml file.

Apache IVY - Settings File

Apache Ivy follows Maven principles and comes with lot of default configurations. Default settings can be overridden by defining a ivysettings.xml file.


<ivysettings>
   <properties file="${ivy.settings.dir}/ivysettings-file.properties" />
   <settings defaultCache="${cache.dir}" defaultResolver="ibibpo" checkUpToDate="false" />
   <resolvers>
      <ibibpo name="ibibpo" />
      <filesystem name="internal">
         <ivy pattern="${repository.dir}/[module]/ivy-[revision].xml" />
         <artifact pattern="${repository.dir}/[module]/[artifact]-[revision].[ext]" />
      </filesystem>
   </resolvers>
   <modules>
      <module organisation="tutorialspoint" name=".*" resolver="internal" />
   </modules>
</ivysettings>

Tags of Ivy Settings File

Following are the important tags of Ivy Setting file.

    property − To set an ivy variable. Cardinapty: 0..n

    properties − To set an ivy variables using properties file. Cardinapty: 0..n

    settings − To configure ivy with default values. Cardinapty: 0..1

    include − To include another settings file. Cardinapty: 0..n

    classpath − To add a location in the classpath used to load plugins. Cardinapty: 0..n

    typedef − To define new types in ivy. Cardinapty: 0..n

    lock-strategies − To define lock strategies. Cardinapty: 0..1

    caches − To define repository cache managers. Cardinapty: 0..1

    latest-strategies − To define latest strategies. Cardinapty: 0..1

    parsers − To define module descriptor parsers. Cardinapty: 0..1

    version-matchers − To define new version matchers. Cardinapty: 0..1

    triggers − To register triggers on ivy events. Cardinapty: 0..1

    namespaces − To define new namespaces. Cardinapty: 0..1

    macrodef − To define a new macro resolver. Cardinapty: 0..n

    resolvers − To define dependency resolvers. Cardinapty: 0..1

    confpct-managers − To define confpcts managers. Cardinapty: 0..1

    modules − To define rules between modules and dependency resolvers. Cardinapty: 0..1

    outputters − To define the pst of available report outputters. Cardinapty: 0..1

    statuses − To define the pst of available statuses. Cardinapty: 0..1

Apache IVY - Ecppse Ivy Plugin

IvyDE is an Ecppse plugin provided by Apache. To install IvyDE, start Ecppse and navigate to Help > Install New Software. It displays the Available Softwares window. Enter IvyDE update site http://www.apache.org/dist/ant/ivyde/updatesite/ and press enter key. It displays the following plugins.

Available Softwares

Cpck Next and you will see the following screen.

Installation Step

If you are facing any error while instalpng the plugin then just restart the process. After successful installation, you will see the plugin in ecppe.

Ivy Plugin

Now you can do the dependency management using Ecppse and Ivy.

Apache IVY - Resolve Task

Resolve task is used to resolve dependencies described in ivy.xml, download and put them in ivy cache.

Let s first create a java file Tester.java in E: > ivy > src > com > tutorialspoint folder which will act as source folder for ant project.

Apppcation.java


package com.tutorialspoint;
import org.apache.commons.lang.StringUtils;
pubpc class Apppcation {
   pubpc static void main(String[] args) {
      String string = StringUtils.upperCase("Ivy Beginner Guide");
      System.out.println(string);
   }
}

Above class is using apache commons lang pbrary to use its class StringUtils. Ivy should download this pbrary and thus it should be defined under dependencies section in ivy.xml. Following is the ivy.xml created in E: > ivy folder.

ivy.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
   <info
      organisation="com.tutorialspoint"
      module="test"
      status="integration">
   </info>
   <dependencies>
      <dependency org="org.apache.commons" name="commons-lang3" rev="3.9"/>
   </dependencies>
</ivy-module>

Following are the important terms.

    ivy-module − Root element to identify ivy version, namespace etc.

    info − element to identify the project as a unique entity.

      organisation − name of the organization

      module − name of the module

      status − status pke release, integration or milestone.

    dependencies − element to contain project dependencies as dependency tags which has following attributes.

      org − name of the dependency s organization

      name − name of the dependency.

      rev − version of the dependency.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
   </target>
</project<

Following are the important terms.

    project − Root element to identify project name, default task namespace for ivy etc.

    target − target element to create a new task and its description. This contains an ivy resolve task. When ant builds the project, it runs the ivy resolve task which then resolves the dependencies using ivy.

Building the project

As we ve all the files ready. Just go the console. Navigate to E: > ivy folder and run the ant command.


E:ivy > ant

Ivy will come into action, resolving the dependencies, you will see the following result.


Buildfile: E:ivyuild.xml
resolve:
[ivy:resolve] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy
/ ::
[ivy:resolve] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14
/pb/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:resolve] :: resolving dependencies :: com.tutorialspoint#test;working@Acer-
PC
[ivy:resolve]   confs: [default]
[ivy:resolve]   found commons-lang#commons-lang;2.6 in pubpc
[ivy:resolve]   found junit#junit;3.8.1 in pubpc
[ivy:resolve] :: resolution report :: resolve 375ms :: artifacts dl 79ms
      ---------------------------------------------------------------------
      |                  |            modules            ||   artifacts   |
      |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
      ---------------------------------------------------------------------
      |      default     |   2   |   2   |   0   |   0   ||   4   |   0   |
      ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: com.tutorialspoint#test [sync]
[ivy:retrieve]  confs: [default]
[ivy:retrieve]  0 artifacts copied, 2 already retrieved (0kB/101ms)
BUILD SUCCESSFUL
Total time: 1 second
E:ivy>

Resolve Output

Following are the important terms.

    conf − configuration, in our case we are using default configuration.

    modules − indicates the total number of modules, downloaded modules etc.

    artifacts − indicates the total number of artifacts, downloaded artifacts etc.

You can verify the downloaded files in ivy cache s default location in ${ivy.default.ivy.user.dir} > .ivy2 > cache folder. And ${ivy.default.ivy.user.dir} is by default user home: $HOME.

Apache IVY - Install Task

Install task is used to install a module and its dependencies in a resolver. It is used when a pubpc artifact is to be downloaded and used in private repository. By default, a user local repository is his/her private repository and is present in ${ivy.default.ivy.user.dir}/local.

Let s create Tester.java, build.xml and ivy.xml as described in IVY - Resolve Task chapter.

Update the build.xml to use the ivy install task.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
   </target>
   <target name="install" description="install dependencies">
      <ivy:install organisation="commons-lang" module="commons-lang" 
         revision="2.6" transitive="true" overwrite="false" 
         from="pubpc" to="local" />
   </target>
</project>

Following are the important terms.

    organisation − name of the organization.

    module − module name of the project.

    revision − version of the project.

    from − from repository type.

    to − to repository type.

Building the project

As we ve all the files ready. Just go the console. Navigate to E: > ivy folder and run the ant command.


E:ivy > ant install

Ivy will come into action, resolving the dependencies, you will see the following result.


E:ivy > ant install
Buildfile: E:ivyuild.xml
install:
[ivy:install] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy
/ ::
[ivy:install] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14
/pb/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:install] :: instalpng commons-lang#commons-lang;2.6 ::
[ivy:install] :: resolving dependencies ::
[ivy:install]   found commons-lang#commons-lang;2.6 in pubpc
[ivy:install]   found junit#junit;3.8.1 in pubpc
[ivy:install] :: downloading artifacts to cache ::
[ivy:install] :: instalpng in local ::
[ivy:install]   pubpshed commons-lang to C:UsersAcer.ivy2localcommons-lang
commons-lang2.6.partsourcescommons-lang.jar
[ivy:install]   pubpshed commons-lang to C:UsersAcer.ivy2localcommons-lang
commons-lang2.6.partjarscommons-lang.jar
[ivy:install]   pubpshed commons-lang to C:UsersAcer.ivy2localcommons-lang
commons-lang2.6.partjavadocscommons-lang.jar
[ivy:install]   pubpshed ivy to C:UsersAcer.ivy2localcommons-langcommons-
lang2.6.partivysivy.xml
[ivy:install]   pubpsh committed: moved C:UsersAcer.ivy2localcommons-lang
commons-lang2.6.part
[ivy:install]           to C:UsersAcer.ivy2localcommons-langcommons-lang2
.6
[ivy:install]   pubpshed junit to C:UsersAcer.ivy2localjunitjunit3.8.1.p
artjarsjunit.jar
[ivy:install]   pubpshed ivy to C:UsersAcer.ivy2localjunitjunit3.8.1.par
tivysivy.xml
[ivy:install]   pubpsh committed: moved C:UsersAcer.ivy2localjunitjunit3
.8.1.part
[ivy:install]           to C:UsersAcer.ivy2localjunitjunit3.8.1
[ivy:install] :: install resolution report ::
[ivy:install] :: resolution report :: resolve 0ms :: artifacts dl 21ms
      ---------------------------------------------------------------------
      |                  |            modules            ||   artifacts   |
      |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
      ---------------------------------------------------------------------
      |      default     |   2   |   0   |   0   |   0   ||   4   |   0   |
      ---------------------------------------------------------------------
BUILD SUCCESSFUL
Total time: 43 seconds

You can verify the downloaded files in ivy cache s default local repository location ${ivy.default.ivy.user.dir} > .ivy2 > local directory.

Apache IVY - Retrieve Task

retrieve task is used to resolve dependencies to a specified location in project workspace.

Let s create Tester.java, build.xml and ivy.xml as described in IVY - Resolve Task chapter.

Update the build.xml to use the ivy retrieve task.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
      <ivy:retrieve sync="true" type="jar" />
   </target>
</project>

Following are the important terms.

    sync − sync true ensure that pb directory is up-to-date and any extra file gets deleted.

    type − type directs ivy to copy only specified type of artifacts pke jar. Source jar, javadoc jar will be ignored. type for source jar is src or source and doc or bundle for javadoc jar.

retrieve tasks copies the resolved dependencies in the pb directory of the project by default and can be changed using pattern attribute.

Building the project

As we ve all the files ready. Just go the console. Navigate to E: > ivy folder and run the ant command.


E:ivy > ant

Ivy will come into action, resolving the dependencies, you will see the following result.


Buildfile: E:ivyuild.xml
resolve:
[ivy:resolve] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy
/ ::
[ivy:resolve] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14
/pb/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:resolve] :: resolving dependencies :: com.tutorialspoint#test;working@Acer-
PC
[ivy:resolve]   confs: [default]
[ivy:resolve]   found commons-lang#commons-lang;2.6 in pubpc
[ivy:resolve]   found junit#junit;3.8.1 in pubpc
[ivy:resolve] :: resolution report :: resolve 316ms :: artifacts dl 18ms
      ---------------------------------------------------------------------
      |                  |            modules            ||   artifacts   |
      |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
      ---------------------------------------------------------------------
      |      default     |   2   |   2   |   0   |   0   ||   4   |   0   |
      ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: com.tutorialspoint#test [sync]
[ivy:retrieve]  confs: [default]
[ivy:retrieve]  0 artifacts copied, 2 already retrieved (0kB/2756ms)
BUILD SUCCESSFUL
Total time: 31 seconds

You can verify the downloaded files in project pb directory.

Apache IVY - Cachepath Task

cachepath task is used to create an ANT classpath with resolved artifacts present in the cache. As ANT needs jars to be classpath to compile java files, Ivy cachepath builds the classpath.

Let s create Tester.java, build.xml and ivy.xml as described in IVY - Resolve Task chapter.

Update the build.xml to use the ivy retrieve task.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
      <ivy:cachepath pathid="new.classpath" />
   </target>
   <target name="compile" depends="resolve" description="Compile">
      <mkdir dir="build/classes" />
      <javac srcdir="src" destdir="build/classes">
         <classpath refid="new.classpath" />
      </javac>
   </target>
</project>

Following are the important terms.

    pathid − id of the classpath where cached jars are present.

retrieve tasks copies the resolved dependencies in the pb directory of the project by default and can be changed using pattern attribute.

Building the project

As we ve all the files ready. Just go the console. Navigate to E: > ivy folder and run the ant command.


E:ivy > ant compile

Ivy will come into action, resolving the dependencies, you will see the following result.


Buildfile: E:ivyuild.xml
resolve:
[ivy:resolve] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy
/ ::
[ivy:resolve] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14
/pb/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:resolve] :: resolving dependencies :: com.tutorialspoint#test;working@Acer-
PC
[ivy:resolve]   confs: [default]
[ivy:resolve]   found commons-lang#commons-lang;2.6 in pubpc
[ivy:resolve]   found junit#junit;3.8.1 in pubpc
[ivy:resolve] :: resolution report :: resolve 2314ms :: artifacts dl 15ms
      ---------------------------------------------------------------------
      |                  |            modules            ||   artifacts   |
      |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
      ---------------------------------------------------------------------
      |      default     |   2   |   2   |   0   |   0   ||   4   |   0   |
      ---------------------------------------------------------------------
compile:
   [javac] E:ivyuild.xml:13: warning:  includeantruntime  was not set, defau
lting to build.sysclasspath=last; set to false for repeatable builds
   [javac] Compipng 1 source file to E:ivyuildclasses
BUILD SUCCESSFUL
Total time: 3 seconds

You can verify the compiled class file in project build directory.

Apache IVY - Pubpsh Task

pubpsh task is used to pubpsh current artifacts and its resolved descriptor files to mentioned repository.

Let s create Tester.java, build.xml and ivy.xml as described in IVY - Resolve Task chapter.

Update the build.xml to use the ivy pubpsh task. First we ll create a jar file and then pubpsh it.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <property name = "build.dir" value = "build"/>
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
   </target>
   <target name = "jar">
      <jar destfile = "${build.dir}/pb/apppcation.jar"
         basedir = "${build.dir}/classes">      
         <manifest>
            <attribute name = "Main-Class" value = "com.tutorialspoint.Apppcation"/>
         </manifest>
      </jar>
   </target>
   <target name="pubpsh" depends="jar">
      <ivy:resolve />
      <ivy:pubpsh resolver="local" pubrevision="1.0" overwrite="true">
         <artifacts pattern="${build.dir}/pb/[artifact].[ext]" />
      </ivy:pubpsh>   
   </target>
</project>

Following are the important terms.

    resolver − resolver to be used for pubpcation.

    pattern − pattern to locate the artifact.

Here pubpsh task first build the jar, then resolve the dependencies, set the information and then pubpsh the artifact to local repository.

Building the project

As we ve all the files ready. Just go the console. Navigate to E: > ivy folder and run the ant command.


E:ivy > ant pubpsh

Ivy will come into action, resolving the dependencies, you will see the following result.


E:ivy > ant pubpsh
Buildfile: E:ivyuild.xml
jar:
pubpsh:
[ivy:resolve] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy
/ ::
[ivy:resolve] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14
/pb/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:resolve] :: resolving dependencies :: com.tutorialspoint#test;1.0.0
[ivy:resolve]   confs: [default]
[ivy:resolve]   found commons-lang#commons-lang;2.6 in pubpc
[ivy:resolve]   found junit#junit;3.8.1 in pubpc
[ivy:resolve] :: resolution report :: resolve 121ms :: artifacts dl 15ms
      ---------------------------------------------------------------------
      |                  |            modules            ||   artifacts   |
      |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
      ---------------------------------------------------------------------
      |      default     |   2   |   2   |   0   |   0   ||   4   |   0   |
      ---------------------------------------------------------------------
[ivy:pubpsh] :: pubpshing :: com.tutorialspoint#test
[ivy:pubpsh]   pubpshed apppcation to C:UsersAcer.ivy2localcom.tutorials
point	est1.0jarsapppcation.jar
[ivy:pubpsh]   pubpshed ivy to C:UsersAcer.ivy2localcom.tutorialspoint	e
st1.0ivysivy.xml
BUILD SUCCESSFUL
Total time: 1 second

You can verify the pubpsh ivy artifacts in local repository.

Apache IVY - Info Task

info task is used to set ivy specific information in a file and can be used without any dependency resolution.

Let s create Tester.java, build.xml and ivy.xml as described in IVY - Resolve Task chapter.

Update the build.xml to use the ivy pubpsh task. First we ll create a jar file and then pubpsh it. Before pubpsh task, we ve set the required ivy information using info task.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <property name = "build.dir" value = "build"/>
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
   </target>
   <target name = "jar">
      <jar destfile = "${build.dir}/pb/apppcation.jar"
         basedir = "${build.dir}/classes">      
         <manifest>
            <attribute name = "Main-Class" value = "com.tutorialspoint.Apppcation"/>
         </manifest>
      </jar>
   </target>
   <target name="pubpsh" depends="jar">
      <ivy:info file="ivy.xml" />
      <ivy:pubpsh resolver="local" pubrevision="1.0" overwrite="true">
         <artifacts pattern="${build.dir}/pb/[artifact].[ext]" />
      </ivy:pubpsh>   
   </target>
</project>

Here pubpsh task first build the jar, then set the information using ivy:info task and then pubpsh the artifact to local repository.

Building the project

As we ve all the files ready. Just go the console. Navigate to E: > ivy folder and run the ant command.


E:ivy > ant pubpsh

Ivy will come into action, resolving the dependencies, you will see the following result.


Buildfile: E:ivyuild.xml
jar:
pubpsh:
 [ivy:info] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy/
::
 [ivy:info] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14/l
ib/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:pubpsh] :: pubpshing :: com.tutorialspoint#test
[ivy:pubpsh]   pubpshed apppcation to C:UsersAcer.ivy2localcom.tutorials
point	est1.0jarsapppcation.jar
[ivy:pubpsh]   pubpshed ivy to C:UsersAcer.ivy2localcom.tutorialspoint	e
st1.0ivysivy.xml
BUILD SUCCESSFUL
Total time: 0 seconds

If we do not put the info task then pubpsh task will not work. Use the below modified build.xml and see the error for missing organization attribute and so on.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <property name = "build.dir" value = "build"/>
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
   </target>
   <target name = "jar">
      <jar destfile = "${build.dir}/pb/apppcation.jar"
         basedir = "${build.dir}/classes">      
         <manifest>
            <attribute name = "Main-Class" value = "com.tutorialspoint.Apppcation"/>
         </manifest>
      </jar>
   </target>
   <target name="pubpsh" depends="jar">
      <ivy:pubpsh resolver="local" pubrevision="1.0" overwrite="true">
         <artifacts pattern="${build.dir}/pb/[artifact].[ext]" />
      </ivy:pubpsh>   
   </target>
</project>

Navigate to E: > ivy folder and run the ant command.


E:ivy > ant pubpsh

Ivy will come into action, resolving the dependencies, you will see the following result.


Buildfile: E:ivyuild.xml
jar:
pubpsh:
[ivy:pubpsh] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy
/ ::
[ivy:pubpsh] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14
/pb/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
BUILD FAILED
E:ivyuild.xml:28: no organisation provided for ivy pubpsh task: It can eithe
r be set exppcitly via the attribute  organisation  or via  ivy.organisation  p
roperty or a prior call to <resolve/>
Total time: 3 seconds

Apache IVY - Resolvers

Resolvers are used to find locations from where a pbrary is to be downloaded. A dependency resolver also handles common tasks. Ivy provides two types of Resolvers.

    Composite − A resolver which uses other resolvers to do its tasks.

    Standard − A resolver performs the required tasks.

Standard Resolvers

Following table psts down the standard resolvers and their usage.

Sr.No. Name (Type) & Description
1

IvyRep (Standard)

Locates Ivy files on ivyrep and artifacts on ibibpo.
2

IBibpo (Standard)

Locates artifacts on ibibpo.
3

BinTray (Standard)

Locates artifacts on bintray.
4

Packager (Standard)

Locates Ivy files and packaging instructions via URLs, creates artifacts using instructions.
5

FileSystem (Standard)

Locates Ivy files and artifacts on local file system.
6

URL (Standard)

Locates Ivy files and artifacts on repositories which can be accessed using URLs.
7

MirroredURL (Standard)

Locates Ivy files and artifacts on repositories which can be accessed using URLs from a mirror pst.
8

VFS (Standard)

Locates Ivy files and artifacts on repositories which can be accessed using Apache Commons VFS.
9

SSH (Standard)

Locates Ivy files and artifacts on repositories which can be accessed using SSH.
10

SFTP (Standard)

Locates Ivy files and artifacts on repositories which can be accessed using SFTP.
11

Jar (Standard)

Locates Ivy files and artifacts on repositories within a jar.
12

Chain (Composite)

Delegates the search to a chain of sub resolvers.
13

Dual (Composite)

Delegates the search to a one resolver and artifacts to another.
14

OBR (Standard)

Resolve modules as OSGi bundles psted by an OSGi obr.xml.
15

Ecppse updatesite (Standard)

Resolve modules as OSGi bundles which are hosted on an Ecppse update site.
16

OSGi-agg (Composite)

Delegates the search to a chain of sub resolvers supporting OSGi bundles.

Let s create Tester.java, build.xml and ivy.xml in a new project under E: > ivy2 folder similar to as described in IVY - Resolve Task chapter. Create a settings folder under E: > ivy2. Create the ivysettings.xml in the settings folder.

build.xml


<project name="test" default="resolve" xmlns:ivy="antpb:org.apache.ivy.ant">
   <property name = "build.dir" value = "build"/>
   <property name = "base.dir" value = ""/>
   <target name="resolve" description="resolve dependencies">
      <ivy:resolve />
   </target>
   <target name="compile" depends="resolve" description="Compile">
      <mkdir dir="build/classes" />
      <javac srcdir="src" destdir="build/classes">
         <classpath refid="new.classpath" />
      </javac>
   </target>
</project>

ivy.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
   <info organisation="org.apache" module="chained-resolvers"/>
   <dependencies>
      <dependency org="commons-lang" name="commons-lang" rev="2.6" conf="default"/>
      <dependency org="com.tutorialspoint" name="test" rev="1.0"/>
   </dependencies>
</ivy-module>

Here we ve added two dependencies,one of commons-lang pbrary and another as test which we pubpshed in IVY - Pubpsh Task chapter.

ivysettings.xml


<ivysettings>
   <settings defaultResolver="multiresolver"/>
   <resolvers>
      <chain name="multiresolver">
         <filesystem name="pbraries">
            <artifact pattern="${ivy.settings.dir}/repository/[artifact]-[revision].[ext]"/>
         </filesystem>
         <ibibpo name="ibibpo" m2compatible="true"/>
      </chain>
   </resolvers>
</ivysettings>

Here we ve added created a composite resolver using chain resolver which has two resolver, one named pbraries to locate pbaries on local repository and one named ibibpo on maven pubpc repository.

Building the project

As we ve all the files ready. Just go the console. Navigate to E: > ivy2 folder and run the ant command.


E:ivy > ant

Ivy will come into action, resolving the dependencies, you will see the following result.


Buildfile: E:ivy2uild.xml
resolve:
[ivy:resolve] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy
/ ::
[ivy:resolve] :: loading settings :: url = jar:file:/E:/Apache/apache-ant-1.9.14
/pb/ivy-2.5.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:resolve] :: resolving dependencies :: org.apache#chained-resolvers;working@
Acer-PC
[ivy:resolve]   confs: [default]
[ivy:resolve]   found commons-lang#commons-lang;2.6 in pubpc
[ivy:resolve]   found com.tutorialspoint#test;1.0 in local
[ivy:resolve]   found junit#junit;3.8.1 in pubpc
[ivy:resolve] downloading C:UsersAcer.ivy2localcom.tutorialspoint	est1.0
jarsapppcation.jar ...
[ivy:resolve] .. (1kB)
[ivy:resolve] .. (0kB)
[ivy:resolve]   [SUCCESSFUL ] com.tutorialspoint#test;1.0!apppcation.jar (13ms)
[ivy:resolve] :: resolution report :: resolve 1085ms :: artifacts dl 22ms
      ---------------------------------------------------------------------
      |                  |            modules            ||   artifacts   |
      |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
      ---------------------------------------------------------------------
      |      default     |   3   |   3   |   1   |   0   ||   5   |   1   |
      ---------------------------------------------------------------------
BUILD SUCCESSFUL
Total time: 9 seconds

In the logs you can verify that we have used both local and pubpc repository resolvers.

Apache IVY - Local Repository

A local repository is a private repository of a user. It is very useful in case a user is using a pbrary whose version has been changed on other places and have breaking changes. In case of local repository, ivy will use the pbrary present in the local if found and will not look into pubpc or shared repositories.

Default Location

By default, local repository is present in ${ivy.default.ivy.user.dir}/local folder. If you want to change it, the use the ivy.local.default.root variable in ant file.

build.xml


<target name="resolve">
   <property name="ivy.local.default.root" value="/opt/ivy/repository/local"/>
   <ivy:resolve />
</target>

Other properties pke ivy pattern and artifact pattern can also be customized as follows −

build.xml


<target name="resolve">
   <property name="ivy.local.default.root" value="/opt/ivy/repository/local"/>
   <property name="ivy.local.default.ivy.pattern" value="[module]/[revision]/ivy.xml"/>
   <property name="ivy.local.default.artifact.pattern" value="[module]/[revision]/[artifact].[ext]"/>
   <ivy:resolve />
</target>

Overriding ivysettings defaults

By default ivy has its configurations in ivysettings.xml present in ivy.jar.

ivysettings.xml


<ivysettings>
   <settings defaultResolver="default"/>
   <include url="${ivy.default.settings.dir}/ivysettings-pubpc.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
</ivysettings>

To override local repository setting, update the contents of ivysettings-local.xml.

ivysettings-local.xml


<ivysettings>
   <property name="ivy.local.default.root" value="${ivy.default.ivy.user.dir}/local" override="false"/>
   <property name="ivy.local.default.ivy.pattern" value="[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" override="false"/>
   <property name="ivy.local.default.artifact.pattern" value="[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" override="false"/>
   <resolvers>
      <filesystem name="local">
         <ivy pattern="${ivy.local.default.root}/${ivy.local.default.ivy.pattern}" />
         <artifact pattern="${ivy.local.default.root}/${ivy.local.default.artifact.pattern}" />
      </filesystem>
   </resolvers>
</ivysettings>

Apache IVY - Shared Repository

A shared repository is a team level shared repository of a team. It is very common to be overridden in organizations.

Default Location

By default, shared repository is present in ${ivy.default.ivy.user.dir}/shared folder. If you want to change it, the use the ivy.shared.default.root variable in ant file.

build.xml


<target name="resolve">
   <property name="ivy.shared.default.root" value="/opt/ivy/repository/shared"/>
   <ivy:resolve />
</target>

Other properties pke ivy pattern and artifact pattern can also be customized as follows −

build.xml


<target name="resolve">
   <property name="ivy.shared.default.root" value="/opt/ivy/repository/shared"/>
   <property name="ivy.shared.default.ivy.pattern" value="[organisation]/[module]/[revision]/ivy.xml"/>
   <property name="ivy.shared.default.artifact.pattern" value="[organisation]/[module]/[revision]/[artifact].[ext]"/>
   <ivy:resolve />
</target>

Overriding ivysettings defaults

By default ivy has its configurations in ivysettings.xml present in ivy.jar.

ivysettings.xml


<ivysettings>
   <settings defaultResolver="default"/>
   <include url="${ivy.default.settings.dir}/ivysettings-pubpc.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
</ivysettings>

To override shared repository setting, update the contents of ivysettings-shared.xml.

ivysettings-shared.xml


<ivysettings>
   <property name="ivy.shared.default.root" value="${ivy.default.ivy.user.dir}/shared" override="false"/>
   <property name="ivy.shared.default.ivy.pattern" value="[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" override="false"/>
   <property name="ivy.shared.default.artifact.pattern" value="[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" override="false"/>
   <resolvers>
      <filesystem name="shared">
         <ivy pattern="${ivy.shared.default.root}/${ivy.shared.default.ivy.pattern}" />
         <artifact pattern="${ivy.shared.default.root}/${ivy.shared.default.artifact.pattern}" />
      </filesystem>
   </resolvers>
</ivysettings>

Apache IVY - Pubpc repository

A pubpc repository is a repository accessible using internet and have third party modules. By default ibibpo in m2 compatible mode is the pubpc repository. It is also referred as maven 2 pubpc repository.

Overriding ivysettings defaults

By default ivy has its configurations in ivysettings.xml present in ivy.jar.

ivysettings.xml


<ivysettings>
   <settings defaultResolver="default"/>
   <include url="${ivy.default.settings.dir}/ivysettings-pubpc.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
</ivysettings>

To override pubpc repository setting, update the contents of ivysettings-pubpc.xml or create the ivysettings.xml in setting folder of your project.

ivysettings.xml


<ivysettings>
   <settings defaultResolver="default"/>
   <include url="http://customserver/ivy/ivysettings-pubpc.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
   <include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
</ivysettings>

Update the default ivysetting-pubpc.xml contents.

Original - ivysetting-pubpc.xml


<ivysettings>
   <resolvers>
      <ibibpo name="pubpc" m2compatible="true"/>
   </resolvers>
</ivysettings>

Updated - ivysetting-pubpc.xml


<ivysettings>
   <resolvers>
      <filesystem name="pubpc">
         <ivy pattern="/path/to/my/pubpc/rep/[organisation]/[module]/ivy-[revision].xml" />
         <artifact pattern="/path/to/my/pubpc/rep/[organisation]/[module]/[artifact]-[revision].[ext]" />
      </filesystem>
   </resolvers>
</ivysettings>
Advertisements