Ivy Retrieve with Classifiers - ivy

I have the following ivy.xml:
<ivy-module version="1.0"
xmlns:maven="http://maven.apache.org">
<configurations>
...
</configurations>
<dependencies>
<dependency org="com.foo" name="fubur"
rev="1.3" conf="runtime->default"/>
<dependency org="com.snafu" name="barfu"
rev="1.4" conf="runtime->default">
<artifact name="barfu"
maven:classifier="ID_10T"
type="jar" ext="jar"/>
</dependency>
</dependencies>
</ivy-module>
In my build.xml, I want to retrieve all of my jars for the war I'm building:
<ivy:retrieve
pattern="${lib.dir}/[artifact]-[classifier]-[revision].[ext]"
conf="runtime"/>
No, that won't work... There's no classifier in fubar-1.3.jar. It will download as fubar--1.3.jar
<ivy:retrieve
pattern="${lib.dir}/[artifact]-[revision].[ext]"
conf="runtime"/>
That's no good either. barfu-ID_10T-1.4.jar will download as barfu-1.4.jar.
I would like the jars in my war to be included as barfu-ID_10T-1.4.jar and fubar-1.3-jar`. Is there an easy way of doing that? I know I could create two different configurations, but that is overkill. I'd rather just have the jars miss-named since it really doesn't affect the war itself.

Use parentheses to specify optional components of an attribute pattern:
<ivy:retrieve
pattern="${lib.dir}/[artifact](-[classifier])-[revision].[ext]"
conf="runtime"/>

Related

Why can't Ivy resolve my configurations

I've got an Ivy and Artifactory setup to publish and depend on builds with.
In Artifactory I have an Ivy file along the lines of:
<?xml version="1.0" encoding="UTF-8"?>
<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" module="module" branch="HEAD" revision="0.277-SNAPSHOT" status="integration" publication="20140724114055">
</info>
<configurations>
<conf name="compile" visibility="public" description="Default required to compile the full module"/>
<conf name="build" visibility="public" extends="compile" description="Incorporates additional build tools onto the classpath"/>
</configurations>
<publications defaultconf="compile">
<artifact name="module" type="jar" ext="jar"/>
<artifact name="module-src" type="source" ext="zip"/>
<artifact name="module-doc" type="doc" ext="zip"/>
</publications>
<dependencies>
<dependency org="junit" name="junit" rev="4.8.1" conf="compile->*"/>
<dependency org="net.sf.proguard" name="proguard" rev="4.11" conf="build">
<artifact name="proguard" ext="jar"/>
</dependency>
</dependencies>
</ivy-module>
When I come to depend on this module in another project I specify the compile configuration but get told by IvyDE that compile does not exist.
<?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"
module="module2"
status="integration">
</info>
<configurations>
<conf name="compile" description="Default required to compile the full module" />
</configurations>
<dependencies>
<dependency transitive="true" org="org" name="module" rev="latest.integration" conf="compile">
<artifact name="module" type="jar" ext="jar" />
<artifact name="module-src" type="source" ext="zip" />
<artifact name="module-doc" type="doc" ext="zip" />
</dependency>
</dependencies>
</ivy-module>
With this setup I get the error message
"configuration not found in org#module;0.277-SNAPSHOT: 'compile'"
I do get the jar if the dependency conf is updated to compile->* but I don't get the source or javadoc downloaded unless they are explicitly defined as dependencies. I am also expecting that junit will appear on the build path as it is defined as a compile dependency but there is no sign of that appearing while it doesn't know what the compile configuration is I guess.
Any suggestions please!
It seems the reason for this was Ivy not finding the ivy in artifactory correctly. Using a URL resolver rather than a ibiblio resolver and defining the ivy pattern everything is working correctly.

Can't Download json-lib in Ivy

I want to download json-lib-2.3-jdk15.jar. I find ivy don't have classifier tag, so I use maven one, below is the ivy.xml
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd"
xmlns:m="http://ant.apache.org/ivy/maven">
<info organisation="xxxx" module="xxx" status="integration"/>
<dependencies>
<dependency org="net.sf.json-lib" name="json-lib" rev="2.3">
<artifact name="json-lib" type="jar" m:classifier="jdk15"/>
</dependency>
</dependencies>
</ivy-module>
And ivysetting.xml
<ivysettings>
<settings defaultResolver="default" />
<include url="${ivy.default.settings.dir}/ivysettings-public.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"/>
<caches artifactPattern="[organisation]/[module]/([branch]/)[type]s/([platform]/)[artifact]-[revision](.[ext])" />
<resolvers>
<filesystem name="local">
<ivy
pattern="${ivy.local.default.root}/[organisation]/[module]/([branch]/)[revision]/ivy.xml" />
<artifact
pattern="${ivy.local.default.root}/[organisation]/[module]/([branch]/)[revision]/[type]s/([platform]/)[artifact](.[ext])" />
</filesystem>
<ibiblio name="public" m2compatible="true" usepoms="true" pattern="[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]" />
<chain name="default" returnFirst="true">
<resolver ref="local" />
<resolver ref="public"/>
</chain>
</resolvers>
</ivysettings>
But I still can't download it. It seems like m:classifier not work. Any suggestion about this? Thanks.
I finally find the root reason. I should not use
xmlns:m="http://ant.apache.org/ivy/maven
I should use
xmlns:m="http://ant.apache.org/ivy/extra
Another thing, in my case, I was add "conf" distribute in "artifact" tag, which will make the jar download fail. So do not add "conf" in "artifact".
Works for me.... What version of ivy are you using?
The following example contains some suggested enhancements
Example
Apache Ant(TM) version 1.8.2
Apache Ivy 2.3.0-rc2
ivy.xml
<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven">
<info organisation="xxxx" module="xxx"/>
<configurations>
<conf name="jdk15" description="JDK 1.5 dependencies"/>
</configurations>
<dependencies>
<dependency org="net.sf.json-lib" name="json-lib" rev="2.3" conf="jdk15->master">
<artifact name="json-lib" type="jar" m:classifier="jdk15"/>
</dependency>
</dependencies>
</ivy-module>
Notes:
It's always a good idea to use configurations. In this case I've created one called "jdk15" to group the files with jdk15 classifiers
The remote "master" configuration is special and contains no transitive dependencies (See following link for explanation)
How are maven scopes mapped to ivy configurations by ivy
ivysettings.xml
<ivysettings>
<settings defaultResolver="central" />
<resolvers>
<ibiblio name="central" m2compatible="true"/>
</resolvers>
</ivysettings>
Notes:
This is a minimal ivy settings file. (In fact you could omit the file and it would be functionally the same).
Are you using the "local" resolver? In my experience it's unnecessary (Unless you're publishing artifacts during your build)

How to take two revisions of the same jar?

I have two versions of the same jar (3.2 and 2.2.1) I need to use both of them but ivy evicts older revision. How to configure ivy to take two versions?
<dependency org="asm" name="asm-all" rev="3.2">
<artifact name="asm-all" type="jar"/>
</dependency>
<dependency org="asm" name="asm-all" rev="2.2.1">
<artifact name="asm-all" type="jar"/>
</dependency>
You need to use ivy configurations. This is a very flexible mechanism to manage arbitrary groups of dependencies.
The example below places each version of the jar onto a separate configuration. This can be used later to create two classpaths, using the ivy cachepath task.
Example
ivy.xml
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="compile1" description="Required to compile application1"/>
<conf name="compile2" description="Required to compile application2"/>
</configurations>
<dependencies>
<!-- compile1 dependencies -->
<dependency org="asm" name="asm-all" rev="3.2" conf="compile1->master"/>
<!-- compile2 dependencies -->
<dependency org="asm" name="asm-all" rev="2.2.3" conf="compile2->master"/>
</dependencies>
</ivy-module>
Notes:
Version 2.2.1 does not exist in Maven Central
Note the configuration mapping "??? -> master". In Maven the remote master configuration mapping resolves to the main module artifact without dependencies. (See)
build.xml
<project name="demo" default="init" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="init" description="Use ivy to resolve classpaths">
<ivy:resolve/>
<ivy:report todir='build/ivy' graph='false' xml='false'/>
<ivy:cachepath pathid="compile1.path" conf="compile1"/>
<ivy:cachepath pathid="compile2.path" conf="compile2"/>
</target>
<target name="clean" description="Clean built artifacts">
<delete dir="build"/>
</target>
<target name="clean-all" depends="clean" description="Additionally purge ivy cache">
<ivy:cleancache/>
</target>
</project>
Notes:
Always a good idea to generate an ivy report. It will tell you which dependencies exist on which ivy configuration.
This example shows ivy managing ANT paths. You can also use ivy configurations with the ivy retrieve task to populate a local "lib" directory when assembling something like a webapp WAR file.

Keep Ivy from including test dependencies

Consider an ivy.xml like the following:
<ivy-module version="2.0">
<info organisation="com.foo" module="FooBar" />
<dependencies>
<dependency org="net.sf.ehcache" name="ehcache-core" rev="2.2.0" />
<!--...-->
</dependencies>
</info>
</ivy-module>
When I run Ivy, it fetches all dependencies for EHCache, even testing dependencies. Specifically, it tries to pull in Hibernate 3.5.1 (which, in the POM file, is listed as a "test" dependency).
How do I prevent Ivy from including test dependencies? I could list it as an excluded dependency, but I don't want to have to do this for every test dependency. I'm new to Ivy and used to the way Maven does things. I was reading about configurations but I don't understand how this aspect of Maven's "scope" maps to "configurations."
You need to define the configuration of the dependency like:
<dependency org="net.sf.ehcache" name="ehcache-core" rev="2.2.0" conf="compile"/>
If you omit conf it is assumed, that you meant conf ="*", which will download all configurations for that dependency.
Here is a simple Example:
<configurations>
<conf name="test" visibility="public" />
<conf name="compile" visibility="public" />
</configurations>
<publications>
<artifact name="${project.name}" type="jar" conf="compile" ext="jar"/>
<artifact name="${project.name}-test" type="jar" conf="test" ext="jar"/>
</publications>
<dependencies>
<!-- COMPILE -->
<dependency org="log4j" name="log4j" rev="1.2.14" conf="compile->*"/>
<dependency org="apache" name="commons-net" rev="2.0" conf="compile->*"/>
<dependency org="itext" name="itext" rev="1.4.6" conf="compile->*"/>
<dependency org="jsch" name="jsch" rev="0.1.29" conf="test->*"/>
<!-- TEST -->
</dependencies>
In this example jsch will be included in the test and the compile configuration.
If you resolve this dependency later with conf ="compile" you will get all dependencies EXCEPT jsch.
If you resolve this dependency with conf ="test" you will get jsch only.
And if test would extend compile, you would get all jars.
<configurations>
<conf name="test" visibility="public" extends="compile" />
<conf name="compile" visibility="public" />
</configurations>

Split retrieved artifacts in two separate lib directories

In my web application, there are two separate lib directories:
/lib, and
/web/webroot/WEB-INF/lib.
The idea behind it is that libraries in the latter one are used by front-end code only, and the first one by both the front-end and the business logic code. There is a class loader in place which lets the business logic code not see the jars in /web/webroot/WEB-INF/lib.
How can I tell ivy that certain dependencies should go to the second directory while all others go to first one?
It's not trival since the the web class loader can see jars in both directories and I don't want jars to be in both directories.
Configurations are used to create logical groupings of dependencies:
ivy.xml
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="frontEnd" description="Jars used by front end"/>
<conf name="businessLogic" description="Jars used for business logic"/>
</configurations>
<dependencies>
<dependency org="commons-lang" name="commons-lang" rev="2.5" conf="businessLogic->default"/>
<dependency org="commons-codec" name="commons-codec" rev="1.4" conf="businessLogic->default"/>
<dependency org="commons-cli" name="commons-cli" rev="1.2" conf="frontEnd->default"/>
<dependency org="commons-logging" name="commons-logging" rev="1.1.1" conf="frontEnd->default"/>
</dependencies>
</ivy-module>
The ivy retrieve ant task can use these configurations to populate your directories:
build.xml
<target name="init" description="--> retrieve dependencies with ivy">
<ivy:retrieve conf="businessLogic" pattern="lib/[artifact].[ext]"/>
<ivy:retrieve conf="frontEnd" pattern="web/webroot/WEB-INF/lib/[artifact].[ext]"/>
</target>
Example
$ find . -type f
./build.xml
./ivy.xml
./lib/commons-lang.jar
./lib/commons-codec.jar
./web/webroot/WEB-INF/lib/commons-cli.jar
./web/webroot/WEB-INF/lib/commons-logging.jar