Using Maven Dependency Plugin to provision Virgo

The following XML snippet taken from a pom.xml file is a showcase how to provision a Virgo runtime with Maven. The Maven plugin maven-dependency-plugin is used to provision the artifacts.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>${maven.dependency.plugin.version}</version>
  <configuration>
    <outputDirectory>${env.VIRGO_HOME}/repository/usr</outputDirectory>
    <artifactItems>
      <artifactItem>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-lgpl</artifactId>
        <version>${jackson.version}</version>
      </artifactItem>
      <artifactItem>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-lgpl</artifactId>
        <version>${jackson.version}</version>
      </artifactItem>
    </artifactItems>
  </configuration>
</plugin>

To avoid the hard coding of the outputDirectory the snippet uses the environment variable: ${env.VIRGO_HOME}.

A small wrapper script can be used to enforce the presence of this environment variable and do some basic error handling:

function fail { echo 1>&2 FAILURE: "$*" ; exit 1; }
function cpchk { cp -avf $* || fail $1 not copied; }

[ -d "$VIRGO_HOME" ] || fail VIRGO_HOME not set.

# 3rd-party dependencies
mvn -f virgo-setup.xml dependency:copy || fail maven deployment failed.

echo
echo SUCCESS.

Running this script will provision all dependencies described as artifactItem into the Virgo runtime. The output should look like this:

$ sh setup.sh                                                                             [virgo-samples-dev]
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building guide-rest-service 0.1.0.BUILD-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.10:copy (default-cli) @ guide-rest-service ---
[INFO] Configured Artifact: org.codehaus.jackson:jackson-core-lgpl:1.9.13:jar
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/jackson/jackson-core-lgpl/1.9.13/jackson-core-lgpl-1.9.13.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/jackson/jackson-core-lgpl/1.9.13/jackson-core-lgpl-1.9.13.jar (233 KB at 252.8 KB/sec)
[INFO] Configured Artifact: org.codehaus.jackson:jackson-mapper-lgpl:1.9.13:jar
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/jackson/jackson-mapper-lgpl/1.9.13/jackson-mapper-lgpl-1.9.13.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/jackson/jackson-mapper-lgpl/1.9.13/jackson-mapper-lgpl-1.9.13.jar (768 KB at 1973.4 KB/sec)
[INFO] Copying jackson-core-lgpl-1.9.13.jar to /Users/fluffi/Projects/virgo-samples/tools/virgo-tomcat-server-latest/repository/usr/jackson-core-lgpl-1.9.13.jar
[INFO] Copying jackson-mapper-lgpl-1.9.13.jar to /Users/fluffi/Projects/virgo-samples/tools/virgo-tomcat-server-latest/repository/usr/jackson-mapper-lgpl-1.9.13.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.358 s
[INFO] Finished at: 2015-10-12T12:38:49+02:00
[INFO] Final Memory: 15M/302M
[INFO] ------------------------------------------------------------------------

SUCCESS.

Happy coding!