Dieses Blog durchsuchen

Samstag, 24. September 2016

ant: retrieve , resolve and publish dependencies to nexus


For everyone who is looking for the basics in Ant, Ivy and Nexus, here is my simplest example how to connect, retrieve and publish  artifacts from nexus with ant and ivy

Prerequisits

We need some things installed on the local machine.
  • ant installed
    • apt-get install ant
  • nexus installed
    • https://github.com/sonatype/docker-nexus

Filestructure:

 <projectroot>
    |__build.xml
    |__ivy.xml
    |__ivysettings.xml

 

Setup the build.xml

We want to cleanup the local build folders, download some needed dependencies and cleanup the build cache. A very simple task, you would think. But have a look. That will come a little bit more complex now.

All single tasks will be defined in the build.xml. This file will be run by ant.

Its important, that you see the "depends" attribute on every single task, This attributes defines the order of processing the single task.

My build.xml
<project name="Testproject" default="init" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">
  <description>
    simple example build file
  </description>
  <!-- set global properties for this build -->
    <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>
  <property name="ivy-version" value="2.2.0"/>
  <property name="ivy.url" value="http://central.maven.org/maven2/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar"/>


<target name="init" depends="clean">
    <ivy:settings file="ivysettings.xml"/>
</target> 

<target name="resolve">
  <echo>Downloading dependencies defined in ivy.xml</echo>
    <get src="${ivy.url}" dest="${basedir}/.ant/lib/ivy-${ivy.version}.jar" skipexisting="true"/>

    <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpath="${basedir}/.ant/lib/ivy-${ivy.version}.jar"/>
    
    <ivy:retrieve pattern="target/modules/[artifact].[ext]" symlink="true" type="zip"/>
    
    <unzip dest=".">
      <fileset dir="target/modules"/>
    </unzip>
    
    <mkdir dir="target/buildlibs"/>
    
    <ivy:retrieve pattern="target/buildlibs/[artifact].[ext]" symlink="true" type="jar"/>
</target>

<target name="clean" description="Cleanup build directory">
    
</target>

<target name="clean-all" depends="init,clean" description="Clean and purge caches">
    <!-- Purge the ivy cache -->
    <ivy:cleancache/>
</target>
</project>
This sets up a "Testproject" in ant which uses ivy for configuring the build task properties. As you can see, we are defining some properties (src, build and dist).

There are 4 Task, which will be executed in following order.

clean
- this task cleans up the build folders. So all sources, which are only needed for this build can be deleted. Its getting called by the "init" task.

init:
- this is the task, which initializes the projectbuild starts the cleaning task first. As you can see, there is a file reference to ivysettings.xml.  Later I will explain that.

resolve:
- this task will download all needed buildtools and project dependencies, we have defined in a the central dependency managementfile named "ivy.xml".

 
clean-all
- At the end we will delete the ivy cache.


To make that buildfile work, we need 3 further more files. A "ivy.xml", a "ivysettings.xml" and a "build.properties file".

 

Setup the ivy.xml

In our build.xml in task init we defined that we want to use ivy and it's settings as our configuration tool to configure the build.xml

If you have a look in the ivy.xml you will see following code

ivy.xml
<ivy-module version="2.0">
    <info organisation="com.test" module="test.php.project"/>
    <publications>
        <artifact type="zip" ext="zip"/>
        <artifact type="pom" ext="pom"/>
    </publications>
    <dependencies>
     <dependency org="com-test" name="test" rev="1.1.1" transitive="false"/>
    </dependencies>
</ivy-module>

As you can see, we have 3 object here:

the info object
- This will define the organisation, which can be the reverted company homepage like "com.google" and the modulename, which defines the name of the softwarepackage

the publications object
-this defines the artifacts , which will be published at the end of the process. Because we will use the maven2 standard we will publish a .pom file, which contains the needed maven2 metadata for our buildartifact. You will see, thet nexus needs that pomfile to store the packageinformation.

the dependencies list
- here you will define your project or builddependencies, which will be downloaded and usedduring the build. All 3rd party libraries can be defined here and merged into your project later by a pecific ant task.
In that project we will simply download a test.zip package from organisation "com-test" in version 1.1.1
Please make sure, you have uploaded a similar file to your nexus repo, so that we can resolve this dependency

Setup the ivysettings.xml

This file you will need to store common buildinformation like credentials to access the artifacttool (nexus) or to define your resolvers to to download and publish your artifacts. 

This file gets included in your init task of your build.xml. Thats very important. otherwhise, your build task wont find its properties to work.

ivysettings.xml
<?xml version="1.0" encoding="UTF-8"?>
<ivysettings>
 <properties file="${user.home}/.ant/config/build.properties"/>

    <credentials 
     host="${repo.host}"
     port="${repo.port}" 
     realm="${repo.realm}" 
     username="${repo.username}" 
     passwd="${repo.password}"/>
    
 <settings defaultResolver="public"/>
 <resolvers>
  <ibiblio name="public" m2compatible="true" useMavenMetadata="false" root="${repo.protocoll}://${repo.host}:${repo.port}/content/repositories/php"/>
   
   <url name="publish" m2compatible="true">
      <artifact 
   pattern="${repo.protocoll}://${publish.host}:${repo.port}/content/repositories/releases/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"/>
  </url>
 </resolvers>
 <modules/>
</ivysettings>

As you can see, we are defining a property named "properties file". This means, that we will load our predefied set of properties from a file named "build.properties". Here we will store our secret accessdata to nexus. Every programmer has to install this file manually in his home dir


Further more you see a creentials object. This object uses the variables defined in the build.properties to store the accessdata to nexus. At last the ivysettings defined the url resolver. The resolver downloads and uploads the build artifacts to nexus under a defined url pattern,

 

Setup the build.properties

build.properties
repo.protocoll=http
repo.host=localhost
repo.port=8082
repo.realm=Sonatype Nexus Repository Manager
repo.username=admin
repo.password=admin123

Save this file to /<homedir>/.ant/config/build.properties

Make sure your nexus connectiondata are working!

 

Run your build

Now you can run ant init on the terminal

ant init
If everything went well you will get a Build success

Keine Kommentare:

Kommentar veröffentlichen