--- layout: default title: Languages --- h2(#java). Java h3. 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. | h3. ECJ You can use the "ECJ compiler":http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-using_batch_compiler.htm instead of javac. ECJ abides to the same options as javac. For example, to configure the project to use ECJ: {% highlight ruby %} compile.using :ecj {% endhighlight %} To use a custom version of ECJ, add an entry to your "settings":/settings_profiles.html. For example, to set the version of ECJ to 3.5.1, add an entry to your project's @buildr.yml@: {% highlight yaml %} ecj: 3.5.1 {% endhighlight %} h3. Testing with Java h4. JUnit The default test framework for Java projects is "JUnit 4":http://www.junit.org. When you use JUnit, the dependencies includes JUnit and "JMock":http://www.jmock.org, 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: {% highlight ruby %} test.using :properties=>{ :currency=>'USD' } {% endhighlight %} 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: {% highlight sh %} $ buildr test junit:report test=all $ firefox report/junit/html/index.html {% endhighlight %} 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: {% highlight yaml %} junit: 4.4 jmock: 2.0 {% endhighlight %} h4. TestNG You can use "TestNG":http://testng.org instead of JUnit. To select TestNG as the test framework, add this to your project: {% highlight ruby %} test.using :testng {% endhighlight %} 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: {% highlight yaml %} testng: 5.7 {% endhighlight %} h4. JBehave "JBehave":http://jbehave.org/ 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: {% highlight text %} src/spec/java/**/*Behaviour.java {% endhighlight %} 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: {% highlight yaml %} jbehave: 1.0.1 {% endhighlight %} h3. Documentation Buildr offers support for using JavaDoc to generate documentation from any Java sources in a project. This is done using the @doc@ task: {% highlight sh %} $ buildr doc {% endhighlight %} 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): {% highlight ruby %} define 'foo' do # ... doc.from projects('foo:bar', 'foo') define 'bar' do # ... end end {% endhighlight %} 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: {% highlight ruby %} define 'foo' do # ... doc :windowtitle => 'Abandon All Hope, Ye Who Enter Here', :private => true end {% endhighlight %} h2(#scala). Scala Before using Scala, you must first @require@ the Scala compiler: {% highlight ruby %} require 'buildr/scala' {% endhighlight %} 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@: {% highlight yaml %} scala.version: 2.8.0.Beta1 # Pick your version {% endhighlight %} Or, you can do the same programmatically: {% highlight yaml %} # Must be placed before require 'buildr/scala' Buildr.settings.build['scala.version'] = "2.8.0.Beta1" {% endhighlight %} You may also determine the version in use by querying the @Scala.version@ attribute: {% highlight ruby %} Scala.version # => '2.8.0' {% endhighlight %} 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":http://oss.sonatype.org/content/repositories/releases 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. p(tip). For Mac users, if you have installed Scala via "MacPorts":http://www.macports.org/ Buildr will look in the @/opt/local/share/scala/@ directory if you have not set @SCALA_HOME@. h3. 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. | h4. 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.) h4. 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: {% highlight ruby %} compile.using :make => :all {% endhighlight %} 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":https://github.com/typesafehub/zinc incremental compilation wrapper for @scalac@. Incremental compilation can be enabled 3 ways, 1) By setting the compiler's option directly, {% highlight ruby %} compile.using :incremental => true compile.options.incremental = true # same as above {% endhighlight %} 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@, {% highlight ruby %} project.scalac_options.incremental = true {% endhighlight %} 3) By setting the global @scalac.incremental@ option, in your @buildfile@: {% highlight ruby %} Buildr.settings.build['scalac.incremental'] = true {% endhighlight %} or in your @build.yaml@: {% highlight yaml %} scalac.incremental: true {% endhighlight %} h4. 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'@): {% highlight ruby %} ENV['SCALA_HOME'] = ENV['SCALA28_HOME'] require 'buildr/scala' ... {% endhighlight %} 2) By setting the @scala.version@ build property in your build.yaml file: {% highlight yaml %} scala.version: 2.9.1.RC1 {% endhighlight %} 3) By setting the @scala.version@ build property in your buildfile: {% highlight ruby %} require 'buildr/scala' ... Buildr.settings.build['scala.version'] = '2.10-M6' {% endhighlight %} h3. Testing with Scala Buildr supports two main Scala testing frameworks: "ScalaTest":http://www.artima.com/scalatest and "Specs":http://code.google.com/p/specs/. "ScalaCheck":http://code.google.com/p/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. {% highlight ruby %} test.using(:scalatest) {% endhighlight %} h4. 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, {% highlight scala %} class MySuite extends org.scalatest.FunSuite { test("addition") { val sum = 1 + 1 assert(sum === 2) } } {% endhighlight %} 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: {% highlight scala %} 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) } } {% endhighlight %} h4. 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: {% highlight scala %} import org.specs._ import org.specs.runner._ object StringSpecs extends Specification { "empty string" should { "have a zero length" in { "".length mustBe 0 } } } {% endhighlight %} 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@: {% highlight ruby %} MOCKITO = 'org.mockito:mockito-all:jar:1.7' CGLIB = 'cglib:cglib:jar:2.1_3' ASM = 'asm:asm:jar:1.5.3' OBJENESIS = 'org.objenesis:objenesis:jar:1.1' define 'killer-app' do ... test.with MOCKITO, CGLIB, ASM, OBJENESIS end {% endhighlight %} The dependencies for Specs's optional features are defined "here":http://code.google.com/p/specs/wiki/RunningSpecs#Dependencies. h4. ScalaCheck You may use ScalaCheck inside ScalaTest- and Specs-inherited classes. Here is an example illustrating checks inside a ScalaTest suite, {% highlight scala %} 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 ) } {% endhighlight %} h3. 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: {% highlight sh %} $ buildr doc {% endhighlight %} 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): {% highlight ruby %} define 'foo' do # ... doc.from projects('foo:bar', 'foo') define 'bar' do # ... end end {% endhighlight %} 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: {% highlight ruby %} define 'foo' do # ... doc :windowtitle => 'Abandon All Hope, Ye Who Enter Here', :private => true end {% endhighlight %} 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: {% highlight ruby %} define 'foo' do doc.using :vscaladoc end {% endhighlight %} 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. h2(#groovy). Groovy h3. Compiling Groovy Before using the Groovy compiler, you must first require it on your buildfile: {% highlight ruby %} require 'buildr/java/groovyc' {% endhighlight %} 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. | h3. Testing with Groovy h4. EasyB "EasyB":http://www.easyb.org/ is a BDD framework using "Groovy":http://groovy.codehaus.org/. 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: {% highlight text %} src/spec/groovy/**/*Behavior.groovy src/spec/groovy/**/*Story.groovy {% endhighlight %} 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@ | h3. Documentation Buildr offers support for using GroovyDoc to generate documentation from any Groovy sources in a project. This is done using the @doc@ task: {% highlight sh %} $ buildr doc {% endhighlight %} 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): {% highlight ruby %} define 'foo' do # ... doc.from projects('foo:bar', 'foo') define 'bar' do # ... end end {% endhighlight %} 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: {% highlight ruby %} define 'foo' do # ... doc :windowtitle => 'Abandon All Hope, Ye Who Enter Here', :private => true end {% endhighlight %} 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. h2(#ruby). Ruby h3. 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":http://www.ruby-lang.org/en/ language, and is run by using "JRuby":http://jruby.codehaus.org/. 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. p(tip). 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: {% highlight yaml %} jruby: 1.3.1 {% endhighlight %} h4. RSpec "RSpec":http://rspec.info/ 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: {% highlight text %} src/spec/ruby/**/*_spec.rb {% endhighlight %} 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) |