Chapter 6. Task packages

Several related tasks are bundled into task packages. Task packages are used for distributing logical groupings of tasks and also for isolating tasks with conflicting library dependencies. A task package is a Zip file that contains task implementations and task reference documentation.

Schmant comes with a basic set of task packages. More task packages can be downloaded from the Schmant site or from elsewhere on the Internet (well, at least in the future, hopefully).

Extra task packages that scripts want to use are put in the Schmant installation's task directory or listed on the Schmant command line with the -t option. See Chapter 3, Running Schmant. The task package Zip file can be used directly, or it can be unpacked to a directory.

The org.schmant.task.base task package is enabled by default. Other task packages must be enabled by a build script before it can use it. Enabling a task package adds a collection of Jar files to the running script's classpath and imports a set of Java packages so that fully qualified class names don't have to be used to reference task factories and other classes in the task package.

For most script languages, a task package is enabled by calling the enableTaskPackage method. See the scripting language guides.

In the example below, the classes compiled in Example 4.2, “Compile Java files and build a Jar file” are analyzed with Findbugs. The ExtFindbugsTF task factory is in the net.findbugs task package that is distributed with Schmant.

Example 6.1. Enabling the Findbugs task package and running Findbugs.

Groovy

// enableTaskPackage net.findbugs // The net.findbugs task package is enabled in the script manifest above. The // task package has to be enabled before the script is loaded so that the // classes in the task package are available to be imported below. import java.io.File import java.text.SimpleDateFormat import java.util.Date import org.entityfs.util.Directories import org.schmant.support.io.TempFileUtil import org.schmant.support.entityfs.SchmantFileSystems import org.schmant.task.findbugs.* import org.schmant.task.jdk.jar.JarTF import org.schmant.task.jdk.javac.jdk6.Jdk6JavacTF // Compile files from the directory /home/me/myproject/src and put the resulting // class files in a temporary directory. The source files have compilation // dependencies to all Jar files in /home/me/myproject/lib and to the Jar file // /home/me/myproject/optlib/opt.jar // A temporary java.io.File directory for the compiled classes. This, along with // all of its contents, will be automatically deleted when Schmant exits (unless // the -k flag is used). def ctarget = TempFileUtil.createTempDir() // This is the lib directory expressed as a (read only) EntityFS Directory. // By using a Directory, the script can use the utility methods of // Directories to extract the files it wants. See below. def libDir = SchmantFileSystems.getEntityForDirectory( new File("/home/me/myproject/lib"), true) // Get all jar files from the lib directory. The jar files are returned as a // Set of EntityFS EFile:s def depjars = Directories.getAllFilesMatching(libDir, "*.jar") // Add a dependency from the optlib directory. This is a Java File depjars.add(new File("/home/me/myproject/optlib/opt.jar")) // Compile the Java source files. new Jdk6JavacTF(). addSource(new File("/home/me/myproject/src")). addClasspathEntries(depjars). setTarget(ctarget).run() // A timestamp for the built archive def timestamp = new SimpleDateFormat("yyyyMMddHHmm"). format(new Date()) // Build the Jar file. Put it in /home/me/myproject new JarTF(). addSource(ctarget). setTarget(new File("/home/me/myproject/myproject" + timestamp + ".jar")). run() // Run Findbugs. new ExtFindbugsTF(). // If the findbugsExecutable property is not set, the task looks for the // findbugs executable by checking if the FINDBUGS_HOME environment variable // is set. If not, it searches for findbugs in the directories referenced by // the PATH environment variable. // setFindbugsExecutable("c:\\Java\\findbugs\\bin\\findbugs.bat"). // // Add classpath dependencies that should not be analyzed addAuxClasspathEntries(depjars). // Add all source files addSourceCodeContainer(new File("/home/me/myproject/src")). // The compiled classes are located under the directory ctarget (and // subdirectories) addSource(ctarget). setFindbugsReportFormat(FindbugsReportFormat.HTML). setTarget( new File("/home/me/myproject/myproject" + timestamp + ".html")).run()

JavaScript

// Compile files from the directory /home/me/myproject/src and put the resulting // class files in a temporary directory. The source files have compilation // dependencies to all Jar files in /home/me/myproject/lib and to the Jar file // /home/me/myproject/optlib/opt.jar // A temporary java.io.File directory for the compiled classes. This, along with // all of its contents, will be automatically deleted when Schmant exits (unless // the -k flag was used when Schmant was launched). ctarget = TempFileUtil.createTempDir(); // This is the lib directory expressed as a (read only) EntityFS Directory. // By using a Directory, the script can use the utility methods of // Directories to extract the files it wants. See below. libDir = SchmantFileSystems.getEntityForDirectory( new File("/home/me/myproject/lib"), true); // Get all jar files from the lib directory. The jar files are returned as a // Set of EntityFS EFile:s depjars = Directories.getAllFilesMatching(libDir, "*.jar"); // Add a dependency from the optlib directory. This is a Java File depjars.add(new File("/home/me/myproject/optlib/opt.jar")); // Compile the Java source files. new Jdk6JavacTF(). addSource(new File("/home/me/myproject/src")). addClasspathEntries(depjars). setTarget(ctarget).run(); // A timestamp for the built archive // java.text.SimpleDateFormat must be fully qualified since the java.text // package classes are not automatically imported by Schmant. The "Packages" // prefix is for telling JavaScript that it is a Java package. It is required // sometimes. See the chapter on script language support for more information. timestamp = new Packages.java.text.SimpleDateFormat("yyyyMMddHHmm"). format(new Date()); // Build the Jar file. Put it in /home/me/myproject new JarTF(). addSource(ctarget). setTarget(new File("/home/me/myproject/myproject" + timestamp + ".jar")). run(); enableTaskPackage("net.findbugs"); // Run Findbugs. new ExtFindbugsTF(). // If the findbugsExecutable property is not set, the task looks for the // findbugs executable by checking if the FINDBUGS_HOME environment variable // is set. If not, it searches for findbugs in the directories referenced by // the PATH environment variable. // setFindbugsExecutable("c:\\Java\\findbugs\\bin\\findbugs.bat"). // // Add classpath dependencies that should not be analyzed addAuxClasspathEntries(depjars). // Add all source files addSourceCodeContainer(new File("/home/me/myproject/src")). // The compiled classes are located under the directory ctarget (and // subdirectories) addSource(ctarget). setFindbugsReportFormat(FindbugsReportFormat.HTML). setTarget( new File("/home/me/myproject/myproject" + timestamp + ".html")).run();

JRuby

# Compile files from the directory /home/me/myproject/src and put the resulting # class files in a temporary directory. The source files have compilation # dependencies to all Jar files in /home/me/myproject/lib and to the Jar file # /home/me/myproject/optlib/opt.jar # A temporary java.io.File directory for the compiled classes. This, along with # all of its contents, will be automatically deleted when Schmant exits (unless # the -k flag was used when Schmant was launched). ctarget = Schmant::TempFileUtil.createTempDir # This is the lib directory expressed as a (read only) EntityFS Directory. # By using a Directory, the script can use the utility methods of # Directories to extract the files it wants. See below. # # File is not included in the Java module, so we have to access it through the # Java module instead. libDir = Schmant::SchmantFileSystems.getEntityForDirectory( Java::JavaIo::File.new("/home/me/myproject/lib"), true) # Get all jar files from the lib directory. The jar files are returned as a # Set of EntityFS EFile:s depjars = Schmant::Directories.getAllFilesMatching(libDir, "*.jar") # Add a dependency from the optlib directory. This is a Java File depjars.add(Java::JavaIo::File.new("/home/me/myproject/optlib/opt.jar")) # Compile the Java source files. Schmant::Jdk6JavacTF.new. addSource(Java::JavaIo::File.new("/home/me/myproject/src")). addClasspathEntries(depjars). setTarget(ctarget).run # A timestamp for the built archive timestamp = Java::JavaText::SimpleDateFormat.new("yyyyMMddHHmm"). format(Java::JavaUtil::Date.new) # Build the Jar file. Put it in /home/me/myproject Schmant::JarTF.new. addSource(ctarget). setTarget( Java::JavaIo::File.new("/home/me/myproject/myproject" + timestamp + ".jar")). run enableTaskPackage "net.findbugs" # Run Findbugs. Schmant::ExtFindbugsTF.new. # If the findbugsExecutable property is not set, the task looks for the # findbugs executable by checking if the FINDBUGS_HOME environment variable # is set. If not, it searches for findbugs in the directories referenced by # the PATH environment variable. # setFindbugsExecutable("c:\\Java\\findbugs\\bin\\findbugs.bat"). # # Add classpath dependencies that should not be analyzed addAuxClasspathEntries(depjars). # Add all source files addSourceCodeContainer(Java::JavaIo::File.new("/home/me/myproject/src")). # The compiled classes are located under the directory ctarget (and # subdirectories) addSource(ctarget). setFindbugsReportFormat(Schmant::FindbugsReportFormat::HTML). setTarget( Java::JavaIo::File.new("/home/me/myproject/myproject" + timestamp + ".html")). run

Jython

# Compile files from the directory /home/me/myproject/src and put the resulting # class files in a temporary directory. The source files have compilation # dependencies to all Jar files in /home/me/myproject/lib and to the Jar file # /home/me/myproject/optlib/opt.jar # A temporary java.io.File directory for the compiled classes. This, along with # all of its contents, will be automatically deleted when Schmant exits (unless # the -k flag was used when Schmant was launched). ctarget = TempFileUtil.createTempDir() # This is the lib directory expressed as a (read only) EntityFS Directory. # By using a Directory, the script can use the utility methods of # Directories to extract the files it wants. See below. libDir = SchmantFileSystems.getEntityForDirectory( File("/home/me/myproject/lib"), True) # Get all jar files from the lib directory. The jar files are returned as a # Set of EntityFS EFile:s depjars = Directories.getAllFilesMatching(libDir, "*.jar") # Add a dependency from the optlib directory. This is a Java File depjars.add(File("/home/me/myproject/optlib/opt.jar")) # Compile the Java source files. Jdk6JavacTF(). \ addSource(File("/home/me/myproject/src")). \ addClasspathEntries(depjars). \ setTarget(ctarget).run() # A timestamp for the built archive # # SimpleDateFormat is not automatically imported by the preparation # script. from java.text import SimpleDateFormat timestamp = SimpleDateFormat("yyyyMMddHHmm"). \ format(Date()) # Build the Jar file. Put it in /home/me/myproject JarTF(). \ addSource(ctarget). \ setTarget(File("/home/me/myproject/myproject" + timestamp + ".jar")). \ run() enableTaskPackage("net.findbugs") # Run Findbugs. # # If the findbugsExecutable property is not set, the task looks for the # findbugs executable by checking if the FINDBUGS_HOME environment variable is # set. If not, it searches for findbugs in the directories referenced by the # PATH environment variable. # # The compiled classes are located under the directory ctarget (and # subdirectories) ExtFindbugsTF(). \ addAuxClasspathEntries(depjars). \ addSourceCodeContainer(File("/home/me/myproject/src")). \ addSource(ctarget). \ setFindbugsReportFormat(FindbugsReportFormat.HTML). \ setTarget( \ File("/home/me/myproject/myproject" + timestamp + ".html")).run()

If a script uses task packages containing tasks with conflicting library dependencies, Schmant can be run with the classloader argument -c isolated, giving all task packages their own java.lang.ClassLoader:s. See Chapter 9, Advanced topics.

The task packages that are included with Schmant, are listed in the Task package index. The Task Author's Guide contains information on how to create new task packages.