1. Start Here
    1. Welcome
    2. Download
    3. User Guide (PDF)
    4. What’s New
    5. Community Wiki
  2. Using Buildr
    1. Getting Started
    2. Projects
    3. Building
    4. Artifacts
    5. Packaging
    6. Testing
    7. Settings/Profiles
    8. Languages
    9. More Stuff
    10. Extending Buildr
    11. Recipes
  3. Reference
    1. API
    2. Rake
    3. Antwrap
    4. Troubleshooting
  4. Get Involved
    1. Mailing Lists
    2. Issues/Bugs
    3. Contributing
  5. Project Status
    1. License
    2. Changelog
    3. Specs
    4. Coverage
Google Custom Search

Languages

  1. Java
    1. Compiling Java
    2. Testing with Java
  2. Scala
    1. Compiling Scala
    2. Testing with Scala
  3. Groovy
    1. Compiling Groovy
    2. Testing with Groovy
  4. Ruby
    1. Testing with Ruby

Java

Compiling Java

The Java compiler looks for source files in the project’s src/main/java directory, and defaults to compiling them into the target/classes directory. It looks for test cases in the project’s src/test/java and defaults to compile them into the target/test/classes directory.

If you point the compile task at any other source directory, it will use the Java compiler if any of these directories contains files with the extension .java.

When using the Java compiler, if you don’t specify the packaging type, it defaults to JAR. If you don’t specify the test framework, it defaults to JUnit.

The Java compiler supports the following options:

Option Usage
:debug Generates bytecode with debugging information. You can also override this by setting the environment variable debug to off.
:deprecation If true, shows deprecation messages. False by default.
:lint Defaults to false. Set this option to true to use all lint options, or specify a specific lint option (e.g. :lint=>'cast').
:other Array of options passed to the compiler (e.g. :other=>'-implicit:none').
:source Source code compatibility (e.g. ‘1.5’).
:target Bytecode compatibility (e.g. ‘1.4’).
:warnings Issue warnings when compiling. True when running in verbose mode.

Testing with Java

JUnit

The default test framework for Java projects is JUnit 4.

When you use JUnit, the dependencies includes JUnit and JMock, and Buildr picks up all test classes from the project by looking for classes that either subclass junit.framework.TestCase, include methods annotated with org.junit.Test, or test suites annotated with org.org.junit.runner.RunWith.

The JUnit test framework supports the following options:

Option Value
:fork VM forking, defaults to true.
:clonevm If true clone the VM each time it is forked.
:properties Hash of system properties available to the test case.
:environment Hash of environment variables available to the test case.
:java_args Arguments passed as is to the JVM.

For example, to pass properties to the test case:

test.using :properties=>{ :currency=>'USD' }

There are benefits to running test cases in separate VMs. The default forking mode is :once, and you can change it by setting the :fork option.

:fork=> Behavior
:once Create one VM to run all test classes in the project, separate VMs for each project.
:each Create one VM for each test case class. Slow but provides the best isolation between test classes.
false Without forking, Buildr runs all test cases in a single VM. This option runs fastest, but at the risk of running out of memory and causing test cases to interfere with each other.

You can see your tests running in the console, and if any tests fail, Buildr will show a list of the failed test classes. In addition, JUnit produces text and XML report files in the project’s reports/junit directory. You can use that to get around too-much-stuff-in-my-console, or when using an automated test system.

In addition, you can get a consolidated XML or HTML report by running the junit:report task. For example:

$ buildr test junit:report test=all
$ firefox report/junit/html/index.html

The junit:report task generates a report from all tests run so far. If you run tests in a couple of projects, it will generate a report only for these two projects. The example above runs tests in all the projects before generating the reports.

You can use the build.yaml settings file to specify a particular version of JUnit or JMock. For example, to force your build to use JUnit version 4.4 and JMock 2.0:

junit: 4.4
jmock: 2.0

TestNG

You can use TestNG instead of JUnit. To select TestNG as the test framework, add this to your project:

test.using :testng

Like all other options you can set with test.using, it affects the projects and all its sub-projects, so you only need to do this once at the top-most project to use TestNG throughout. You can also mix TestNG and JUnit by setting different projects to use different frameworks, but you can’t mix both frameworks in the same project. (And yes, test.using :junit will switch a project back to using JUnit)

TestNG works much like JUnit, it gets included in the dependency list along with JMock, Buildr picks test classes that contain methods annotated with org.testng.annotations.Test, and generates test reports in the reports/testng directory. At the moment we don’t have consolidated HTML reports for TestNG.

The TestNG test framework supports the following options:

Option Value
:properties Hash of system properties available to the test case.
:java_args Arguments passed as is to the JVM.

You can use the build.yaml settings file to specify a particular version of TestNG, for example, to force your build to use TestNG 5.7:

testng: 5.7

JBehave

JBehave is a pure Java BDD framework, stories and behaviour specifications are written in the Java language.

To use JBehave in your project you can select it with test.using :jbehave.

This framework will search for the following patterns under your project:

src/spec/java/**/*Behaviour.java

Supports the following options:

Option Value
:properties Hash of system properties available to the test case.
:java_args Arguments passed as is to the JVM.

You can use the build.yaml settings file to specify a particular version of JBehave, for example, to force your build to use JBehave 1.0.1:

jbehave: 1.0.1

Scala

Before using Scala features, you must first set the SCALA_HOME environment variable to point to the root of your Scala distribution.

On Windows:

> set SCALA_HOME=C:\Path\To\Scala-2.7.1

On Linux and other Unix variants,

> export SCALA_HOME=/path/to/scala-2.7.1

The SCALA_HOME base directory should be such that Scala core libraries are located directly under the “lib” subdirectory, and Scala scripts are under the “bin” directory.

Compiling Scala

The Scala compiler looks for source files in the project’s src/main/scala directory, and defaults to compiling them into the target/classes directory. It looks for test cases in the project’s src/test/scala and defaults to compile them into the target/test/classes directory.

If you point the compile task at any other source directory, it will use the Scala compiler if any of these directories contains files with the extension .scala.

When using the Scala compiler, if you don’t specify the packaging type, it defaults to JAR.

The Scala compiler supports the following options:

Option Usage
:debug Generates bytecode with debugging information. You can also override this by setting the environment variable debug to off.
:deprecation If true, shows deprecation messages. False by default.
:optimise Generates faster bytecode by applying optimisations to the program.
:other Array of options passed to the compiler (e.g. :other=>'-Xprint-types').
:target Bytecode compatibility (e.g. ‘1.4’).
:warnings Issue warnings when compiling. True when running in verbose mode.

Fast Scala Compiler

You may use fsc, the Fast Scala Compiler, which submits compilation jobs to a compilation daemon, by setting the environment variable USE_FSC to yes. Note that fsc may cache class libraries — don’t forget to run fsc -reset if you upgrade a library.

Rebuild detection

The Scala compiler task assumes that each .scala source file generates a corresponding .class file under target/classes (or target/test/classses for tests). The source may generate more .class files if it contains more than one class, object, trait or for anonymous functions and closures.

For example, src/main/scala/com/example/MyClass.scala should generate at least target/classes/com/example/MyClass.class. If that it not the case, Buildr will always recompile your sources because it will assume this is a new source file that has never been compiled before.

Testing with Scala

Buildr supports three Scala testing frameworks: ScalaTest, ScalaCheck and Specs.

Scala testing is automatically enabled if you have any .scala source files under src/test/scala. If you are not using this convention, you can explicit set the test framework by doing,

test.using(:scalatest)

The :scalatest test framework handles ScalaTest, Specs and ScalaCheck therefore all 3 frameworks may be used within the same project.

ScalaTest

Buildr automatically detects and runs tests that extend the org.scalatest.Suite interface.

A very simplistic test class might look like,

class MySuite extends org.scalatest.FunSuite {
  test("addition") {
    val sum = 1 + 1
    assert(sum === 2)
  }
}

You can also pass properties to your tests by doing test.using :properties => { 'name'=>'value' }, and by overriding the Suite.runTests method in a manner similar to:

import org.scalatest._

class PropertyTestSuite extends FunSuite {
  var properties = Map[String, Any]()
  
  test("testProperty") {
    assert(properties("name") === "value")
  }

  protected override def runTests(testName: Option[String], 
    reporter: Reporter, stopper: Stopper, includes: Set[String], 
    excludes: Set[String], properties: Map[String, Any])
  {
    this.properties = properties;                              
    super.runTests(testName, reporter, stopper, 
                   includes, excludes, properties)
  }
}

Specs

The :scalatest framework currently recognizes specifications with class names ending with “Specs”, e.g., org.example.StringSpecs.

A simple specification might look like this:

import org.specs._
import org.specs.runner._

object StringSpecs extends Specification {
  "empty string" should {
    "have a zero length" in {
      ("".length) mustEqual(0)
    }
  }
}

ScalaCheck

You may use ScalaCheck inside ScalaTest- and Specs-inherited classes. Here is an example illustrating checks inside a ScalaTest suite,

import org.scalatest.prop.PropSuite
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop._

class MySuite extends PropSuite {

  test("list concatenation") {
    val x = List(1, 2, 3)
    val y = List(4, 5, 6)
    assert(x ::: y === List(1, 2, 3, 4, 5, 6))
    check((a: List[Int], b: List[Int]) => a.size + b.size == (a ::: b).size)
  }

  test(
    "list concatenation using a test method",
    (a: List[Int], b: List[Int]) => a.size + b.size == (a ::: b).size
  )
}

Groovy

Compiling Groovy

Before using the Groovy compiler, you must first require it on your buildfile:

require 'buildr/java/groovyc'

Once loaded, the groovyc compiler will be automatically selected if any .groovy source files are found under src/main/groovy directory, compiling them by default into the target/classes directory.

If the project has java sources in src/main/java they will get compiled using the groovyc joint compiler.

Sources found in src/test/groovy are compiled into the target/test/classes.

If you don’t specify the packaging type, it defaults to JAR.

The Groovy compiler supports the following options:

Option Usage
encoding Encoding of source files.
verbose Asks the compiler for verbose output, true when running in verbose mode.
fork Whether to execute groovyc using a spawned instance of the JVM. Defaults to no.
memoryInitialSize The initial size of the memory for the underlying VM, if using fork mode, ignored otherwise. Defaults to the standard VM memory setting. (Examples: 83886080, 81920k, or 80m)
memoryMaximumSize The maximum size of the memory for the underlying VM, if using fork mode, ignored otherwise. Defaults to the standard VM memory setting. (Examples: 83886080, 81920k, or 80m)
listfiles Indicates whether the source files to be compiled will be listed. Defaults to no.
stacktrace If true each compile error message will contain a stacktrace.
warnings Issue warnings when compiling. True when running in verbose mode.
debug Generates bytecode with debugging information. Set from the debug environment variable/global option.
deprecation If true, shows deprecation messages. False by default.
optimise Generates faster bytecode by applying optimisations to the program.
source Source code compatibility.
target Bytecode compatibility.
javac Hash of options passed to the ant javac task.

Testing with Groovy

EasyB

EasyB is a BDD framework using Groovy.

Specifications are written in the Groovy language, of course you get seamless Java integration as with all things groovy.

To use this framework in your project you can select it with test.using :easyb.

This framework will search for the following patterns under your project:

src/spec/groovy/**/*Behavior.groovy
src/spec/groovy/**/*Story.groovy

Supports the following options:

Option Value
:properties Hash of system properties available to the test case.
:java_args Arguments passed as is to the JVM.
:format Report format, either :txt or :xml

Ruby

Testing with Ruby

Buildr provides integration with some ruby testing frameworks, allowing you to test your Java code with state of the art tools.

Testing code is written in Ruby language, and is run by using JRuby means you have access to all your Java classes and any Java or Ruby tool out there.

Because of the use of JRuby, you will notice that running ruby tests is faster when running Buildr on JRuby, as in this case there’s no need to run another JVM.

When not running on JRuby, Buildr will use the JRUBY_HOME environment variable to find the JRuby installation directory. If no JRUBY_HOME is set or it points to an empty directory, Buildr will prompt you to either install JRuby manually or let it extract it for you.

You can use the build.yaml settings file to specify a particular version of JRuby (defaults to 1.1.4). For example:

jruby: 1.1.3

RSpec

RSpec is the de-facto BDD framework for ruby. It’s the framework used to test Buildr itself.

To use this framework in your project you can select it with test.using :rspec.

This framework will search for the following patterns under your project:

src/spec/ruby/**/*_spec.rb

Supports the following options:

Option Value
:gems Hash of gems needed before running the tests. Keys are gem names, values are the required gem version. An example use of this option would be to require the ci_reporter gem to generate xml reports
:requires Array of ruby files to require before running the specs
:format Array of valid RSpec --format option values. Defaults to html report on the reports directory and text progress
:output File path to output dump. false to supress output
:fork Run the tests on a new java vm. (enabled unless running on JRuby)
:properties Hash of system properties available to the test case.
:java_args Arguments passed as is to the JVM. (only when fork is enabled)

JtestR

JtestR is a tool that makes it easier to test Java code with state of the art Ruby tools. Using JtestR you can describe your application behaviour using many testing frameworks at the same time.

To use this framework in your project you can select it with test.using :jtestr.

You can use the build.yaml settings file to specify a particular version of JtestR (defaults to 0.3.1). For example:

jtestr: 0.3.1

To customize TestNG/JUnit versions refer to their respective section.

When selected, Buildr will configure JtestR to use your project/testing classpath and will search for the following test patterns for each framework supported by JtestR:

Framework Patterns
RSpec Files in src/spec/ruby ending with *_spec.rb or *_story.rb
TestUnit Files in src/spec/ruby ending with *_test.rb, *Test.rb
Expectations Files in src/spec/ruby ending with *_expect.rb
JUnit Classes from src/test/java that either subclass junit.framework.TestCase, include methods annotated with org.junit.Test, or test suites annotated with org.org.junit.runner.RunWith.
TestNG Classes from src/test/java annotated with org.testng.annotations.Test

If you create a src/spec/ruby/jtestr_config.rb file, it will be loaded by JtestR, just after being configured by Buildr, this way you can configure as described on JtestR guide.

If you have a jtestr_config.rb file, don’t set JtestR::result_handler. Buildr uses its (RSpecResultHandler) so that it can know which tests succeeded/failed, this handler is capable of using RSpec formatter classes, so that you can obtain an html report or use a custom rspec formatter with JtestR. See the format option.

Supports the following options:

Option Value
:config The JtestR config file to be loaded after being configured by Buildr. Defaults to src/spec/ruby/jtestr_config.rb.
:gems Hash of gems needed before running the tests. Keys are gem names, values are the required gem version. An example use of this option would be to require the ci_reporter gem to generate xml reports
:requires Array of ruby files to require before running the specs
:format Array of valid RSpec --format option values. Defaults to html report on the reports directory and text progress
:output File path to output dump. false to supress output
:fork Run the tests on a new java vm. (enabled unless running on JRuby)
:properties Hash of system properties available to the test case. (only when fork is enabled)
:java_args Arguments passed as is to the JVM. (only when fork is enabled)