class Buildr::CompileTask

Compile task.

Attempts to determine which compiler to use based on the project layout, for example, uses the Javac compiler if it finds any .java files in src/main/java. You can also select the compiler explicitly:


Accepts multiple source directories that are invoked as prerequisites before compilation. You can pass a task as a source directory:


Likewise, dependencies are invoked before compiling. All dependencies are evaluated as artifacts, so you can pass artifact specifications and even projects:

compile.with('module1.jar', 'log4j:log4j:jar:1.0', project('foo'))

Creates a file task for the target directory, so executing that task as a dependency will execute the compile task first.

Compiler options are inherited form a parent task, e.g. the foo:bar:compile task inherits its options from the foo:compile task. Even if foo is an empty project that does not compile any classes itself, you can use it to set compile options for all its sub-projects.

Normally, the project will take care of setting the source and target directory, and you only need to set options and dependencies. See Buildr::Compile#compile.



Compilation dependencies.


Returns the compiler options.


The project this task belongs to.


Source directories.


The target directory for the compiled code.


The usage, one of :main or :test.

Public Instance Methods

compiler() click to toggle source

Returns the compiler if known. The compiler is either automatically selected based on existing source directories (e.g. src/main/java), or by requesting a specific compiler (see using).

# File lib/buildr/core/compile.rb, line 336
def compiler
  guess_compiler unless @compiler
  @compiler && @compiler.class.to_sym
from(*sources) → self click to toggle source

Adds source directories and files to compile, and returns self.

For example:

# File lib/buildr/core/compile.rb, line 274
def from(*sources)
  @sources |= sources.flatten
  guess_compiler if @compiler.nil? && sources.flatten.any? { |source| File.exist?(source.to_s) }
into(path) → self click to toggle source

Sets the target directory and returns self. This will also set the compile task as a prerequisite to a file task on the target directory.

For example:


Both compile.invoke and file(target_dir).invoke will compile the source files.

# File lib/buildr/core/compile.rb, line 310
def into(path)
  @target = file(path.to_s).enhance([self]) unless @target.to_s == path.to_s
language() click to toggle source

Returns the compiled language, if known. See also compiler.

# File lib/buildr/core/compile.rb, line 342
def language
  compiler && @compiler.class.language
packaging() click to toggle source

Returns the default packaging type for this compiler, if known.

# File lib/buildr/core/compile.rb, line 347
def packaging
  compiler && @compiler.class.packaging
using(options) → self click to toggle source

Sets the compiler options from a hash and returns self. Can also be used to select the compiler.

For example:

compile.using(:warnings=>true, :source=>'1.5')
# File lib/buildr/core/compile.rb, line 327
def using(*args)
  args.pop.each { |key, value| options.send "#{key}=", value } if Hash === args.last
  self.compiler = args.pop until args.empty?
with(*artifacts) → self click to toggle source

Adds files and artifacts as dependencies, and returns self.

Calls artifacts on the arguments, so you can pass artifact specifications, tasks, projects, etc. Use this rather than setting the dependencies array directly.

For example:

compile.with('module1.jar', 'log4j:log4j:jar:1.0', project('foo'))
# File lib/buildr/core/compile.rb, line 293
def with(*specs)
  @dependencies |= Buildr.artifacts(specs.flatten).uniq