Sunday, June 23, 2013



Maven2: Creating run time maven repository for external jar dependencies.

Recently I came across a situation where I had to consume a third party project on the nightly basis. Provisioning and deploying a maven repository management tool for just one of the jar files is not what I felt worth investing time and hardware resources.

Solution:
We asked third party vendor to keep pushing the required jar file to a stage location available to our build system. Added an extra maven module which serves the following purpose:

  • Location named lib which holds the required jar file.
  • Ant build.xml file which does “maven deploy:file” to the same location at the module level.
Module structure
stage
|-- build.xml
|-- pom.xml
 build.xml:
<exec executable="mvn">
  <arg line="-B deploy:deploy-file -Dfile=${basedir}/lib/&lt;required jar file>.jar -DgroupId=com.example.product -DartifactId=jarname  -Dversion=jarversion  -Dpackaging=jar -Durl=file://repo -DcreateChecksum=true"/>
</exec>

pom.xml:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
  <execution>
  <id>lib-repo</id>
  <phase>initialize</phase>
  <goals>
    <goal>run</goal>
  </goals>
    <configuration>
    <target>
     <ant target="default"/>
    </target>
  </configuration>
  </execution>
</executions>

  •  Above 2 steps will create in project maven repository at the module build stage.
To consume the dependencies added to the in module repository we need to modify the parent pom to add the repository at the repositories section as:
<repositories>
        <repository>
            <id>external-lib</id>
            <name>stage repo</name>
            <url>file://stage/repo</url>
        </repository>
</repositories>
and add the stage to the <modules> section in parent pom.

Further we are ready to consume the dependency “com.example.product:jarname” to any maven module after the stage module. 

Footnote: the above magic fails to work in Maven3. I think Maven3 tries to gather and resolve all the project modules dependency at the start of the maven reactor. In the case above repository is created at build time during the call of the stage module.

cheers, make world open.