Class: Buildr::CompileTask

Inherits:
Rake::Task show all
Defined in:
lib/buildr/core/compile.rb

Overview

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:

compile.using(:scalac)

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

compile.from(apt)

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 Project#compile.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Rake::Task

#invoke, #invoke_with_call_chain

Constructor Details

#initialize(*args) ⇒ CompileTask

:nodoc:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/buildr/core/compile.rb', line 215

def initialize(*args) #:nodoc:
  super
  parent_task = Project.parent_task(name)
  inherit = lambda { |hash, key| parent_task.options[key] } if parent_task.respond_to?(:options)
  @options = OpenObject.new &inherit
  @sources = FileList[]
  @dependencies = FileList[]

  enhance do |task|
    unless sources.empty?
      raise 'No compiler selected and can\'t determine which compiler to use' unless compiler
      raise 'No target directory specified' unless target
      mkpath target.to_s, :verbose=>false
      info "Compiling #{task.name.gsub(/:[^:]*$/, '')} into #{target.to_s}"
      @compiler.compile(sources.map(&:to_s), target.to_s, dependencies.map(&:to_s))
      # By touching the target we let other tasks know we did something,
      # and also prevent recompiling again for dependencies.
      touch target.to_s, :verbose=>false
    end
  end
end

Instance Attribute Details

#dependenciesObject

Compilation dependencies.



266
267
268
# File 'lib/buildr/core/compile.rb', line 266

def dependencies
  @dependencies
end

#optionsObject (readonly)

Returns the compiler options.



301
302
303
# File 'lib/buildr/core/compile.rb', line 301

def options
  @options
end

#projectObject (readonly)

The project this task belongs to.



343
344
345
# File 'lib/buildr/core/compile.rb', line 343

def project
  @project
end

#sourcesObject

Source directories.



238
239
240
# File 'lib/buildr/core/compile.rb', line 238

def sources
  @sources
end

#targetObject (readonly)

The target directory for the compiled code.



284
285
286
# File 'lib/buildr/core/compile.rb', line 284

def target
  @target
end

#usageObject (readonly)

The usage, one of :main or :test.



346
347
348
# File 'lib/buildr/core/compile.rb', line 346

def usage
  @usage
end

Instance Method Details

#classpathObject

Deprecated: Use dependencies instead.



254
255
256
257
# File 'lib/buildr/core/compile.rb', line 254

def classpath
  Buildr.application.deprecated 'Use dependencies instead.'
  dependencies
end

#classpath=(artifacts) ⇒ Object

Deprecated: Use dependencies= instead.



260
261
262
263
# File 'lib/buildr/core/compile.rb', line 260

def classpath=(artifacts)
  Buildr.application.deprecated 'Use dependencies= instead.'
  self.dependencies = artifacts
end

#compilerObject

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).



321
322
323
324
# File 'lib/buildr/core/compile.rb', line 321

def compiler
  guess_compiler unless @compiler
  @compiler && @compiler.class.to_sym
end

#from(*sources) ⇒ Object

:call-seq:

from(*sources) => self

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

For example:

compile.from('src/java').into('classes').with('module1.jar')


247
248
249
250
251
# File 'lib/buildr/core/compile.rb', line 247

def from(*sources)  
  @sources |= sources.flatten
  guess_compiler if @compiler.nil? && sources.flatten.any? { |source| File.exist?(source) }
  self
end

#into(path) ⇒ Object

:call-seq:

into(path) => self

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:

compile(src_dir).into(target_dir).with(artifacts)

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



295
296
297
298
# File 'lib/buildr/core/compile.rb', line 295

def into(path)
  @target = file(path.to_s).enhance([self]) unless @target.to_s == path.to_s
  self
end

#languageObject

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



327
328
329
# File 'lib/buildr/core/compile.rb', line 327

def language
  compiler && @compiler.class.language
end

#packagingObject

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



332
333
334
# File 'lib/buildr/core/compile.rb', line 332

def packaging
  compiler && @compiler.class.packaging
end

#timestampObject

:nodoc:



336
337
338
339
340
# File 'lib/buildr/core/compile.rb', line 336

def timestamp #:nodoc:
  # If we compiled successfully, then the target directory reflects that.
  # If we didn't, see needed?
  target ? target.timestamp : Rake::EARLY
end

#using(*args) ⇒ Object

:call-seq:

using(options) => self

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')
compile.using(:scala)


312
313
314
315
316
# File 'lib/buildr/core/compile.rb', line 312

def using(*args)
  args.pop.each { |key, value| options.send "#{key}=", value } if Hash === args.last
  self.compiler = args.pop until args.empty?
  self
end

#with(*specs) ⇒ Object

:call-seq:

with(*artifacts) => self

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'))


278
279
280
281
# File 'lib/buildr/core/compile.rb', line 278

def with(*specs)
  @dependencies |= Buildr.artifacts(specs.flatten).uniq
  self
end