Chapter 8. Projects

Table of Contents

Building with the workspace builder task
Building manually
Eclipse workspaces
IntelliJ IDEA projects

A common scenario for a build script is to build and package a set of several interdependent source code modules such as different projects or modules in an IDE.

Schmant represents the collection of source code modules as a ProjectRepository that contains a collection of Project:s. There are two ProjectRepository implementations – EclipseWorkspace and IntelliJWorkspace. A Project may be a JavaProject or an IntelliJJavaProject.

The easiest way to build Java projects in a project repository is to use the JavaWorkspaceBuilderTF. It can build all or some of the projects in a ProjectRepository, and possibly also preprocess the source code before building and/or postprocess the class files after building.

The example below shows how all Java projects in an Eclipse workspace can be built.

Example 8.1. Build all projects in an Eclipse workspace using the workspace builder task.

Groovy

import org.schmant.project.eclipse.EclipseWorkspace import org.schmant.task.project.JavaWorkspaceBuilderTF // Create an EclipseWorkspace object for the workspace in the directory wos. // wos may be a File directory or an EntityFS Directory. def ewos = new EclipseWorkspace(wos) // ...if the workspace had been an IntelliJ IDEA project instead // def ewos = new IntelliJWorkspace( // wos, // new AbsoluteLocation("/MyProject.ipr")) // Create the build task and run it new JavaWorkspaceBuilderTF(). setWorkspace(ewos). // Put the classes in the directory bin setTarget(bin). run()

JavaScript

// Create an EclipseWorkspace object for the workspace in the directory wos. // wos may be a File directory or an EntityFS Directory. ewos = new EclipseWorkspace(wos); // ...if the workspace had been an IntelliJ IDEA project instead // ewos = new IntelliJWorkspace( // wos, // new AbsoluteLocation("/MyProject.ipr")); // Create the build task and run it new JavaWorkspaceBuilderTF(). setWorkspace(ewos). // Put the classes in the directory bin setTarget(bin). run();

JRuby

# Create an EclipseWorkspace object for the workspace in the directory wos. # wos may be a File directory or an EntityFS Directory. ewos = Schmant::EclipseWorkspace.new($wos) # ...if the workspace had been an IntelliJ IDEA project instead # ewos = IntelliJWorkspace.new( # $wos, # new AbsoluteLocation("/MyProject.ipr")) # Create the build task and run it Schmant::JavaWorkspaceBuilderTF.new. setWorkspace(ewos). # Put the classes in the directory bin setTarget($bin). run

Jython

# Create an EclipseWorkspace object for the workspace in the directory wos. # wos may be a File directory or an EntityFS Directory. ewos = EclipseWorkspace(wos) # ...if the workspace had been an IntelliJ IDEA project instead # ewos = IntelliJWorkspace( \ # wos, # AbsoluteLocation("/MyProject.ipr")) # Create the build task and run it. Put the compiled classes in the directory # bin. JavaWorkspaceBuilderTF(). \ setWorkspace(ewos). \ setTarget(bin). \ run()

The JavaWorkspaceBuilderTF automatically ignores all non-Java projects in the workspace. To further narrow down the selection of which projects to build, a Filter<Project> can be used with the task. There are a number of filter implementations bundled with Schmant:


All of Schmant's filter implementations also implement ConvenientFilter which adds convenience methods for combining different filters through logical operations such as AND or OR. All project filters also implement the marker interface ProjectFilter, which makes them easier to find in the API documentation.

The static methods in the ProjectFilterUtil class can be used for applying filters to collections of projects.

The functionality of JavaWorkspaceBuilderTF may not be enough for a build script. In that case, the build script can build a collection of projects manually. To do so, the script uses a ProjectDependencies object to sort out the TaskDependency:s between projects, and a TaskExecutor to schedule and run the build tasks. The ProjectDependencies object serves two purposes: it keeps track of a project's dependencies to other projects, and it is used to configure the build task for a project with the location of the built class files from each of the projects that it depends on.

The example below shows how all Java projects in an Eclipse workspace can be built manually.

Example 8.2. Build all projects in an Eclipse workspace manually

Groovy

import org.schmant.project.eclipse.EclipseWorkspace import org.schmant.project.java.* import org.schmant.run.TaskExecutor import org.schmant.task.jdk.javac.jdk6.Jdk6JavacTF // Create an EclipseWorkspace object for the workspace in the directory wos. // wos may be a File directory or an EntityFS Directory. def ewos = new EclipseWorkspace(wos) // Create a JavaProjectDependencies object to keep track of dependencies // between the projects in the workspace. def projDeps = new JavaProjectDependencies() // A TaskExecutor used for scheduling and running the compile tasks. def te = new TaskExecutor(). setNumberOfThreads(2). start() try { // Loop over all projects in the workspace. This assumes that all projects are // Java projects. See the next example for how to deal with non-Java projects. ewos.projects.each { proj -> def javacTask = new Jdk6JavacTF(). addSources(proj.sourceDirectories). setTarget(bin). addClasspathDecorator( // This classpath decorator is used to handle dependencies to the other // Java projects. It uses the JavaProjectDependencies object created // above. new JavaProjectClasspathDecorator(). setProject(proj). setDependencies(projDeps)).create() // Register this project's dependency with the dependency object. By doing // this, other projects that depend on this project will know when all their // dependencies are met. projDeps.registerDependency(proj, javacTask) // Register where the classes built from this project will be put. projDeps.registerClassDirectory(proj, bin) // Schedule this project for building. Get all dependencies for building // this project from the JavaProjectDependencies object. te.add(javacTask, projDeps.getDependencies(proj)) } te.waitFor() } finally { te.shutdown() }

JavaScript

// Create an EclipseWorkspace object for the workspace in the directory wos. // wos may be a File directory or an EntityFS Directory. ewos = new EclipseWorkspace(wos); // Create a JavaProjectDependencies object to keep track of dependencies // between the projects in the workspace. projDeps = new JavaProjectDependencies(); // A TaskExecutor used for scheduling and running the compile tasks. te = new TaskExecutor(). setNumberOfThreads(2). start(); try { // Get all projects from the workspace. This assumes that all projects are // Java projects. See the next example for how to deal with non-Java projects. itr = ewos.getProjects().iterator(); while(itr.hasNext()) { proj = itr.next(); javacTask = new Jdk6JavacTF(). addSources(proj.getSourceDirectories()). setTarget(bin). addClasspathDecorator( // This classpath decorator is used to handle dependencies to the other // Java projects. It uses the JavaProjectDependencies object created // above. new JavaProjectClasspathDecorator(). setProject(proj). setDependencies(projDeps)).create(); // Register this project's dependency with the dependency object. By doing // this, other projects that depend on this project will know when all their // dependencies are met. projDeps.registerDependency(proj, javacTask); // Register where the classes built from this project will be put. projDeps.registerClassDirectory(proj, bin); // Schedule this project for building. Get all dependencies for building // this project from the JavaProjectDependencies object. te.add(javacTask, projDeps.getDependencies(proj)); } te.waitFor(); } finally { te.shutdown(); }

JRuby

# Create an EclipseWorkspace object for the workspace in the directory wos. # wos may be a File directory or an EntityFS Directory. ewos = Schmant::EclipseWorkspace.new($wos) # Create a JavaProjectDependencies object to keep track of dependencies # between the projects in the workspace. projDeps = Schmant::JavaProjectDependencies.new # A TaskExecutor used for scheduling and running the compile tasks. te = Schmant::TaskExecutor.new. setNumberOfThreads(2). start begin # Get all projects from the workspace. This assumes that all projects are # Java projects. See the next example for how to deal with non-Java projects. ewos.projects.each do |proj| javacTask = Schmant::Jdk6JavacTF.new. addSources(proj.sourceDirectories). setTarget($bin). addClasspathDecorator( # This classpath decorator is used to handle dependencies to the other # Java projects. It uses the JavaProjectDependencies object created # above. Schmant::JavaProjectClasspathDecorator.new. setProject(proj). setDependencies(projDeps)).create() # Register this project's dependency with the dependency object. By doing # this, other projects that depend on this project will know when all their # dependencies are met. projDeps.registerDependency(proj, javacTask) # Register where the classes built from this project will be put. projDeps.registerClassDirectory(proj, $bin) # Schedule this project for building. Get all dependencies for building # this project from the JavaProjectDependencies object. te.add(javacTask, projDeps.getDependencies(proj)) end te.waitFor() ensure te.shutdown() end

Jython

# Create an EclipseWorkspace object for the workspace in the directory wos. # wos may be a File directory or an EntityFS Directory. ewos = EclipseWorkspace(wos) # Create a JavaProjectDependencies object to keep track of dependencies # between the projects in the workspace. projDeps = JavaProjectDependencies() # A TaskExecutor used for scheduling and running the compile tasks. te = TaskExecutor(). \ setNumberOfThreads(2). \ start() try: # Get all projects from the workspace. This assumes that all projects are # Java projects. See the next example for how to deal with non-Java projects. itr = ewos.getProjects().iterator(); while itr.hasNext(): proj = itr.next() # Create a compile task. # The classpath decorator is used to handle dependencies to the other Java # projects in the workspace. It uses the JavaProjectDependencies object # created above. javacTask = Jdk6JavacTF(). \ addSources(proj.getSourceDirectories()). \ setTarget(bin). \ addClasspathDecorator( \ JavaProjectClasspathDecorator(). \ setProject(proj). \ setDependencies(projDeps)).create() # Register this project's dependency with the dependency object. By doing # this, other projects that depend on this project will know when all their # dependencies are met. projDeps.registerDependency(proj, javacTask) # Register where the classes built from this project will be put. projDeps.registerClassDirectory(proj, bin) # Schedule this project for building. Get all dependencies for building # this project from the JavaProjectDependencies object. te.add(javacTask, projDeps.getDependencies(proj)) te.waitFor() finally: te.shutdown()

The example above made the assumption that all projects in the Eclipse workspace were Java projects, but of course this is not always the case. Use project filters like in the examples below to hide non-Java projects.

The EclipseWorkspace is used for representing an Eclipse workspace. It parses workspace and project information from the workspace and project configuration files, like a project's .project and .classpath files.

EclipseWorkspace supports Eclipse Java projects, classpath variables and user libraries. The workspace object tries to parse the values of classpath variables and user libraries from the workspace settings, if they are available. If the variable definitions are not available, or if the parsed values should be overridden, new values can be supplied in an EclipseWorkspaceSettings object instead when creating the workspace object.

EclipseWorkspace tries to create Project objects from all directories in the workspace directory. If there are project directories located elsewhere, the EclipseWorkspaceSettings object can be configured with their locations.

The examples below shows how all non-test Java projects in an Eclipse workspace can be built.

Example 8.3. Build a Jar file from the projects in an Eclipse workspace

Groovy

import java.io.File import org.schmant.project.eclipse.* import org.schmant.project.filter.ProjectNameGlobFilter import org.schmant.support.io.TempFileUtil import org.schmant.task.jdk.jar.JarTF import org.schmant.task.project.JavaWorkspaceBuilderTF // Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. // Create an EclipseWorkspaceSettings object that is used to override // information that is parsed from to the workspace configuration files. def settings = new EclipseWorkspaceSettings() // Add a classpath variable to the settings object. // The EclipseWorkspace object tries to parse the values of classpath // variables from the workspace metadata. If they are not defined there, or if // a value there should be overridden, classpath variable values may be added // manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable("MY_VARIABLE", new File("lib/mylib.jar")) def eWos = new EclipseWorkspace(wos, settings) // To create an Eclipse workspace object for the contents in the Schmant // process' working directory, use // def eWos = new EclipseWorkspace(new File("."), settings) // Create a temporary directory for putting the class files in. This directory // will be removed when the Schmant process exits. def tmpDir = TempFileUtil.createTempDirectory() // To create a temporary directory in memory: // def tmpDir = SchmantFileSystems.createRamFileSystem() new JavaWorkspaceBuilderTF(). setWorkspace(eWos). setTarget(tmpDir). // Don't build test projects. Negate the filter using the ~ operator. setProjectFilter(~(new ProjectNameGlobFilter("*_test"))). run() // Build the Jar file // The target file targetFile may be a File or a FutureFile. new JarTF(). setSource(tmpDir). setTarget(targetFile).run()

JavaScript

// Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. // Create an EclipseWorkspaceSettings object that is used to override // information that is parsed from to the workspace configuration files. settings = new EclipseWorkspaceSettings(); // Add a classpath variable to the settings object. // The EclipseWorkspace object tries to parse the values of classpath // variables from the workspace metadata. If they are not defined there, or if // a value there should be overridden, classpath variable values may be added // manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable("MY_VARIABLE", new File("lib/mylib.jar")); eWos = new EclipseWorkspace(wos, settings); // To create an Eclipse workspace object for the contents in the Schmant // process' working directory, use // eWos = new EclipseWorkspace(new File("."), settings); // Create a temporary directory for putting the class files in. This directory // will be removed when the Schmant process exits. tmpDir = TempFileUtil.createTempDirectory(); // To create a temporary directory in memory: // var tmpDir = SchmantFileSystems.createRamFileSystem(); new JavaWorkspaceBuilderTF(). setWorkspace(eWos). setTarget(tmpDir). // Don't build test projects. setProjectFilter(new ProjectNameGlobFilter("*_test").not()). run(); // Build the Jar file // The target file targetFile may be a File or a FutureFile. new JarTF(). setSource(tmpDir). setTarget(targetFile).run();

JRuby

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. # Create an EclipseWorkspaceSettings object that is used to override # information that is parsed from to the workspace configuration files. settings = Schmant::EclipseWorkspaceSettings.new # Add a classpath variable to the settings object. # The EclipseWorkspace object tries to parse the values of classpath # variables from the workspace metadata. If they are not defined there, or if # a value there should be overridden, classpath variable values may be added # manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable( "MY_VARIABLE", Java::JavaIo::File.new("lib/mylib.jar")) eWos = Schmant::EclipseWorkspace.new($wos, settings) # To create an Eclipse workspace object for the contents in the Schmant # process' working directory, use # eWos = Schmant::EclipseWorkspace.new( # Java::JavaIo::File.new("."), # settings) # Create a temporary directory for putting the class files in. This directory # will be removed when the Schmant process exits. tmpDir = Schmant::TempFileUtil.createTempDirectory # To create a temporary directory in memory: # tmpDir = Schmant::SchmantFileSystems.createRamFileSystem Schmant::JavaWorkspaceBuilderTF.new. setWorkspace(eWos). setTarget(tmpDir). # Don't build test projects. setProjectFilter(Schmant::ProjectNameGlobFilter.new("*_test").not). run # Build the Jar file # The target file targetFile may be a File or a FutureFile. Schmant::JarTF.new. setSource(tmpDir). setTarget($targetFile).run

Jython

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. # Create an EclipseWorkspaceSettings object that is used to override # information that is parsed from to the workspace configuration files. settings = EclipseWorkspaceSettings() # Add a classpath variable to the settings object. # The EclipseWorkspace object tries to parse the values of classpath # variables from the workspace metadata. If they are not defined there, or if # a value there should be overridden, classpath variable values may be added # manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable("MY_VARIABLE", File("lib/mylib.jar")) eWos = EclipseWorkspace(wos, settings) # To create an Eclipse workspace object for the contents in the Schmant # process' working directory, use # eWos = EclipseWorkspace(new File("."), settings) # Create a temporary directory for putting the class files in. This directory # will be removed when the Schmant process exits. tmpDir = TempFileUtil.createTempDirectory() # To create a temporary directory in memory: # var tmpDir = SchmantFileSystems.createRamFileSystem() JavaWorkspaceBuilderTF(). \ setWorkspace(eWos). \ setTarget(tmpDir). \ setProjectFilter(ProjectNameGlobFilter("*_test").not()). \ run() # Build the Jar file # The target file targetFile may be a File or a FutureFile. JarTF(). \ setSource(tmpDir). \ setTarget(targetFile).run()

Example 8.4. Build a Jar file from the projects in an Eclipse workspace manually

Groovy

import java.io.File import org.entityfs.util.Directories import org.schmant.project.eclipse.* import org.schmant.project.filter.ProjectNameGlobFilter import org.schmant.project.java.* import org.schmant.run.TaskExecutor import org.schmant.task.jdk.jar.JarTF import org.schmant.task.jdk.javac.jdk6.Jdk6JavacTF // Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. // Create an EclipseWorkspaceSettings object that is used to override // information that is parsed from the workspace configuration files. def settings = new EclipseWorkspaceSettings() // Add a classpath variable to the settings object. // The EclipseWorkspace object tries to parse the values of classpath // variables from the workspace metadata. If they are not defined there, or if // a value there should be overridden, classpath variable values may be added // manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable("MY_VARIABLE", new File("lib/mylib.jar")) def eWos = new EclipseWorkspace(wos, settings) // To create an Eclipse workspace object for the contents in the Schmant // process' working directory, use // def eWos = new EclipseWorkspace(new File("."), settings) // Create a JavaProjectDependencies object for keeping track of dependencies // between different projects. def projDeps = new JavaProjectDependencies() // This collection will contain all the directories with class files def classDirs = [] // The tmpDir variable points to a directory where temporary files are kept // during the build. It can for instance be created by calling // def tmpDir = TempFileUtil.createTempDirectory() // Create a TaskExecutor and start it def te = new TaskExecutor(). setNumberOfThreads(2). start() try { // Get all non-test Java projects from the workspace // In Groovy, project filters may be combined using Groovy operators. def javaProjects = eWos.getProjects( JavaProjectFilter.INSTANCE & ~(new ProjectNameGlobFilter("*_test"))) // Loop over all non-test Java projects and create compile tasks for them javaProjects.each { proj -> // Create a temporary directory for compiled files def classDir = Directories.newDirectory(tmpDir, proj.name) classDirs << classDir // Create the compilation task def javacTask = new Jdk6JavacTF(). // A project may have several source directories addSources(proj.sourceDirectories). setTarget(classDir). // Add a JavaProjectClasspathDecorator that uses the // JavaProjectDependencies object to give the compile task its classpath addClasspathDecorator( new JavaProjectClasspathDecorator(). setProject(proj). setDependencies(projDeps)).create() // Register the compile task as the dependency that this project hinges on projDeps.registerDependency(proj, javacTask) // Also register the class directory so that it can be found by projects // that depend on this project projDeps.registerClassDirectory(proj, classDir) // Add the compile task to the task executor. Use the projDeps object to // get the dependencies te.add(javacTask, projDeps.getDependencies(proj)) } // Create a Jar file with all the compiled classes // (Since this task depends on all the compile tasks, it might as well have // been defined after the task executor has been shut down.) te.add( new JarTF(). // targetFile is a FutureFile setTarget(targetFile). addSources(classDirs), // The ProjectDependencies object is also a TaskDependency // object that is satisfied when all projects have been built. projDeps) // Wait for all tasks to complete te.waitFor() } finally { te.shutdown() }

JavaScript

// Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. // Create an EclipseWorkspaceSettings object that is used to override // information that is parsed from the workspace configuration files. settings = new EclipseWorkspaceSettings(); // Add a classpath variable to the settings object. // The EclipseWorkspace object tries to parse the values of classpath // variables from the workspace metadata. If they are not defined there, or if // a value there should be overridden, classpath variable values may be added // manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable("MY_VARIABLE", new File("lib/mylib.jar")); eWos = new EclipseWorkspace(wos, settings); // To create an Eclipse workspace object for the contents in the Schmant // process' working directory, use // var eWos = new EclipseWorkspace(new File("."), settings); // Create a JavaProjectDependencies object for keeping track of dependencies // between different projects. projDeps = new JavaProjectDependencies(); // This collection will contain all the directories with class files classDirs = [] // The tmpDir variable points to a directory where temporary files are kept // during the build. It can for instance be created by calling // var tmpDir = TempFileUtil.createTempDirectory(); // Create a TaskExecutor and start it var te = new TaskExecutor(). setNumberOfThreads(2). start(); try { // Get all non-test Java projects from the workspace var javaProjects = eWos.getProjects( JavaProjectFilter.INSTANCE.and( new ProjectNameGlobFilter("*_test").not())); // Iterate over all non-test Java projects and create compile tasks for them itr = javaProjects.iterator(); while(itr.hasNext()) { proj = itr.next(); // Create a temporary directory for compiled files classDir = Directories.newDirectory(tmpDir, proj.getName()); classDirs.push(classDir); // Create the compilation task javacTask = new Jdk6JavacTF(). // A project may have several source directories addSources(proj.getSourceDirectories()). setTarget(classDir). // Add a JavaProjectClasspathDecorator that uses the // JavaProjectDependencies object to give the compile task its classpath addClasspathDecorator( new JavaProjectClasspathDecorator(). setProject(proj). setDependencies(projDeps)).create(); // Register the compile task as the dependency that this project hinges on projDeps.registerDependency(proj, javacTask); // Also register the class directory so that it can be found by projects // that depend on this project projDeps.registerClassDirectory(proj, classDir); // Add the compile task to the task executor. Use the projDeps object to // get the dependencies te.add(javacTask, projDeps.getDependencies(proj)); } // Create a Jar file with all the compiled classes // (Since this task depends on all the compile tasks, it might as well have // been defined after the task executor has been shut down.) te.add( new JarTF(). // targetFile is a FutureFile setTarget(targetFile). addSources(classDirs), // The ProjectDependencies object is also a TaskDependency // object that is satisfied when all projects have been built. projDeps); // Wait for all tasks to complete te.waitFor(); } finally { te.shutdown(); }

JRuby

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. # Create an EclipseWorkspaceSettings object that is used to override # information that is parsed from the workspace configuration files. settings = Schmant::EclipseWorkspaceSettings.new # Add a classpath variable to the settings object. # The EclipseWorkspace object tries to parse the values of classpath # variables from the workspace metadata. If they are not defined there, or if # a value there should be overridden, classpath variable values may be added # manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable( "MY_VARIABLE", Java::JavaIo::File.new("lib/mylib.jar")) eWos = Schmant::EclipseWorkspace.new($wos, settings) # To create an Eclipse workspace object for the contents in the Schmant process' # working directory, use # eWos = Schmant::EclipseWorkspace.new(java.io.File.new("."), settings); # Create a JavaProjectDependencies object for keeping track of dependencies # between different projects. projDeps = Schmant::JavaProjectDependencies.new # This array will contain all the directories with class files classDirs = [] # The tmpDir variable points to a directory where temporary files are kept # during the build. It can for instance be created by calling # tmpDir = Schmant::TempFileUtil.createTempDirectory # Create a TaskExecutor and start it te = TaskExecutor.new. setNumberOfThreads(2). start begin # Get all non-test Java projects from the workspace javaProjects = eWos.getProjects( Schmant::JavaProjectFilter::INSTANCE.and( Schmant::ProjectNameGlobFilter.new("*_test").not)) # Loop over all non-test Java projects and create compile tasks for them javaProjects.each do |proj| # Create a temporary directory for compiled files classDir = Schmant::Directories.newDirectory($tmpDir, proj.name) classDirs << classDir # Create the compilation task javacTask = Schmant::Jdk6JavacTF.new. # A project may have several source directories addSources(proj.sourceDirectories). setTarget(classDir). # Add a JavaProjectClasspathDecorator that uses the # JavaProjectDependencies object to give the compile task its classpath addClasspathDecorator( Schmant::JavaProjectClasspathDecorator.new. setProject(proj). setDependencies(projDeps)).create # Register the compile task as the dependency that this project hinges on projDeps.registerDependency(proj, javacTask) # Also register the class directory so that it can be found by projects # that depend on this project projDeps.registerClassDirectory(proj, classDir) # Add the compile task to the task executor. Use the projDeps object to # get the dependencies te.add(javacTask, projDeps.getDependencies(proj)) end # Create a Jar file with all the compiled classes # (Since this task depends on all the compile tasks, it might as well have # been defined after the task executor has been shut down.) te.add( Schmant::JarTF.new. # targetFile is a FutureFile setTarget($targetFile). addSources(classDirs), # The ProjectDependencies object is also a TaskDependency # object that is satisfied when all projects have been built. projDeps) # Wait for all tasks to complete te.waitFor ensure te.shutdown end

Jython

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. # Create an EclipseWorkspaceSettings object that is used to override # information that is parsed from the workspace configuration files. settings = EclipseWorkspaceSettings() # Add a classpath variable to the settings object. # The EclipseWorkspace object tries to parse the values of classpath # variables from the workspace metadata. If they are not defined there, or if # a value there should be overridden, classpath variable values may be added # manually to the EclipseWorkspaceSettings object. settings.addClasspathVariable("MY_VARIABLE", File("lib/mylib.jar")) eWos = EclipseWorkspace(wos, settings) # To create an Eclipse workspace object for the contents in the Schmant process' # working directory, use # eWos = EclipseWorkspace(File("."), settings) # Create a JavaProjectDependencies object for keeping track of dependencies # between different projects. projDeps = JavaProjectDependencies() # This list will contain all the directories with class files classDirs = [] # The tmpDir variable points to a directory where temporary files are kept # during the build. It can for instance be created by calling # tmpDir = TempFileUtil.createTempDirectory() # Create a TaskExecutor and start it te = TaskExecutor(). \ setNumberOfThreads(2). \ start() try: # Get all non-test Java projects from the workspace javaProjects = \ eWos.getProjects( \ JavaProjectFilter.INSTANCE.and( \ ProjectNameGlobFilter("*_test").not())) # Iterate over all non-test Java projects and create compile tasks for them itr = javaProjects.iterator() while itr.hasNext(): proj = itr.next() # Create a temporary directory for compiled files classDir = Directories.newDirectory(tmpDir, proj.getName()) classDirs.append(classDir) # Create the compilation task # Add a JavaProjectClasspathDecorator that uses the # JavaProjectDependencies object to give the compile task its classpath javacTask = Jdk6JavacTF(). \ addSources(proj.getSourceDirectories()). \ setTarget(classDir). \ addClasspathDecorator( \ JavaProjectClasspathDecorator(). \ setProject(proj). \ setDependencies(projDeps)).create() # Register the compile task as the dependency that this project hinges on projDeps.registerDependency(proj, javacTask) # Also register the class directory so that it can be found by projects # that depend on this project projDeps.registerClassDirectory(proj, classDir) # Add the compile task to the task executor. Use the projDeps object to # get the dependencies te.add(javacTask, projDeps.getDependencies(proj)) # Create a Jar file with all the compiled classes # (Since this task depends on all the compile tasks, it might as well have # been defined after the task executor has been shut down.) # # targetFile is a FutureFile. # # The ProjectDependencies object is also a TaskDependency # object that is satisfied when all projects have been built. te.add( JarTF(). \ setTarget(targetFile). \ addSources(classDirs), \ projDeps) # Wait for all tasks to complete te.waitFor() finally: te.shutdown()

The next example shows how the user library EriksLib is defined and used when compiling a workspace.

Example 8.5. Build an Eclipse workspace using a user-defined library

Groovy

import org.entityfs.util.Directories import org.schmant.project.eclipse.* import org.schmant.project.java.Library import org.schmant.task.project.JavaWorkspaceBuilderTF // Create an EclipseWorkspaceSettings object that is used to provide information // that cannot be parsed from its .project and .classpath files to the Eclipse // workspace object. def settings = new EclipseWorkspaceSettings() // The EriksLib library contains all Jar files in the lib directory. def eriksLib = new Library(). addEntries(Directories.getAllFilesMatching(lib, "*.jar")) // Add the user library to the settings object. The name used for the library is // the name used in the Eclipse project's .classpath file. settings.addLibrary("org.eclipse.jdt.USER_LIBRARY/EriksLib", eriksLib) // Create an EclipseWorkspace object for the workspace in the directory wos. // wos may be a File directory or an EntityFS Directory. def eWos = new EclipseWorkspace(wos, settings) // Build it new JavaWorkspaceBuilderTF(). setWorkspace(eWos). // This is a File directory or a Directory setTarget(bin). run()

JavaScript

// Create an EclipseWorkspaceSettings object that is used to provide information // that cannot be parsed from its .project and .classpath files to the Eclipse // workspace object. settings = new EclipseWorkspaceSettings(); // The EriksLib library contains all Jar files in the lib directory. eriksLib = new Library(). addEntries(Directories.getAllFilesMatching(lib, "*.jar")); // Add the user library to the settings object. The name used for the library is // the name used in the Eclipse project's .classpath file. settings.addLibrary("org.eclipse.jdt.USER_LIBRARY/EriksLib", eriksLib); // Create an EclipseWorkspace object for the workspace in the directory wos. // wos may be a File directory or an EntityFS Directory. eWos = new EclipseWorkspace(wos, settings); // Build it new JavaWorkspaceBuilderTF(). setWorkspace(eWos). // This is a File directory or a Directory setTarget(bin). run();

JRuby

# Create an EclipseWorkspaceSettings object that is used to provide information # that cannot be parsed from its .project and .classpath files to the Eclipse # workspace object. settings = Schmant::EclipseWorkspaceSettings.new # The EriksLib library contains all Jar files in the lib directory. eriksLib = Schmant::Library.new. addEntries(Schmant::Directories.getAllFilesMatching($lib, "*.jar")) # Add the user library to the settings object. The name used for the library is # the name used in the Eclipse project's .classpath file. settings.addLibrary("org.eclipse.jdt.USER_LIBRARY/EriksLib", eriksLib) # Create an EclipseWorkspace object for the workspace in the directory wos. # wos may be a File directory or an EntityFS Directory. eWos = EclipseWorkspace.new($wos, settings) # Build it Schmant::JavaWorkspaceBuilderTF.new. setWorkspace(eWos). # This is a File directory or a Directory setTarget($bin). run

Jython

# Create an EclipseWorkspaceSettings object that is used to provide information # that cannot be parsed from its .project and .classpath files to the Eclipse # workspace object. settings = EclipseWorkspaceSettings() # The EriksLib library contains all Jar files in the lib directory. eriksLib = Library(). \ addEntries(Directories.getAllFilesMatching(lib, "*.jar")) # Add the user library to the settings object. The name used for the library is # the name used in the Eclipse project's .classpath file. settings.addLibrary("org.eclipse.jdt.USER_LIBRARY/EriksLib", eriksLib) # Create an EclipseWorkspace object for the workspace in the directory wos. # wos may be a File directory or an EntityFS Directory. eWos = EclipseWorkspace(wos, settings) # Build it # bin is a File directory or a Directory JavaWorkspaceBuilderTF(). \ setWorkspace(eWos). \ setTarget(bin). \ run()

In the next two examples, all source files are preprocessed before they are compiled.

Example 8.6. Preprocess source files in Eclipse projects. Compile. Build Jar

Groovy

import org.entityfs.util.filter.entity.* import org.schmant.project.eclipse.EclipseWorkspace import org.schmant.support.io.TempFileUtil import org.schmant.task.jdk.jar.JarTF import org.schmant.task.meta.RecursiveProcessTF import org.schmant.task.project.JavaWorkspaceBuilderTF import org.schmant.task.text.TextReplaceTF // Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. def eWos = new EclipseWorkspace(wos) // An EntityFilter that hides Subversion .svn directories def noSvnFilter = ~(DirectoryFilter.FILTER & new EntityNameFilter(".svn")) // The tmpDir variable points to a directory where temporary files are kept // during the build. It can for instance be created by calling // tmpDir = TempFileUtil.createTempDirectory() new JavaWorkspaceBuilderTF(). setWorkspace(eWos). setTarget(tmpDir). // // Set a task factory for creating preprocess tasks. A task will be created // from this factory for each source directory for each project. The source // and target properties are set automatically. setPreprocessTaskFactory( // // Recurse through the source directory. new RecursiveProcessTF(). setTaskFactory( new TextReplaceTF(). addReplace("!!!VERSION!!!", "1.0"). // (For the documentation's unit tests. Pretend it's not here...) addReplace("232", "233"))). run() // Create a Jar file with all the compiled classes new JarTF(). // targetFile is a File or a FutureFile setTarget(targetFile). setSource(tmpDir). run()

JavaScript

// Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. eWos = new EclipseWorkspace(wos); // An EntityFilter that hides Subversion .svn directories noSvnFilter = DirectoryFilter.FILTER.and( new EntityNameFilter(".svn")).not(); // The tmpDir variable points to a directory where temporary files are kept // during the build. It can for instance be created by calling // tmpDir = TempFileUtil.createTempDirectory(); new JavaWorkspaceBuilderTF(). setWorkspace(eWos). setTarget(tmpDir). // // Set a task factory for creating preprocess tasks. A task will be created // from this factory for each source directory for each project. The source // and target properties are set automatically. setPreprocessTaskFactory( // // Recurse through the source directory. new RecursiveProcessTF(). setTaskFactory( new TextReplaceTF(). addReplace("!!!VERSION!!!", "1.0"). // (For the documentation's unit tests. Pretend it's not here...) addReplace("232", "233"))). run(); // Create a Jar file with all the compiled classes new JarTF(). // targetFile is a File or a FutureFile setTarget(targetFile). setSource(tmpDir). run();

JRuby

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. eWos = Schmant::EclipseWorkspace.new $wos # An EntityFilter that hides Subversion .svn directories noSvnFilter = Schmant::DirectoryFilter::FILTER.and( Schmant::EntityNameFilter.new(".svn")).not # The $tmpDir variable points to a directory where temporary files are kept # during the build. It can for instance be created by calling # tmpDir = TempFileUtil.createTempDirectory Schmant::JavaWorkspaceBuilderTF.new. setWorkspace(eWos). setTarget($tmpDir). # # Set a task factory for creating preprocess tasks. A task will be created # from this factory for each source directory for each project. The source # and target properties are set automatically. setPreprocessTaskFactory( # # Recurse through the source directory. Schmant::RecursiveProcessTF.new. setTaskFactory( Schmant::TextReplaceTF.new. addReplace("!!!VERSION!!!", "1.0"). # (For the documentation's unit tests. Pretend it's not here...) addReplace("232", "233"))). run # Create a Jar file with all the compiled classes Schmant::JarTF.new. # targetFile is a File or a FutureFile setTarget($targetFile). setSource($tmpDir).run

Jython

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. eWos = EclipseWorkspace(wos) # An EntityFilter that hides Subversion .svn directories noSvnFilter = DirectoryFilter.FILTER.and( EntityNameFilter(".svn")).not() # The tmpDir variable points to a directory where temporary files are kept # during the build. It can for instance be created by calling # tmpDir = TempFileUtil.createTempDirectory() # Build the workspace # # Set a task factory for creating preprocess tasks. A task will be created from # this factory for each source directory for each project. The source and target # target properties are set automatically. # # Recurse through the source directory. # # The replace 232 -> 233 is for the documentation's unit tests. Pretend it's not # there... JavaWorkspaceBuilderTF(). \ setWorkspace(eWos). \ setTarget(tmpDir). \ setPreprocessTaskFactory( RecursiveProcessTF(). \ setTaskFactory( TextReplaceTF(). \ addReplace("!!!VERSION!!!", "1.0"). \ addReplace("232", "233"))). \ run() # Create a Jar file with all the compiled classes # # targetFile is a File or a FutureFile JarTF(). \ setTarget(targetFile). \ setSource(tmpDir). \ run()

Example 8.7. Preprocess source files in Eclipse projects. Compile manually. Build Jar

Groovy

import org.entityfs.util.Directories import org.entityfs.util.filter.entity.* import org.schmant.arg.DirectoryAndFilter import org.schmant.project.eclipse.* import org.schmant.project.java.* import org.schmant.run.* import org.schmant.support.io.TempFileUtil import org.schmant.task.jdk.jar.JarTF import org.schmant.task.jdk.javac.jdk6.Jdk6JavacTF import org.schmant.task.meta.RecursiveProcessTF import org.schmant.task.text.TextReplaceTF // Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. // vars is a Map of the values of classpath variables used in // the workspace. These variables override the values parsed from the workspace // metadata. def settings = new EclipseWorkspaceSettings(). addClasspathVariables(vars) def eWos = new EclipseWorkspace(wos, settings) // Get all Java projects from the workspace def javaProjects = eWos.getProjects().findAll{ proj -> JavaProjectFilter.INSTANCE.matches(proj)} // An EntityFilter that hides Subversion .svn directories def noSvnFilter = ~(DirectoryFilter.FILTER & new EntityNameFilter(".svn")) // Create a JavaProjectDependencies object for keeping track of dependencies // between different projects. def projDeps = new JavaProjectDependencies() // This collection will contain all the directories with class files def classDirs = [] // Create a TaskExecutor and start it def te = new TaskExecutor(). setNumberOfThreads(props.getIntValue("noOfBuildThreads", 2)). start() try { // The tmpDir variable points to a directory where temporary files are kept // during the build. It can for instance be created by calling // def tmpDir = TempFileUtil.createTempDirectory() // Create compile tasks for each Java project javaProjects.each{ proj -> def projName = proj.name // Create a temporary directory to put the preprocessed source files in def sourceDir = Directories.newDirectory(tmpDir, projName + "_src") // Create the preprocess task for all source files def ppt = new RecursiveProcessTF(). // Preprocess all files in the directory hierarchy. Ignore Subversion's // .svn directories. addSources( DirectoryAndFilter.listWithFilter( Directories.newViews(proj.sourceDirectories, noSvnFilter), EFileFilter.FILTER)). setTarget(sourceDir). setTaskFactory( new TextReplaceTF(). addReplace("!!!VERSION!!!", "1.0"). // (For the documentation's unit tests. Pretend it's not here...) addReplace("232", "233")). create() te.add(ppt) // Create a temporary directory for compiled files def classDir = Directories.newDirectory(tmpDir, projName) classDirs.add(classDir) // Create the compilation task def javacTask = new Jdk6JavacTF(). addSource(sourceDir). setTarget(classDir). // Add a JavaProjectClasspathDecorator that uses the // JavaProjectDependencies object created above to give the compile // task its classpath addClasspathDecorator( new JavaProjectClasspathDecorator(). setProject(proj). setDependencies(projDeps)).create() // Register the compile task as the dependency that this project hinges on. // (If we would post process the compiled class files in any way, for // instance Emma instrument them, the post process task would (probably) be // the one that was registered here.) projDeps.registerDependency(proj, javacTask) // Also register the class directory so that it can be found by projects // that depend on this project. projDeps.registerClassDirectory(proj, classDir) // Add the compile task to the task executor. Now the compile task hinges // on both it's dependent projects and the preprocess task te.add( javacTask, new CompoundTaskDependency(). addAll(projDeps.getDependencies(proj)). add(ppt)) } // Create a Jar file with all the compiled classes te.add( new JarTF(). // targetFile is a File or a FutureFile setTarget(targetFile). addSources(classDirs), // This is also a dependency object for all compilation tasks. projDeps) // Wait for all tasks to complete te.waitFor() } finally { te.shutdown() }

JavaScript

// Create the Eclipse workspace object for the contents in the directory wos. // wos may be a File directory or an EntityFS Directory. // vars is a Map of the values of classpath variables used in // the workspace. These variables override the values parsed from the workspace // metadata. settings = new EclipseWorkspaceSettings(). addClasspathVariables(vars); eWos = new EclipseWorkspace(wos, settings); // Get all Java projects from the workspace javaProjects = eWos.getProjects( JavaProjectFilter.INSTANCE); // An EntityFilter that hides Subversion .svn directories noSvnFilter = DirectoryFilter.FILTER.and( new EntityNameFilter(".svn")).not(); // Create a JavaProjectDependencies object for keeping track of dependencies // between different projects. projDeps = new JavaProjectDependencies(); // This collection will contain all the directories with class files classDirs = new ArrayList(); // Create a TaskExecutor and start it te = new TaskExecutor(). setNumberOfThreads(props.getIntValue("noOfBuildThreads", 2)). start(); try { // The tmpDir variable points to a directory where temporary files are kept // during the build. It can for instance be created by calling // var tmpDir = TempFileUtil.createTempDirectory(); // Iterate over all Java projects and create compile tasks for them itr = javaProjects.iterator(); while(itr.hasNext()) { proj = itr.next(); projName = proj.getName(); // Create a temporary directory to put the preprocessed source files in sourceDir = Directories.newDirectory(tmpDir, projName + "_src"); // Create the preprocess task for all source files ppt = new RecursiveProcessTF(). // Preprocess all files in the directory hierarchy. Ignore Subversion's // .svn directories. addSources( DirectoryAndFilter.listWithFilter( Directories.newViews(proj.getSourceDirectories(), noSvnFilter), EFileFilter.FILTER)). setTarget(sourceDir). setTaskFactory( new TextReplaceTF(). addReplace("!!!VERSION!!!", "1.0"). // (For the documentation's unit tests. Pretend it's not here...) addReplace("232", "233")). create(); te.add(ppt); // Create a temporary directory for compiled files classDir = Directories.newDirectory(tmpDir, projName); classDirs.add(classDir); // Create the compilation task javacTask = new Jdk6JavacTF(). addSource(sourceDir). setTarget(classDir). // Add a JavaProjectClasspathDecorator that uses the // JavaProjectDependencies object created above to give the compile // task its classpath addClasspathDecorator( new JavaProjectClasspathDecorator(). setProject(proj). setDependencies(projDeps)).create(); // Register the compile task as the dependency that this project hinges on. // (If we would post process the compiled class files in any way, for // instance Emma instrument them, the post process task would (probably) be // the one that was registered here.) projDeps.registerDependency(proj, javacTask); // Also register the class directory so that it can be found by projects // that depend on this project. projDeps.registerClassDirectory(proj, classDir); // Add the compile task to the task executor. Now the compile task hinges // on both it's dependent projects and the preprocess task te.add( javacTask, new CompoundTaskDependency(). addAll(projDeps.getDependencies(proj)). add(ppt)); } // Create a Jar file with all the compiled classes te.add( new JarTF(). // targetFile is a File or a FutureFile setTarget(targetFile). addSources(classDirs), // This is also a dependency object for all compilation tasks. projDeps); // Wait for all tasks to complete te.waitFor(); } finally { te.shutdown(); }

JRuby

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. # vars is a Map of the values of classpath variables used in # the workspace. These variables override the values parsed from the workspace # metadata. settings = Schmant::EclipseWorkspaceSettings.new. addClasspathVariables($vars) eWos = Schmant::EclipseWorkspace.new($wos, settings) # Get all Java projects from the workspace javaProjects = eWos.getProjects Schmant::JavaProjectFilter::INSTANCE # An EntityFilter that hides Subversion .svn directories noSvnFilter = Schmant::DirectoryFilter::FILTER.and( Schmant::EntityNameFilter.new(".svn")).not # Create a JavaProjectDependencies object for keeping track of dependencies # between different projects. projDeps = Schmant::JavaProjectDependencies.new # This list will contain all the directories with class files classDirs = [] # Create a TaskExecutor and start it te = Schmant::TaskExecutor.new. setNumberOfThreads($props.getIntValue("noOfBuildThreads", 2)). start begin # The tmpDir variable points to a directory where temporary files are kept # during the build. It can for instance be created by calling # tmpDir = TempFileUtil.createTempDirectory # Iterate over all Java projects and create compile tasks for them javaProjects.each do |proj| projName = proj.name # Create a temporary directory to put the preprocessed source files in sourceDir = Schmant::Directories.newDirectory($tmpDir, projName + "_src") # Create the preprocess task for all source files ppt = Schmant::RecursiveProcessTF.new. # Preprocess all files in the directory hierarchy. Ignore Subversion's # .svn directories. addSources( Schmant::DirectoryAndFilter.listWithFilter( Schmant::Directories.newViews(proj.sourceDirectories, noSvnFilter), Schmant::EFileFilter::FILTER)). setTarget(sourceDir). setTaskFactory( Schmant::TextReplaceTF.new. addReplace("!!!VERSION!!!", "1.0"). # (For the documentation's unit tests. Pretend it's not here...) addReplace("232", "233")).create te.add ppt # Create a temporary directory for compiled files classDir = Schmant::Directories.newDirectory($tmpDir, projName) classDirs.push classDir # Create the compilation task javacTask = Schmant::Jdk6JavacTF.new. addSource(sourceDir). setTarget(classDir). # Add a JavaProjectClasspathDecorator that uses the # JavaProjectDependencies object created above to give the compile # task its classpath addClasspathDecorator( Schmant::JavaProjectClasspathDecorator.new. setProject(proj). setDependencies(projDeps)).create # Register the compile task as the dependency that this project hinges on. # (If we would post process the compiled class files in any way, for # instance Emma instrument them, the post process task would (probably) be # the one that was registered here.) projDeps.registerDependency(proj, javacTask) # Also register the class directory so that it can be found by projects # that depend on this project. projDeps.registerClassDirectory(proj, classDir) # Add the compile task to the task executor. Now the compile task hinges # on both it's dependent projects and the preprocess task te.add( javacTask, Schmant::CompoundTaskDependency.new. addAll(projDeps.getDependencies(proj)). add(ppt)) end # Create a Jar file with all the compiled classes te.add( Schmant::JarTF.new. # targetFile is a File or a FutureFile setTarget($targetFile). addSources(classDirs), # This is also a dependency object for all compilation tasks. projDeps) # Wait for all tasks to complete te.waitFor ensure te.shutdown end

Jython

# Create the Eclipse workspace object for the contents in the directory wos. # wos may be a File directory or an EntityFS Directory. # vars is a Map of the values of classpath variables used in # the workspace. These variables override the values parsed from the workspace # metadata. settings = EclipseWorkspaceSettings(). \ addClasspathVariables(vars) eWos = EclipseWorkspace(wos, settings) # Get all Java projects from the workspace javaProjects = eWos.getProjects( JavaProjectFilter.INSTANCE) # An EntityFilter that hides Subversion .svn directories noSvnFilter = DirectoryFilter.FILTER.and( EntityNameFilter(".svn")).not() # Create a JavaProjectDependencies object for keeping track of dependencies # between different projects. projDeps = JavaProjectDependencies() # This collection will contain all the directories with class files classDirs = [] # Create a TaskExecutor and start it te = TaskExecutor(). \ setNumberOfThreads(props.getIntValue("noOfBuildThreads", 2)). \ start() try: # The tmpDir variable points to a directory where temporary files are # kept during the build. It can for instance be created by calling # tmpDir = TempFileUtil.createTempDirectory() # Iterate over all Java projects and create compile tasks for them itr = javaProjects.iterator() while(itr.hasNext()): proj = itr.next() projName = proj.getName() # Create a temporary directory to put the preprocessed source # files in. sourceDir = Directories.newDirectory(tmpDir, projName + "_src") # Create the preprocess task for all source files # # Preprocess all files in the directory hierarchy. Ignore # Subversion's .svn directories. # # The replace 232 -> 233 is for the documentation's unit tests. # Pretend that it's not there... ppt = RecursiveProcessTF(). \ addSources( DirectoryAndFilter.listWithFilter( Directories.newViews( proj.getSourceDirectories(), \ noSvnFilter), \ EFileFilter.FILTER)). \ setTarget(sourceDir). \ setTaskFactory( TextReplaceTF(). \ addReplace("!!!VERSION!!!", "1.0"). \ addReplace("232", "233")). \ create() te.add(ppt) # Create a temporary directory for compiled files classDir = Directories.newDirectory(tmpDir, projName) classDirs.append(classDir) # Create the compilation task # # Add a JavaProjectClasspathDecorator that uses # the JavaProjectDependencies object created above to give the # compile task its classpath javacTask = Jdk6JavacTF(). \ addSource(sourceDir). \ setTarget(classDir). \ addClasspathDecorator( JavaProjectClasspathDecorator(). \ setProject(proj). \ setDependencies(projDeps)).create() # Register the compile task as the dependency that this project # hinges on. (If we would post process the compiled class files # in any way, for instance Emma instrument them, the post # process task would (probably) be the one that was registered # here.) projDeps.registerDependency(proj, javacTask) # Also register the class directory so that it can be found by # projects that depend on this project. projDeps.registerClassDirectory(proj, classDir) # Add the compile task to the task executor. Now the compile # task hinges on both it's dependent projects and the preprocess # task. te.add( javacTask, \ CompoundTaskDependency(). \ addAll(projDeps.getDependencies(proj)). \ add(ppt)) # End while # Create a Jar file with all the compiled classes # # targetFile is a File or a FutureFile # # projDeps is also a dependency object for all compilation tasks. te.add( JarTF(). \ setTarget(targetFile). \ addSources(classDirs), \ projDeps) # Wait for all tasks to complete te.waitFor() finally: te.shutdown()

The EclipseWorkspace uses a plugin mechanism for parsing project directories. Task packages may register new plugins that teaches it to create new kinds of Project objects. See the reference documentation for details.

IntelliJ IDEA project support is provided by the IntelliJWorkspace class.

Note

There is a bit of name confusion at work here. The equivalent of the Eclipse workspace is called a project in IntelliJ and the equivalent of the Eclipse project is called a module. But, to try to keep some semblance of consistency, in Schmant and from now on in this manual, an IntelliJ project is called a workspace and an IntelliJ module is called a project. Sorry for the confusion.

The IntelliJWorkspace supports several types of project dependencies. A project can have dependencies to module libraries, project libraries, global libraries and application-supplied (IntelliJ-supplied) libraries such as the Java EE libraries, as well as to single Jar files and class file directories. If used by the workspace, information about global and application-supplied libraries has to be provided in an IntelliJWorkspaceSettings object when creating the workspace object. All other necessary information about where to locate dependencies is parsed from the workspace's .ipr and .iml files by the IntelliJWorkspace object.

An IntelliJ JavaProject also implements the IntelliJJavaProject interface which adds a getTestSourceDirectories method for getting a project's test sources.

The example below shows how to build an IntelliJ workspace with global and application-supplied library dependencies.

Example 8.8. Build an IntelliJ workspace

Groovy

import java.io.File import org.entityfs.el.AbsoluteLocation import org.entityfs.util.* import org.schmant.project.intellij.* import org.schmant.project.java.Library import org.schmant.task.project.JavaWorkspaceBuilderTF // Create an IntelliJWorkspaceSettings object that is used to provide // information that cannot be parsed from its .ipr and .iml files to the // IntelliJ workspace object. def settings = new IntelliJWorkspaceSettings(). // Supply the IntelliJ IDEA installation directory. You don't need to do this // if your workspace does not use any application-supplied libraries. setApplicationHomeDirectory( FileSystems.getEntityForDirectory( new File("/home/kalle/java/idea-7590"), true)); // The GlobalLib library uses all Jar files in the lib and lib2 directories and // the class files in the directory tree under the classes directory. def globalLib = new Library(). addEntries(Directories.getAllFilesMatching(lib, "*.jar")). addEntries(Directories.getAllFilesMatching(lib2, "*.jar")). addEntry(classes) // Add the global library to the settings. settings.addGlobalLibrary("GlobLib1", globalLib) // Create an IntelliJWorkspace object for the workspace in the directory wos. // Supply both the global library and the path to the IntelliJ installation. // // If you don't want to use a proper IntelliJ installation for the application- // supplied libraries, use any old directory that contains all required library // files in the places where the workspace expects them. If a required file // cannot be found, the workspace object throws an exception with a message that // can be used to fix the problem. // // The application installation path or the global library map can be set to // null if they are not used by the workspace. def ijWos = new IntelliJWorkspace( wos, // We have to tell the constructor where to find the workspace's .ipr file new AbsoluteLocation("/MyProject.ipr"), settings) // Build the workspace. // The default is to build all Java files in the source and the test source // directories. The dontBuildTestSources and dontBuildRegularSources properties // may be set to avoid building either kind of sources. new JavaWorkspaceBuilderTF(). setWorkspace(ijWos). // bin is a Java File or an EntityFS Directory. Class files built // from both the regular and the test sources are put here. setTarget(bin). run()

JavaScript

// Create an IntelliJWorkspaceSettings object that is used to provide // information that cannot be parsed from its .ipr and .iml files to the // IntelliJ workspace object. settings = new IntelliJWorkspaceSettings(). // Supply the IntelliJ IDEA installation directory. You don't need to do this // if your workspace does not use any application-supplied libraries. setApplicationHomeDirectory( FileSystems.getEntityForDirectory( new File("/home/kalle/java/idea-7590"), true)); // The GlobalLib library uses all Jar files in the lib and lib2 directories and // the class files in the directory tree under the classes directory. globalLib = new Library(). addEntries(Directories.getAllFilesMatching(lib, "*.jar")). addEntries(Directories.getAllFilesMatching(lib2, "*.jar")). addEntry(classes); // Add the global library to the settings. settings.addGlobalLibrary("GlobLib1", globalLib); // Create an IntelliJWorkspace object for the workspace in the directory wos. // Supply both the global library and the path to the IntelliJ installation. // // If you don't want to use a proper IntelliJ installation for the application- // supplied libraries, use any old directory that contains all required library // files in the places where the workspace expects them. If a required file // cannot be found, the workspace object throws an exception with a message that // can be used to fix the problem. // // The application installation path or the global library map can be set to // null if they are not used by the workspace. ijWos = new IntelliJWorkspace( wos, // We have to tell the constructor where to find the workspace's .ipr file new AbsoluteLocation("/MyProject.ipr"), settings); // Build the workspace. // The default is to build all Java files in the source and the test source // directories. The dontBuildTestSources and dontBuildRegularSources properties // may be set to avoid building either kind of sources. new JavaWorkspaceBuilderTF(). setWorkspace(ijWos). // bin is a Java File or an EntityFS Directory. Class files built // from both the regular and the test sources are put here. setTarget(bin). run();

JRuby

# Create an IntelliJWorkspaceSettings object that is used to provide # information that cannot be parsed from its .ipr and .iml files to the # IntelliJ workspace object. settings = Schmant::IntelliJWorkspaceSettings.new. # Supply the IntelliJ IDEA installation directory. You don't need to do this # if your workspace does not use any application-supplied libraries. setApplicationHomeDirectory( Schmant::FileSystems.getEntityForDirectory( Java::JavaIo::File.new("/home/kalle/java/idea-7590"), true)) # The GlobalLib library uses all Jar files in the lib and lib2 directories and # the class files in the directory tree under the classes directory. globalLib = Schmant::Library.new. addEntries(Schmant::Directories.getAllFilesMatching($lib, "*.jar")). addEntries(Schmant::Directories.getAllFilesMatching($lib2, "*.jar")). addEntry($classes) # Add the global library to the settings. settings.addGlobalLibrary("GlobLib1", globalLib) # Create an IntelliJWorkspace object for the workspace in the directory wos. # Supply both the global library and the path to the IntelliJ installation. # # If you don't want to use a proper IntelliJ installation for the application- # supplied libraries, use any old directory that contains all required library # files in the places where the workspace expects them. If a required file # cannot be found, the workspace object throws an exception with a message that # can be used to fix the problem. # # The application installation path or the global library map can be set to # null if they are not used by the workspace. ijWos = Schmant::IntelliJWorkspace.new( $wos, # We have to tell the constructor where to find the workspace's .ipr file Schmant::AbsoluteLocation.new("/MyProject.ipr"), settings) # Build the workspace. # The default is to build all Java files in the source and the test source # directories. The dontBuildTestSources and dontBuildRegularSources properties # may be set to avoid building either kind of sources. Schmant::JavaWorkspaceBuilderTF.new. setWorkspace(ijWos). # bin is a Java File or an EntityFS Directory. Class files built # from both the regular and the test sources are put here. setTarget($bin). run

Jython

# Create an IntelliJWorkspaceSettings object that is used to provide # information that cannot be parsed from its .ipr and .iml files to the # IntelliJ workspace object. # # The IntelliJ IDEA installation directory has to be supplied if the workspace # does not use any application-supplied libraries. settings = IntelliJWorkspaceSettings(). \ setApplicationHomeDirectory( \ FileSystems.getEntityForDirectory( \ File("/home/kalle/java/idea-7590"), \ True)) # The GlobalLib library uses all Jar files in the lib and lib2 directories and # the class files in the directory tree under the classes directory. globalLib = Library(). \ addEntries(Directories.getAllFilesMatching(lib, "*.jar")). \ addEntries(Directories.getAllFilesMatching(lib2, "*.jar")). \ addEntry(classes) # Add the global library to the settings. settings.addGlobalLibrary("GlobLib1", globalLib) # Create an IntelliJWorkspace object for the workspace in the directory wos. # Supply both the global library and the path to the IntelliJ installation. # # If you don't want to use a proper IntelliJ installation for the application- # supplied libraries, use any old directory that contains all required library # files in the places where the workspace expects them. If a required file # cannot be found, the workspace object throws an exception with a message that # can be used to fix the problem. # # The application installation path or the global library map can be set to # null if they are not used by the workspace. ijWos = IntelliJWorkspace( \ wos, \ AbsoluteLocation("/MyProject.ipr"), \ settings) # Build the workspace. # The default is to build all Java files in the source and the test source # directories. The dontBuildTestSources and dontBuildRegularSources properties # may be set to avoid building either kind of sources. # # bin is a Java File or an EntityFS Directory. Class files built from # both the regular and the test sources are put here. JavaWorkspaceBuilderTF(). \ setWorkspace(ijWos). \ setTarget(bin). \ run()

The IntelliJWorkspace uses a plugin mechanism for parsing project directories. Task packages may register new plugins that teaches it to create new kinds of Project objects. See the reference documentation for details.