Languages
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. |
ECJ
You can use the ECJ compiler instead of javac. ECJ abides to the same options as javac.
For example, to configure the project to use ECJ:
To use a custom version of ECJ, add an entry to your settings.
For example, to set the version of ECJ to 3.5.1, add an entry to your project’s buildr.yml
:
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:
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:
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:
TestNG
You can use TestNG instead of JUnit. To select TestNG as the test framework, add this to your project:
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:
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:
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:
Documentation
Buildr offers support for using JavaDoc to generate documentation from any Java sources in a project. This is done using the doc
task:
This will use the same .java
sources used by the compile
task to produce JavaDoc results in the target/doc/
directory. By default, these sources are chosen only from the current project. However, it is possible to override this and generate documentation from the sources in a sub-project (potentially more than one):
With this configuration, the doc
task will use sources from both foo:bar
and
foo
.
The doc
task supports any option that the javadoc
command does (e.g. -windowtitle
). To pass an option to the JavaDoc generator, simply specify it using the doc
method:
Scala
Before using Scala, you must first require
the Scala compiler:
By default, Buildr will attempt to use the latest stable release of Scala, which is currently Scala 2.9.0 as of May 2011. Of course you can configure a specific version of Scala for your project by adding the following entry in build.yaml
:
Or, you can do the same programmatically:
You may also determine the version in use by querying the Scala.version
attribute:
Regardless of how the Scala version is determined, if you have the same Scala version installed on your system and the SCALA_HOME environment variable points to it, then your local installation will be used. Otherwise, Buildr will download it from the Sonatype repository which is automatically enlisted when you require
Scala. The only drawback if you don’t have a local installation is the FSC compiler won’t be available.
For Mac users, if you have installed Scala via MacPorts Buildr will look in the
/opt/local/share/scala/
directory if you have not set SCALA_HOME
.
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.
Any Java source files found in the src/main/java
directory will be compiled using the Scala/Java joint compiler into the target/classes
directory. Both the Java and the Scala sources are compiled with an inclusive classpath, meaning that you may have a Java class which depends upon a Scala class which depends upon a Java class, all within the same project. The Java sources will be compiled with the same dependencies as the Scala sources with the addition of the scala-library.jar
file as required for Scala interop.
Note that you cannot use the Groovy and the Scala joint compilers in the same project. If both are required, the Groovy joint compiler will take precedence.
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
. The joint compilation of Java sources may only be pointed at an alternative directory using the feature to redefine the _(:src, :main, :java)
path.
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 |
If true, generates bytecode with debugging information. Scala 2.9 also accepts: none,source,line,vars,notc. |
:deprecation |
If true, shows deprecation messages. False by default. |
:make |
Make strategy to be used by the compiler (e.g. :make=>'transitive' ). Scala 2.8 only |
: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. |
:javac |
A hash of options passed to the javac compiler verbatim. |
:incremental |
If true, enables incremental compilation using Zinc. |
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.
(Note fsc
is not compatible with zinc
incremental compilation.)
Rebuild detection
Scala 2.7
The Scala 2.7 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.
Scala 2.8
Scala 2.8 provides a substantially better interface for implementing change detection. Whenever you use Scala 2.8 (see below), Buildr will auto-detect the version and enable this feature dynamically. After the compile
task runs, the relevant target directory will contain a .scala-deps
file, generated by the Scala compiler. The manner in which this file is used can be configured using the :make
compiler option. The following values are available:
:all
– Disables compiler-level change detection:changed
– Only build changed files without considering file dependencies:immediate
– unknown:transitive
– Build changed files as well as their transitive file dependencies:transitivenocp
– Build changed files as well as their transitive file dependencies (default)
Please note that there are limits to compiler-level change detection. Most notably, dependencies cannot be tracked across separate compilation targets. This would cause problems in the case where an API has been changed in a main source file. The test suite for the project will not be detected as requiring recompilation, potentially resulting in unexpected runtime exceptions. When in doubt, run clean
to remove all dependency information. In extreme cases, it is possible to completely disable compiler-level change detection by adding the following statement to your project definition:
Effectively, this is telling the Scala compiler to ignore the information it has built up regarding source file dependencies. When in this mode, only Buildr’s change detection semantics remain in play (as described above).
To avoid unusual behavior, compiler-level change detection is disabled whenever the joint Scala-Java compiler is used. Thus, any .java
files in a project handled by the Scala compiler will cause the :make
option to be ignored and revert to the exclusive use of Buildr’s change detection mechanism (as described above).
Scala 2.9 and later
Starting with Buildr 1.4.8, Buildr integrates with the Zinc incremental compilation wrapper for scalac
. Incremental compilation can be enabled 3 ways,
1) By setting the compiler’s option directly,
Note that this won’t enable incremental compilation for both compile
and test.compile
, you would have to set options on both. For this reason, it’s recommended that you set the option on the project instead (see below).
2) By setting the project’s scalac_options.incremental
,
3) By setting the global scalac.incremental
option,
in your buildfile
:
or in your build.yaml
:
Support for different Scala versions
Buildr defaults to the latest stable Scala version available at the time of the release if neither SCALA_HOME
nor the scala.version
build property are set.
If your SCALA_HOME
environment variable points to an installation of Scala (2.7, 2.8, 2.9, …), then Buildr will use that compiler and enable version-specific features.
You may select the Scala version by dynamically in different ways,
1) By reassigning SCALA_HOME
at the top of the buildfile (before require 'buildr/scala'
):
2) By setting the scala.version
build property in your build.yaml file:
3) By setting the scala.version
build property in your buildfile:
Testing with Scala
Buildr supports two main Scala testing frameworks: ScalaTest and Specs. ScalaCheck is also supported within the confines of either of these two frameworks. Thus, your Specs may use ScalaCheck properties, as may your ScalaTest suites.
ScalaTest
ScalaTest support is activated automatically when there are any .scala
source files contained in the src/test/scala
directory. If you are not using this directory convention, you may force the test framework by using the test.using :scalatest
directive.
Buildr automatically detects and runs tests that extend the org.scalatest.Suite
interface.
A very simplistic test class might look like,
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:
Specs
Specs is automatically selected whenever there are .scala
source files under the src/spec/scala
directory. It is also possible to force selection of the test framework by using the test.using :specs
directive. This can sometimes be useful when Scala sources may be found in both src/test/scala
and src/spec/scala
. Normally in such cases, ScalaTest will have selection precedence, meaning that in case of a conflict between it and Specs, ScalaTest will be chosen.
Any objects which extend the org.specs.Specification
or org.specs2.Specification
superclass will be automatically detected and run. Note that any classes which extend Specification
will also be invoked. As such classes will not have a main
method, such an invocation will raise an error.
A simple specification might look like this:
ScalaCheck is automatically added to the classpath when Specs is used. However, JMock, Mockito, CGlib and similar are not. This is to avoid downloading extraneous artifacts which are only used by a small percentage of specifications. To use Specs with Mockito (or any other library) in a Buildr project, simply add the appropriate dependencies to test.with
:
The dependencies for Specs’s optional features are defined here.
ScalaCheck
You may use ScalaCheck inside ScalaTest- and Specs-inherited classes. Here is an example illustrating checks inside a ScalaTest suite,
Documentation
Buildr offers support for using ScalaDoc or VScalaDoc to generate documentation from any Scala sources in a project. This is done using the doc
task:
This will use the same .scala
sources used by the compile
task to produce ScalaDoc results in the target/doc/
directory. By default, these sources are chosen only from the current project. However, it is possible to override this and generate documentation from the sources in a sub-project (potentially more than one):
With this configuration, the doc
task will use sources from both foo:bar
and
foo
.
The doc
task supports any option that the scaladoc
command does (e.g. -windowtitle
). To pass an option to the ScalaDoc (or VScalaDoc) generator, simply specify it using the doc
method:
By default, the doc
task will use the ScalaDoc generator on Scala projects. To select the VScalaDoc generator, you must use the doc.using
invocation:
The doc
task is not joint-compilation aware. Thus, it will only generate ScalaDoc for mixed-source projects, it will not attempt to generate both JavaDoc and ScalaDoc.
Groovy
Compiling Groovy
Before using the Groovy compiler, you must first require it on your buildfile:
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:
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 |
Documentation
Buildr offers support for using GroovyDoc to generate documentation from any Groovy sources in a project. This is done using the doc
task:
This will use the same .groovy
sources used by the compile
task to produce GroovyDoc results in the target/doc/
directory. By default, these sources are chosen only from the current project. However, it is possible to override this and generate documentation from the sources in a sub-project (potentially more than one):
With this configuration, the doc
task will use sources from both foo:bar
and
foo
.
The doc
task supports any option that the groovydoc
command does (e.g. -windowtitle
). To pass an option to the GroovyDoc generator, simply specify it using the doc
method:
The doc
task is not joint-compilation aware. Thus, it will only generate GroovyDoc for mixed-source projects, it will not attempt to generate both JavaDoc and GroovyDoc.
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. That 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.4.0
as of Buildr 1.3.5). For example:
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:
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) |