22.4. Multiple Wrapper Scripts

This example illustrates use of a target's control parameter to cause that target to be run multiple times. In this case, we set the java.wrapper parameter to a list of maps so that we can generate two wrapper scripts. One of them is used to test the library and code generator discussed in Section 22.3, “Code Generator Example for Groovy”, and also illustrates reading a file that was placed in the JAR by placing it in src/resources. The other main just prints a message. Here is the very ordinary Abuild.conf:

java/executable/Abuild.conf

name: executable
platform-types: java
deps: library

Here is the first main class:

java/executable/src/java/com/example/executable/Executable.java

package com.example.executable;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import com.example.library.Library;

public class Executable
{
    private void showTextFile()
    {
        try
        {
            InputStream is = getClass().getClassLoader().getResourceAsStream(
                "com/example/file.txt");
            if (is == null)
            {
                System.err.println("can't find com/example/file.txt");
                System.exit(2);
            }
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = r.readLine()) != null)
            {
                System.out.println(line);
            }
            r.close();
        }
        catch (IOException e)
        {
            System.err.println(e.getMessage());
        }
    }

    public static void main(String[] args)
    {
        if (args.length != 1)
        {
            System.err.println("Executable: one argument is required");
            System.exit(2);
        }

        int value = 0;
        try
        {
            Integer i = new Integer(args[0]);
            value = i.intValue();
        }
        catch (NumberFormatException e)
        {
            System.err.println("Executable: argument must be a number");
            System.exit(2);
        }

        Library lib = new Library(value);
        System.out.println("The opposite of " + value +
                           " is " + lib.getOppose());


        new Executable().showTextFile();
    }
}

Here is the file it reads from resources:

java/executable/src/resources/com/example/file.txt

This is a text file.

Here is the second main class:

java/executable/src/java/com/example/executable/Other.java

package com.example.executable;

public class Other
{
    public static void main(String[] args)
    {
        System.out.println("Here's another main just for show.");
    }
}

Finally, here is the Abuild.groovy file. Observe here how we set the java.wrapper parameter to a list of maps by appending one map at a time. This is one of many syntaxes that could be used, but it uses less extraneous punctuation than many of the other choices:

java/executable/Abuild.groovy

parameters {
    java.jarName = 'example-executable.jar'

    // Here we are going to generate multiple wrapper scripts.  We do
    // this by appending two different maps to the java.wrapper
    // parameter, each of which has a name key and a mainclass key.
    // There are many choices of syntax for doing this.  Here we use
    // Groovy's << operator to add something to a list.  We could also
    // have appended twice to java.wrapper in two separate statements,
    // or we could have explicitly assigned it to a list of maps.
    java.wrapper <<
        ['name': 'example',
         'mainclass' : 'com.example.executable.Executable'] <<
        ['name': 'other',
         'mainclass' : 'com.example.executable.Other']

    abuild.rules = 'java'
}