Logo

Publisher Shell Script






Home > Java Pub Prep Script > 



Java Pub Prep Script
Matthias Baerens
Contact
Links
Disclaimer



I store my source code in one file tree. While developing there come up some auxaliary files that should not be published. I needed a script that leeches given files into another directory and builds all files needed for publishing.

So I use a project directory that contains sub-directories as "projects". In this subdirectories there must be a file named FILES.txt. This file lists every file from the source code tree (relative paths!) that should be published. Another file "build.xml" must be there for the ant building tool. It must contain a special target that is described below.

These are the steps you have to do (presumed you habe an existing source tree and ant, zip and tar ready to run on your system):

  1. [ONCE]: Create a publishing directory where publishing files should be stored
  2. [ONCE]: Create a project directory where you store your publishing projects
  3. [ONCE]: Fit the first three lines in the script to your needs
  4. Create a sub-directory in your publishing directory as your project directory
  5. Place a file named FILES.txt into your project directory
  6. Place a build.xml file into your project directory
  7. Place some more files into your project directory if you want
  8. Be sure FILES.txt and build.xml fit the needs of the script
  9. Run the script

FILES.txt
Just list the files from your source tree you want to publish. The paths must be given relatively.
An example: Say, you have a directory layout like this:

/boot
/etc
/home
.    /user1
           /java
                /src
                    /pkg1
                         /Class1.java
                         /Class2.java
                         /Class3.java
                    /pkg2
                    /pkg21
                          /Class1.java
                          /Class2.java
                          /Class3.java
                    /pkg22
                          /Class1.java
                          /Class2.java
                          /Class3.java
                    /pkg23
                    /pkg3
                         /Class1.java
                         /Class2.java
                         /Class3.java
And /home/user1/java/src is your source tree root and you want to bundle pkg1.Class3, pkg21.Class1, pkg22.Class1 and pkg3.Class2 in one project. Then your FILE.txt would look like this:
pkg1/Class3.java
pkg21/Class1.java
pkg22/Class1.java
pkg3/Class2.java

build.xml
To make the script working correctly together with ant there must be a section in the build.xml file that build a lib.jar file in the parent directory from where it is called (will be the publishing directory in the script) and a document build part. My build.xml files do that as follows (bold parts are essential for the script).

<target name="xjar" depends="build">
	<jar destfile="../lib.jar" basedir="${build}" >
		<fileset dir="." excludes="${build}/**" />
	</jar>
	<delete dir="${build}" />
</target>

[...]

<target name="docs" depends="build">
	<jar destfile="../lib.jar" basedir="${build}" >
		<fileset dir="." excludes="${build}/**" />
	</jar>
	<delete dir="${build}" />
</target>

This is what the script does for you:

  1. Deleting all files in pubishing root
  2. Copy all files given in FILES.txt
  3. Create zip, tar, jar and docs
  4. Place them into a sub-directory of the publishing directory (named as in project directory)

ATTENTION! You may free use, edit or distribute this script as you like. But it comes with NO WARRANTY at all!

So here is the shell script:


# First fit the following three lines to your needs!
DEST=/path/to/your/pub/dest         # where to publish
SOURCES=/path/to/your/source/root   # souce home
PROJECTS=/path/to/your/projects     # project directory


echo "Clean up... $DEST"
rm -r $DEST/*

echo "copy from $SOURCES:"
cd $SOURCES

shopt -s extglob   # needed to exclude docs when deleting, see (*)

for folder in $PROJECTS/*; do
   path=$folder
   if test -d $folder; then         # isDirectory

       # Extracting project name and make a subfolder in $DEST
       IFS="/"
       set -- $folder 
       project=${*:$[$#]}
       echo $project
       mkdir --parents "$DEST/$project/src" 
       IFS=" "

       # copy source files
       while read line
       do
           echo "$SOURCES/$line => $DEST/$project/src/$line"
           cp -r $line --parents -t $DEST/$project/src
       done < "$path/FILES.txt"
       cp $path/* "$DEST/$project"


       # cd to project publishing dir and create packages and docs
       cd $DEST/$project
       zip -r ../$project.zip ./*       # create zip file
       tar cvzf ../$project.tar.gz ./*  # create tar.gz file
       ant xjar                         # create jar file
       ant docs                         # create documentation

       # at least clean up
       rm -r ./!(docs)                  # delete all but docs (*)
       mv ../$project.zip .
       mv ../$project.tar.gz .
       mv ../lib.jar ./$project.jar
   fi
done


END