Apache Ivy – Publish to Artifactory

Previously we have talked about how to work with Apache Ivy and Artifactory.

 

The next thing i would like to share is to publish your built .jar file to Artifactory.

1. Login to Artifactory web portal and create a new repository, click on the Admin tab and select Repository on the left menu side bar.
apache-ivy-publish-to-artifactory-1
 

2. Create a new local repository and named it test-snapshot-local.
apache-ivy-publish-to-artifactory-2
 

3. Edit the ivysettings.xml to setup the artifact pattern for publishing.

<ivysettings>
  <settings defaultResolver="main" />
  <credentials host="localhost" realm="Artifactory Realm" username="admin" passwd="password" />
  <resolvers>
    <chain name="main">
      <ibiblio name="public" m2compatible="true" root="http://localhost:8081/artifactory/repo" />
      <url name="publish">
        <!-- You can use  m2compatible="true" instead of specifying your own pattern -->
        <ivy pattern="http://localhost:8081/artifactory/test-snapshot-local/[organization]/[module]/[revision]/ivy-[revision].xml" />
        <artifact pattern="http://localhost:8081/artifactory/test-snapshot-local/[organization]/[module]/[revision]/[artifact].[ext]"/>
      </url>
    </chain>
  </resolvers>
</ivysettings>

 

4. Add the publish Ant target in build.xml.

<project name="hello-world-ant" basedir="." default="main" xmlns:ivy="antlib:org.apache.ivy.ant">

  <!-- Ant properties -->
  <property name="src.dir"     value="src"/>
  <property name="lib.dir"     value="lib"/>
  <property name="build.dir"   value="build"/>
  <property name="classes.dir" value="${build.dir}/classes"/>
  <property name="jar.dir"     value="${build.dir}/jar"/>
  <property name="main-class"  value="com.eureka.HelloWorld"/>

  <ivy:settings file="./ivysettings.xml" />

  <target name="clean">
    <delete dir="${build.dir}"/>
    <delete dir="${lib.dir}"/>
  </target>

  <target name="resolve">
    <ivy:retrieve/>
  </target>    
    
  <target name="report" depends="resolve">
    <ivy:report todir="${build.dir}"/>
  </target>

  <target name="compile" depends="report">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
  </target>

  <target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
      <manifest>
        <attribute name="Main-Class" value="${main-class}"/>
      </manifest>
    </jar>
  </target>

  <target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
  </target>

  <target name="publish" depends="jar">
    <ivy:publish resolver="publish" overwrite="true" artifactspattern="${jar.dir}/[artifact].[ext]" />
  </target>

  <target name="clean-build" depends="clean,jar"/>

  <target name="main" depends="clean,run"/>

</project>

 

5. Build and publish your project by running ant publish.
apache-ivy-publish-to-artifactory-3
 

6. Verify the setup in Artifactory.
apache-ivy-publish-to-artifactory-4
 

Done =)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s