how to publish 3rdparty artifacts with ivy and nexus

Nexus is primarily a Maven repository, this means one must adapt to the way Maven structures artifacts.

Since you’re focused on bulk loading Nexus I suggest looking at the answer to the following question:

Upload artifacts to Nexus, without Maven

If you wish to stick with ivy read on…..

Background

Need a Maven POM

Your first issue is that your Maven module(s) will need a POM file. This file describes the maven module and can be easily generated from the contents of your ivy.xml file (See solution below).

Secondly, Maven assumes that there is one primary artifact being built. For example:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myspotontheweb</groupId>
  <artifactId>donaldduck</artifactId>
  <version>1.0.1</version>
  <packaging>txt</packaging>
</project>

A Maven client would translate this information into the following URL:

http://<host>/<repo>/com/myspotontheweb/donaldduck/1.0.1/donaldduck-1.0.1.txt

This demonstrates how Nexus can store any type of binary dependency. The packaging parameter defaults to “jar”.

How maven handles additional module artifacts

While Maven focuses on a single build artifact, it is possible to add additional supplementary artifacts by posting them into the same directory (as you’ve done).

These are not listed in the Maven POM. Instead a Maven uses a special “classifier” attribute. The following is a possible dependency declaration.

<dependency>
  <groupId>com.myspotontheweb</groupId>
  <artifactId>donaldduck</artifactId>
  <version>1.0.1</version>
  <classifier>metadata</classifier>
  <type>n3</type>
</dependency>

A Maven client would translate this into the following URL:

http://<host>/<repo>/com/myspotontheweb/donaldduck/1.0.1/donaldduck-1.0.1-metadata.n3

Open source projects typically release their source code in this manner.

Ivy Solution

So finally how does one publish files into Nexus using ivy?

First of all decide which artifact is the “main” build artifact and add an additional entry for your POM file:

<ivy-module version='2.0' xmlns:e="http://ant.apache.org/ivy/extra">

    <info organisation="com.myspotonontheweb" module="donaldduck" revision="1.0.1"/>

    <publications>
        <artifact name="donaldduck" type="txt"/>
        <artifact name="donaldduck" type="pom"/>
        <artifact name="donaldduck" type="n3" e:classifier="metadata"/>
        <artifact name="donaldduck" type="zip" e:classifier="disto"/>
    </publications>

</ivy-module>

The other files can also be listed but each must have a unique classifier attribute….. Here you will be faced with one of the classic problems translating an ANT project into Maven…. Each jar file you publish, will probably need to have a separate POM. They not really “supplementary” artifacts…..

Pretending that you don’t need to publish multiple modules…. Use the following build targets to publish your module:

<target name="prepare" description="Generate POM">
    <!-- Optional: Intermediate file containing resolved version numbers -->
    <ivy:deliver deliverpattern="${build.dir}/ivy.xml" pubrevision="${publish.revision}" status="release"/>

    <!-- Generate the Maven POM -->
    <ivy:makepom ivyfile="${build.dir}/ivy.xml" pomfile="${build.dir}/donaldduck.pom"/>
</target>

<target name="publish" depends="init,prepare" description="Upload to Nexus">
    <ivy:publish resolver="nexus-deploy" pubrevision="${publish.revision}" overwrite="true" publishivy="false" >
        <artifacts pattern="${build.dir}/[artifact](-[classifier]).[ext]"/>
    </ivy:publish>
</target>

Nexus credentials

And for completeness here’s the ivysettings.xml file containing the Nexus repository location and credentials:

<ivysettings>
    <settings defaultResolver="nexus-central"/>
    <credentials host="somehost" realm="Sonatype Nexus Repository Manager" username="????" passwd="????"/>
    <resolvers>
        <ibiblio name="nexus-central" root="http://somehost/nexus/content/repositories/central/" m2compatible="true"/>
        <ibiblio name="nexus-deploy" root="http://somehost/nexus/content/repositories/repo" m2compatible="true"/>
    </resolvers>
</ivysettings>

Update

Downloading artifacts

To retrieve all the published artifacts (not just the main one), you need to list them as follows:

<dependency org="com.myspotontheweb" name="donaldduck" rev="1.0.1">
    <artifact name="donaldduck" type="txt"/>
    <artifact name="donaldduck" type="n3" e:classifier="metadata"/>
    <artifact name="donaldduck" type="zip" e:classifier="distro"/>
</dependency>

Functionally the same as the following Maven fragment:

<dependency>
  <groupId>com.myspotontheweb</groupId>
  <artifactId>donaldduck</artifactId>
  <version>1.0.1</version>
  <type>txt</type>
</dependency>

<dependency>
  <groupId>com.myspotontheweb</groupId>
  <artifactId>donaldduck</artifactId>
  <version>1.0.1</version>
  <classifier>metadata</classifier>
  <type>n3</type>
</dependency>

<dependency>
  <groupId>com.myspotontheweb</groupId>
  <artifactId>donaldduck</artifactId>
  <version>1.0.1</version>
  <classifier>distro</classifier>
  <type>zip</type>
</dependency>

Leave a Comment