#!/bin/sh # The jar compiler. # Builds a jar file by compiling some java source and then # jaring up the resulting classes. # # (C) Tapsell-Ferrier Limited 2003 # Nic Ferrier, # Argument 1 MUST contain the name of the JAR file # Stdin MUST contain the list of the Java source files to compile. # The following environment variables MUST be present # JARCOMPILER_JAVAC the java compiler command # JARCOMPILER_JAVAC_OPTS options for the java compiler # JARCOMPILER_CLASSPATH the CLASSPATH for the compile # JARCOMPILER_JAR the jar command # # # About Stdin # The files passed on stdin need not be JUST java source files, # the jarcompiler script also recognises META-INF files and # a manifest.mf file. All META-INF files are added to the META-INF # directory in the resulting JAR file and the manifest.mf, if # present, is used as the resulting JAR file's manifest. # The first manifest.mf found in the input list is used. # What's the target jar file called? TARGETJAR= if [ $1 ] then TARGETJAR=$1 else echo 'No jar file specified.' exit 1 fi # Make the filelist from stdin. FILELIST=filelist-$$ tr ' ' '\012' | tr '\009' '\012' > $FILELIST # Add the file list to the clean up list export CLEANUP=${FILELIST} # Traps cause the CLEANUP to be removed trap 'rm -rf `echo ${CLEANUP} | tr ":" " "`' 1 2 3 15 # Make the list of source files. SOURCEFILELIST=sourcefilelist-$$ grep -e '\.java$' ${FILELIST} > $SOURCEFILELIST # Add the source file list to the clean up list. CLEANUP=${CLEANUP}:${SOURCEFILELIST} # Make the list of META-INF files METAINFCONTENTS=metainf-$$ grep -e '^META-INF/.*$' ${FILELIST} > $METAINFCONTENTS # Add the meta inf file list to the clean up list. CLEANUP=${CLEANUP}:${METAINFCONTENTS} # Find a manifest file. MANIFESTFILE=`grep -e 'manifest.mf$' ${FILELIST} | head -1` # Check the directory for temporary storage TARGETDIR=classes-$$ if [ -f $TARGETDIR ] then echo 'The target directory already exists.' exit 1 else mkdir $TARGETDIR # Add the target directory to the clean up list. CLEANUP=${CLEANUP}:${TARGETDIR} fi # Unpack existing target if necessary. if [ -f $TARGETJAR ] then ${JARCOMPILER_JAR} xf ${TARGETJAR} -C ${TARGETDIR} 2> /dev/null fi # Compile the java source files, quitting if it fails. FILESTOCOMPILE=`cat ${SOURCEFILELIST} | tr '\012' ' '` echo "CLASSPATH=${JARCOMPILER_CLASSPATH} ${JARCOMPILER_JAVAC} ${JARCOMPILER_JAVAC_OPTS} ${FILESTOCOMPILE}" CLASSPATH=$JARCOMPILER_CLASSPATH ${JARCOMPILER_JAVAC} ${JARCOMPILER_JAVAC_OPTS} -d ${TARGETDIR} @${SOURCEFILELIST} JAVACEXITVALUE=$? if [ $JAVACEXITVALUE -gt 0 ] then echo Compilation failed. rm -rf `echo ${CLEANUP} | tr ':' ' '` exit $JAVACEXITVALUE fi # Create the jar from the compiled classes, adding manifest if necessary. JAROPTS=cf if [ $MANIFESTFILE ] then JAROPTS="m${JAROPTS} ${MANIFESTFILE}" fi ${JARCOMPILER_JAR} ${JAROPTS} ${TARGETJAR} -C ${TARGETDIR} `ls ${TARGETDIR}` 2> /dev/null # Now remove all the temporary stuff (including the .class files) rm -rf `echo ${CLEANUP} | tr ':' ' '`