Monday, September 19, 2011

How to publish artifacts to ivy local repository and use it in another project

Build artifacts that are required by another project

ivy.xml: Attributes organisation, module and revision of element ivy-module/info.

1) Add a new resolver to ivy by adding following snippet to ivysettings.xml:

<filesystem name="gerald-local-ivy" m2compatible="false" force="false" local="true">
    <artifact pattern="${ivy.default.ivy.user.dir}/local/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/>
</filesystem>
This will create a directory local under <user.home>/.ivy2/.

2) Add following snippet to your build.xml:

<target name="publish" depends="jar" description="Publish">
    <!-- following property defines the version to publish -->
<property name="ivy.deliver.revision" value="${version}"/> <ivy:publish resolver="gerald-local-ivy" forcedeliver="true" settingsRef="${ant.project.name}.ivy.settings" overwrite="true"> <artifacts pattern="${build.dir}/[artifact]-[revision](-[classifier]).[ext]" /> </ivy:publish> </target>

The resolver attribute of ivy:publish must match name attribute specified in step 1). Change pattern attribute for artifacts element to match where you put the artifact.

3) Use command ant publish to publish your jar.

Build main project that depends on the artifacts built above.

1) Add the same resolver to ivy by adding following snippet to ivysettings.xml:

<filesystem name="gerald-local-ivy" m2compatible="false" force="false" local="true">
    <artifact pattern="${ivy.default.ivy.user.dir}/local/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/>
</filesystem>

2) Add it to your effective resolver chain.

3) Add dependency declaration to ivy.xml

<dependency org="<organization>" name="<module>" 
            rev="<version>" conf="<common->master>">
The attributes org, name, rev must match the values specified when you built the dependency jar.

4) Manually remove the artifacts that exist in local cache (<user.home>/.ivy2/cache).

5) Build your project

Resources

http://ant.apache.org/ivy/history/2.2.0/use/publish.html
http://ant.apache.org/ivy/history/latest-milestone/resolver/chain.html
http://stackoverflow.com/questions/353336/how-does-ivypublish-work
http://mail-archives.apache.org/mod_mbox/ant-ivy-user/201002.mbox/%3C27714488.post@talk.nabble.com%3E

No comments: