Class: Buildr::Java::CompileTask

Inherits:
Rake::Task
  • Object
show all
Defined in:
lib/java/compile.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CompileTask

Returns a new instance of CompileTask.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/java/compile.rb', line 89

def initialize(*args)
  super
  if name[":"] # Only if in namespace
    parent = Rake::Task["^compile"]
    if parent && parent.respond_to?(:options)
      @options = Options.new(parent.options)
    end
  end

  enhance do |task|
    # Do we have any files to compile?
    if target && files.empty?
      puts "All source files are up to date" if Rake.application.options.trace && !sources.empty?
    elsif target
      mkpath target, :verbose=>false
      Java.javac files, :sourcepath=>sourcepath, :classpath=>real_classpath,
        :output=>target, :javac_args=>options.javac_args, :name=>task.name
      # By touching the target we let other tasks know we did something,
      # and also prevent recompiling again for classpath dependencies.
      touch target, :verbose=>false
      @compiled = true
    end
  end
end

Instance Attribute Details

#targetObject

The target directory for the generated class files.



87
88
89
# File 'lib/java/compile.rb', line 87

def target
  @target
end

Instance Method Details

#classpathObject

Array of classpath dependencies: files, file tasks, artifacts specs.



124
125
126
# File 'lib/java/compile.rb', line 124

def classpath()
  @classpath ||= []
end

#classpath=(paths) ⇒ Object



128
129
130
# File 'lib/java/compile.rb', line 128

def classpath=(paths)
  @classpath = paths.flatten
end

#compiled?Boolean

Returns true if any classes were compiled.

Returns:

  • (Boolean)


183
184
185
# File 'lib/java/compile.rb', line 183

def compiled?()
  @compiled || false
end

#into(dir) ⇒ Object

Sets the target directory and returns self. For example:

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


156
157
158
159
# File 'lib/java/compile.rb', line 156

def into(dir)
  self.target = dir
  self
end

#needed?Boolean

Returns:

  • (Boolean)


191
192
193
194
195
196
# File 'lib/java/compile.rb', line 191

def needed?()
  return false unless target
  return true unless File.exist?(target)
  real_classpath.any? { |f| File.stat(f).mtime > timestamp } ||
    files.any? { |f| File.stat(f).mtime > timestamp }
end

#optionsObject

Returns the compiler options.



133
134
135
# File 'lib/java/compile.rb', line 133

def options()
  @options ||= Options.new
end

#options=(arg) ⇒ Object

Sets the compile options based on a hash of values, or reset to the default by passing nil.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/java/compile.rb', line 139

def options=(arg)
  case arg
  when Options
    @options = arg
  when Hash
    options.clear
    arg.each { |k,v| options.send "#{k}=", v }
  when nil
    options.clear
  else
    raise ArgumentError, "Expecting Options, hash or nil (to reset)"
  end
  @options
end

#sourcesObject

An array of source directories and files.



115
116
117
# File 'lib/java/compile.rb', line 115

def sources()
  @sources ||= []
end

#sources=(paths) ⇒ Object



119
120
121
# File 'lib/java/compile.rb', line 119

def sources=(paths)
  @sources = paths.flatten
end

#timestampObject



187
188
189
# File 'lib/java/compile.rb', line 187

def timestamp()
  @timestamp ||= File.exist?(target) ? File.stat(target).mtime : Rake::EARLY
end

#using(hash) ⇒ Object

Sets the compiler options and returns self. For example:

compile(src_dir).using(:warnings=>true, :verbose=>true)


177
178
179
180
# File 'lib/java/compile.rb', line 177

def using(hash)
  self.options = hash
  self
end

#with(*args) ⇒ Object

Adds files and artifacts to the classpath and returns self. For example:

compile(src_dir).to(target_dir).with(artifact, file, task)


170
171
172
173
# File 'lib/java/compile.rb', line 170

def with(*args)
  self.classpath |= artifacts(*args)
  self
end