Notes on maven

Some useful (to me) things that I did not readily find in one place on the web.

Setting up a simple local maven repository

Without any server, so on the local host.
  1. Determine where to set up the repository, create a corresponding directory
    	mkdir /home/me/maven-repository
    
  2. Add the following to your pom.xml
      	<project>
      	...
      	  <repositories> 
         	    <repository> 
                  <id>internal</id>
                  <url>file:///home/me/maven-repository</url>
                </repository>
              </repositories>
    	...
    	</project>
    
    Then you can mention stuff as a dependency in pom.xml in the usual way:
      	<project>
      	...
    	  <dependencies>
    	  ...
    	    <dependency>
                  <groupId>org.me.stuff</groupId>
          	      <artifactId>stuff</artifactId> 
                  <version>1.0.0-alpha</version>
                </dependency>
    	  ...
    	  </dependencies>
    	...
    	</project>
    
To add e.g. a jar file /home/me/lib/stuff-1.0.0-alpha.jar containing a package org.me.stuff to the local repository:
	mvn deploy:deploy-file -DgroupId=org.me.stuff -DartifactId=stuff \
        -Dversion=1.0.0-alpha -Dpackaging=jar \
        -Dfile=/home/me/lib/stuff-1.0.0-alpha.jar \
        -Durl=file:///home/me/maven-repository \
        -DrepositoryId=internal
The following script automates this:
#!/bin/bash
repository=/home/me/maven-repository
repository_id=internal
usage="$0 package path (e.g. $0 org.jaxen ~/download/jaxen-1.1.2.jar"
[ $# -eq 2 ] || { echo $usage; exit 1; }
package=$1
path=$2
file=$(basename $path)
id=$(echo $file| sed -e 's/-.*//')
version=$(echo $file| sed -e 's/[^-]*-//' | sed -e 's/.jar//')
echo "id=${id}, version=${version} .."
[ -f $path ] || { echo "Cannot open $path"; echo $usage; exit 1; }
mvn deploy:deploy-file \
  -DgroupId=${package} -DartifactId=${id} -Dversion=${version} \
  -Dpackaging=jar -Dfile=${path} -Durl=file://${repository} \
  -DrepositoryId=${repository_id}

Replicating an artifactory repository

While artifactory has a nice web interface, the command line tool artadmin is more practical, e.g. to replicate repositories. Unfortunately, artadmin help export is rather confusing about the source/target of the export, and the same for import. Hopefully the following script, which replicates a local artifactory repository to a remote one, clarifies things a bit.
#!/bin/bash
# This script assumes that both the local and remote artifactory
# servers run on port 8081 (the default) and use the same admin
# user name and password.
destination=remote.machine
user=admin
passwd=mypasswd
# artadmin lives in a directory which is usually not on ${PATH}
artadmin=/usr/local/artifactory-2.0.6/bin/artadmin
# artadmin creates exported archives in this directory
home=/home/artifactory
# the archive will be copied to ${destionation}:${tmpdir}
tmpdir=/tmp
# 'artadmin help export' is a bit confusing:
#   export puts its result in a directory ${home}/${dir}/YYYYMMDD.hhmmss
#   where ${dir} is the artadmin argument and YYYYMMDD.hhmmss
#   corresponds to the current time 
dir=$$
# we use a new ${dir} to ensure that YYYYMMDD.hhmmss is the only child
# directory in ${dir}, thus $(ls ${home}/${dir}) will yield YYYYMMDD.hhmmss .
${artadmin} export ${dir} --username ${user} --password ${passwd} --m2
archive=$(ls ${home}/${dir})
# copy the archive directory to ${destination}:${tmpdir}/${archive}
tar -C ${home}/${dir} -z -cf - ${archive} | ssh ${destination} "cd ${tmpdir}; tar zxf -"
# artadmin help import needs the YYYYMMDD.hhmmss directory as argument
ssh ${destination} ${artadmin} import ${tmpdir}/${archive} \
  --username ${user} --password ${passwd}
#
rm -fr ${home}/${dir}
ssh ${destination} rm -fr ${tmpdir}/${archive}
The script can be downloaded from copyartifactory.sh.
Dirk Vermeir (dvermeir@vub.ac.be) [Last modified: Mon Jun 1 17:46:22 CEST 2009 ]