Sample usage of Commons Launcher

This documentation has been the base of the official Commons-launcher project example.

Presentation

The Jakarta Commons Launcher project provides a smart way to start a Java application.

In the facts, the Commons Launcher provides a bootstrap class which starts a ant process.

This ant process :

  • makes the needed pre execution tasks : create the needed classpath, check resources, download resources ..
  • invoke a 'launcher' task which allows to start a java application

Example

We want to start a small Main class :

 package org.kolaka.jdf.demo;
 
 import org.apache.commons.logging.LogFactory;
 
 public class Main {
   public static void main(String[] args) {
     LogFactory.getLog(Main.class).info("start"); 
   }
 }

We will name it “our application” :-).

Global distribution

We planify this runtime distribution :

  • dist/demo/bin/jdf2-demo.sh : the user script to start our small Main class
  • dist/demo/bin/ : contains the other files used by the script
  • dist/demo/etc/ : contains the configuration file (log4j configuration, ..)
  • dist/demo/lib/jdf2-demo.jar : contains the small Main class
  • dist/demo/lib/ : contains the needed jar files to the application
  • dist/demo/lib/launcher/ : contains the needed jar files to the commons launcher

bin directory

User script

the jdf2-demo.sh is the bootstrap script used by the user to start the application :

 #!/bin/sh
 
 basedir=`dirname $0`
 java -cp $basedir LauncherBootstrap -executablename `basename $0` demo

Launcher ant configuration

our launcher.xml is as simple as possible :

 <project name="JDF2 Demo Launcher" default="demo" basedir=".">
 
  <property name="base.dir" value="${basedir}/.."/>
 
  <property name="etc.dir" value="${base.dir}/etc"/>
  <property name="lib.dir" value="${base.dir}/lib"/>
  <property name="log.dir" value="${java.io.tmpdir}"/>
 
  <path id="base.class.path">
   <pathelement path="${etc.dir}"/>
   <fileset dir="${lib.dir}" includes="*.jar"/>
  </path>
 
  <target name="demo">
   <launch classname="org.kolaka.jdf.demo.Main">
    <classpath refid="base.class.path"/>
    <syspropertyset>
     <sysproperty key="log.dir" file="${log.dir}"/>
    </syspropertyset>
   </launch>
  </target>
 </project>

It creates the needed classpath and executes our Main class.

Other files

  • LauncherBootstrap.class, commons-launcher.jar : the needed classes the Commons Launcher
  • launcher.properties : the bootstrap configuration (used to find the lib/launcher files)

Build process

This is the target which prepares the dist/bin and dist/demo/lib/launcher directories (the entire ant script is included into the example sources) :

 <target name="dist.demo.bin">
 
  <!-- create the lib/launcher directory with the lib files provided by the commons launcher -->
  <copy todir="dist/demo/lib/launcher">
   <fileset dir="import/commons/launcher/lib">
    <include name="*.jar"/>
   </fileset>
  </copy>
 
  <!-- copy the needed files into the bin directory -->
  <copy todir="dist/demo/bin" flatten="true">
   <!-- from the commons launcher -->
   <fileset dir="import/commons/launcher/bin">
    <include name="LauncherBootstrap.class"/>
    <include name="commons-launcher.jar"/>
   </fileset>
   <!-- from the application sources -->
   <fileset dir="source/org/kolaka/jdf/demo">
    <include name="jdf2-demo.sh"/>
    <include name="launcher.xml"/>
   </fileset>
  </copy>
 
  <!-- make executable the shell scripts -->
  <chmod perm="+x">
   <fileset dir="dist/demo/bin">
    <include name="*.sh"/>
   </fileset>
  </chmod>
 
  <!-- create a property with the path to all 'lib/launcher' files -->
  <pathconvert property="dist.demo.bin.ant.class.path" pathSep=":" >
   <path>
    <fileset dir="dist/demo/lib/launcher">
     <include name="*.jar"/>
    </fileset>
   </path>
   <map from="${basedir}/dist/demo" to=".."/>
  </pathconvert>
 
  <!-- creates the 'launcher.properties' with this path -->	
  <propertyfile file="dist/demo/bin/launcher.properties">
   <entry key="ant.class.path" value="${dist.demo.bin.ant.class.path}"/>
  </propertyfile>
 
 </target>

Download

Links

 
sample_usage_of_commons_launcher.txt · Last modified: 2006/10/21 19:22 by alban
 
Except where otherwise noted, content on this wiki is licensed under the following license:Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Run by Debian Driven by DokuWiki