buildr 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -159,7 +159,7 @@ module Buildr
159
159
  # For example:
160
160
  # compile.with("module1.jar", "log4j:log4j:jar:1.0", project("foo"))
161
161
  def with(*specs)
162
- @classpath |= artifacts(specs.flatten).uniq
162
+ @classpath |= Buildr.artifacts(specs.flatten).uniq
163
163
  self
164
164
  end
165
165
 
@@ -368,7 +368,7 @@ module Buildr
368
368
  #
369
369
  # Adds files and artifacts as classpath dependencies, and returns self.
370
370
  def with(*specs)
371
- @classpath |= artifacts(specs.flatten).uniq
371
+ @classpath |= Buildr.artifacts(specs.flatten).uniq
372
372
  self
373
373
  end
374
374
 
@@ -538,7 +538,7 @@ module Buildr
538
538
  resources = Java::ResourcesTask.define_task("resources")
539
539
  resources.filter.from project.path_to("src/main/resources")
540
540
  # Compile task requires prepare and performs resources, if anything compiled.
541
- compile = Java::CompileTask.define_task("compile"=>[prepare, resources]) { |task| project.resources.invoke }
541
+ compile = Java::CompileTask.define_task("compile"=>[prepare, resources])
542
542
  project.path_to("src/main/java").tap { |dir| compile.from dir if File.exist?(dir) }
543
543
  compile.into project.path_to("target/classes")
544
544
  resources.filter.into project.compile.target
@@ -24,7 +24,7 @@ module Buildr
24
24
  sources << File.expand_path(Rake.application.rakefile, root_path) if Rake.application.rakefile
25
25
 
26
26
  # Only for projects that are Eclipse packagable.
27
- if project.packages.detect { |pkg| pkg.type =~ /(jar)|(war)|(rar)|(mar)|(aar)/ }
27
+ if project.packages.detect { |pkg| pkg.type.to_s =~ /(jar)|(war)|(rar)|(mar)|(aar)/ }
28
28
  eclipse.enhance [ file(project.path_to(".classpath")), file(project.path_to(".project")) ]
29
29
 
30
30
  # The only thing we need to look for is a change in the Rakefile.
@@ -7,7 +7,7 @@ module Buildr
7
7
  module Java
8
8
 
9
9
  # Options accepted by #java and other methods here.
10
- JAVA_OPTIONS = [ :verbose, :classpath, :name, :java_args ]
10
+ JAVA_OPTIONS = [ :verbose, :classpath, :name, :java_args, :properties ]
11
11
  # Classpath dependencies available when running JUnit.
12
12
  JUNIT_REQUIRES = [ "junit:junit:jar:3.8.1", "jmock:jmock:jar:1.1.0" ]
13
13
 
@@ -26,11 +26,13 @@ module Buildr
26
26
  include Singleton
27
27
 
28
28
  def initialize() #:nodoc:
29
- @classpath = []
29
+ @classpath = [Java.tools_jar]
30
30
  @onload = []
31
31
  onload do
32
- Rjb.load(artifacts(classpath).each { |task| task.invoke if task.respond_to?(:invoke) }.
33
- map(&:to_s).join(File::PATH_SEPARATOR))
32
+ onload do
33
+ Rjb.load(Buildr.artifacts(classpath).each { |task| task.invoke if task.respond_to?(:invoke) }.
34
+ map(&:to_s).join(File::PATH_SEPARATOR))
35
+ end
34
36
  end
35
37
  end
36
38
 
@@ -99,6 +101,7 @@ module Buildr
99
101
  # * :classpath -- One or more file names, tasks or artifact specifications.
100
102
  # These are all expanded into artifacts, and all tasks are invoked.
101
103
  # * :java_args -- Any additional arguments to pass (e.g. -hotspot, -xms)
104
+ # * :properties -- Hash of system properties (e.g. "path"=>base_dir).
102
105
  # * :name -- Shows this name, otherwise shows the first argument (the class name).
103
106
  # * :verbose -- If true, prints the command and all its argument.
104
107
  def java(*args)
@@ -110,6 +113,7 @@ module Buildr
110
113
  cmd_args = []
111
114
  classpath = classpath_from(options)
112
115
  cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
116
+ options[:properties].each { |k, v| cmd_args << "-D#{k}=#{v}" } if options[:properties]
113
117
  cmd_args += options[:java_args].flatten if options[:java_args]
114
118
  cmd_args += args.flatten.compact
115
119
  cmd_args << { :verbose=>options[:verbose] }
@@ -131,29 +135,29 @@ module Buildr
131
135
  # generated class files when compiling.
132
136
  # * :classpath -- One or more file names, tasks or artifact specifications.
133
137
  # These are all expanded into artifacts, and all tasks are invoked.
134
- # * :verbose -- If true, prints the command and all its argument and also runs
135
- # Apt in verbose mode (the default is -nowarn).
136
138
  def apt(*args)
137
139
  options = Hash === args.last ? args.pop : {}
138
- options[:verbose] ||= Rake.application.options.trace || false
139
- fu_check_options options, :verbose, :compile, :source, :output, :classpath
140
+ fu_check_options options, :compile, :source, :output, :classpath
140
141
 
141
142
  files = args.flatten.map(&:to_s).
142
143
  collect { |arg| File.directory?(arg) ? FileList["#{arg}/**/*.java"] : arg }.flatten
143
- args = [ options[:verbose] ? "-verbose" : "-nowarn" ]
144
+ cmd_args = [ Rake.application.options.trace ? "-verbose" : "-nowarn" ]
144
145
  if options[:compile]
145
- args << "-d" << options[:output].to_s
146
+ cmd_args << "-d" << options[:output].to_s
146
147
  else
147
- args << "-nocompile" << "-s" << options[:output].to_s
148
+ cmd_args << "-nocompile" << "-s" << options[:output].to_s
148
149
  end
149
- args << "-source" << options[:source] if options[:source]
150
+ cmd_args << "-source" << options[:source] if options[:source]
150
151
  classpath = classpath_from(options)
151
- args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
152
- args += files
153
- args << { :verbose=>options[:verbose] }
152
+ cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
153
+ cmd_args += files
154
154
  unless Rake.application.options.dryrun
155
155
  puts "Running apt" if verbose
156
- sh(path_to_bin("apt"), *args) { |ok, res| fail "Failed to execute apt, see errors above" unless ok }
156
+ puts (["apt"] + cmd_args).join(" ") if Rake.application.options.trace
157
+ Java.rjb do |rjb|
158
+ rjb.import("com.sun.tools.apt.Main").process(cmd_args) == 0 or
159
+ fail "Failed to process annotations, see errors above"
160
+ end
157
161
  end
158
162
  end
159
163
 
@@ -169,11 +173,9 @@ module Buildr
169
173
  # * :sourcepath -- Additional source paths to use.
170
174
  # * :javac_args -- Any additional arguments to pass (e.g. -extdirs, -encoding)
171
175
  # * :name -- Shows this name, otherwise shows the working directory.
172
- # * :verbose -- If true, prints the command and all its argument.
173
176
  def javac(*args)
174
177
  options = Hash === args.last ? args.pop : {}
175
- options[:verbose] ||= Rake.application.options.trace || false
176
- fu_check_options options, :verbose, :classpath, :sourcepath, :output, :javac_args, :name
178
+ fu_check_options options, :classpath, :sourcepath, :output, :javac_args, :name
177
179
 
178
180
  files = args.flatten.each { |f| f.invoke if f.respond_to?(:invoke) }.map(&:to_s).
179
181
  collect { |arg| File.directory?(arg) ? FileList["#{arg}/**/*.java"] : arg }.flatten
@@ -181,16 +183,18 @@ module Buildr
181
183
 
182
184
  cmd_args = []
183
185
  classpath = classpath_from(options)
184
- #classpath += options[:output] if options[:output]
185
186
  cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty?
186
187
  cmd_args << "-sourcepath" << options[:sourcepath].join(File::PATH_SEPARATOR) if options[:sourcepath]
187
188
  cmd_args << "-d" << options[:output].to_s if options[:output]
188
189
  cmd_args += options[:javac_args].flatten if options[:javac_args]
189
190
  cmd_args += files
190
- cmd_args << { :verbose=>options[:verbose] }
191
191
  unless Rake.application.options.dryrun
192
192
  puts "Compiling #{files.size} source files in #{name}" if verbose
193
- sh(path_to_bin("javac"), *cmd_args) { |ok, res| fail "Failed to compile, see errors above" unless ok }
193
+ puts (["javac"] + cmd_args).join(" ") if Rake.application.options.trace
194
+ Java.rjb do |rjb|
195
+ rjb.import("com.sun.tools.javac.Main").compile(cmd_args) == 0 or
196
+ fail "Failed to compile, see errors above"
197
+ end
194
198
  end
195
199
  end
196
200
 
@@ -204,7 +208,6 @@ module Buildr
204
208
  # * :classpath -- Array of classpath dependencies.
205
209
  # * :sourcepath -- Array of sourcepaths (paths or tasks).
206
210
  # * :name -- Shows this name, otherwise shows the working directory.
207
- # * :verbose -- If you want an overload of details.
208
211
  #
209
212
  # All other options are passed to Javadoc as following:
210
213
  # * true -- As is, for example, :author=>true becomes -author
@@ -213,39 +216,38 @@ module Buildr
213
216
  # * array -- Option with set of values separated by spaces.
214
217
  def javadoc(*args)
215
218
  options = Hash === args.last ? args.pop : {}
216
- options[:verbose] ||= Rake.application.options.trace || false
217
219
 
218
- Tempfile.open("javadoc") do |opt_file|
219
- options.reject { |key, value| [:output, :verbose, :name, :sourcepath, :classpath].include?(key) }.
220
- each { |key, value| value.invoke if value.respond_to?(:invoke) }.
221
- each do |key, value|
222
- case value
223
- when true, nil
224
- opt_file.puts "-#{key}"
225
- when false
226
- opt_file.puts "-no#{key}"
227
- when Array
228
- opt_file.puts "-#{key} " << value.map { |val| %q{"#{val}"} }.join(" ")
229
- when Hash
230
- value.each { |k,v| opt_file.puts "-#{key} #{k} #{v}" }
231
- else
232
- opt_file.puts "-#{key} \"#{value}\""
233
- end
234
- end
235
- [:sourcepath, :classpath].each do |option|
236
- options[option].to_a.flatten.tap do |paths|
237
- opt_file.puts "-#{option} " << paths.flatten.map(&:to_s).join(File::PATH_SEPARATOR) unless paths.empty?
220
+ cmd_args = [ "-d", options[:output], Rake.application.options.trace ? "-verbose" : "-quiet" ]
221
+ options.reject { |key, value| [:output, :name, :sourcepath, :classpath].include?(key) }.
222
+ each { |key, value| value.invoke if value.respond_to?(:invoke) }.
223
+ each do |key, value|
224
+ case value
225
+ when true, nil
226
+ cmd_args << "-#{key}"
227
+ when false
228
+ cmd_args << "-no#{key}"
229
+ when Array
230
+ cmd_args << "-#{key}"
231
+ cmd_args += value.map(&:to_s)
232
+ when Hash
233
+ value.each { |k,v| cmd_args << "-#{key}" << k.to_s << v.to_s }
234
+ else
235
+ cmd_args << "-#{key}" << value.to_s
238
236
  end
239
237
  end
240
- opt_file.puts args.flatten.uniq.join(" ")
241
- opt_file.flush
242
- cmd_args = [ "-d", options[:output], options[:verbose] ? "-verbose" : "-quiet", "@#{opt_file.path}"]
243
- cmd_args << { :verbose=>options[:verbose] }
244
- name = options[:name] || Dir.pwd
245
- unless Rake.application.options.dryrun
246
- puts "Generating Javadoc for #{name}" if verbose
247
- puts File.read(opt_file.path) if options[:verbose]
248
- sh(path_to_bin("javadoc"), *cmd_args) { |ok, res| fail "Failed to generate Javadocs, see errors above" unless ok }
238
+ [:sourcepath, :classpath].each do |option|
239
+ options[option].to_a.flatten.tap do |paths|
240
+ cmd_args << "-#{option}" << paths.flatten.map(&:to_s).join(File::PATH_SEPARATOR) unless paths.empty?
241
+ end
242
+ end
243
+ cmd_args += args.flatten.uniq
244
+ name = options[:name] || Dir.pwd
245
+ unless Rake.application.options.dryrun
246
+ puts "Generating Javadoc for #{name}" if verbose
247
+ puts (["javadoc"] + cmd_args).join(" ") if Rake.application.options.trace
248
+ Java.rjb do |rjb|
249
+ rjb.import("com.sun.tools.javadoc.Main").execute(cmd_args) == 0 or
250
+ fail "Failed to generate Javadocs, see errors above"
249
251
  end
250
252
  end
251
253
  end
@@ -260,17 +262,19 @@ module Buildr
260
262
  # The last argument may be a Hash with additional options:
261
263
  # * :classpath -- One or more file names, tasks or artifact specifications.
262
264
  # These are all expanded into artifacts, and all tasks are invoked.
265
+ # * :properties -- Hash of system properties (e.g. "path"=>base_dir).
263
266
  # * :verbose -- If true, prints the command and all its argument.
264
267
  def junit(*args)
265
268
  options = Hash === args.last ? args.pop : {}
266
269
  options[:verbose] ||= Rake.application.options.trace || false
267
- fu_check_options options, :verbose, :classpath
270
+ fu_check_options options, :verbose, :classpath, :properties
268
271
 
269
272
  classpath = classpath_from(options) + junit_artifacts
270
273
  tests = args.flatten
271
274
  failed = tests.inject([]) do |failed, test|
272
275
  begin
273
- java "junit.textui.TestRunner", test, :classpath=>classpath, :name=>"tests in #{test}", :verbose=>options[:verbose]
276
+ java "junit.textui.TestRunner", test, :classpath=>classpath, :properties=>options[:properties],
277
+ :name=>"#{test}", :verbose=>options[:verbose]
274
278
  failed
275
279
  rescue
276
280
  failed << test
@@ -299,7 +303,7 @@ module Buildr
299
303
  def rjb()
300
304
  if block_given?
301
305
  RjbWrapper.instance.load
302
- yield
306
+ yield RjbWrapper.instance
303
307
  else
304
308
  RjbWrapper.instance
305
309
  end
@@ -323,7 +327,7 @@ module Buildr
323
327
  # each of the artifacts and returns an array of paths.
324
328
  def classpath_from(options)
325
329
  classpath = (options[:classpath] || []).collect
326
- artifacts(classpath).each { |t| t.invoke if t.respond_to?(:invoke) }.map(&:to_s)
330
+ Buildr.artifacts(classpath).each { |t| t.invoke if t.respond_to?(:invoke) }.map(&:to_s)
327
331
  end
328
332
 
329
333
  # :call-seq:
@@ -331,7 +335,7 @@ module Buildr
331
335
  #
332
336
  # Returns the JUnit artifacts as paths, after downloading and installing them (if necessary).
333
337
  def junit_artifacts()
334
- @junit_artifacts ||= artifacts(JUNIT_REQUIRES).each { |task| task.invoke }.map(&:to_s)
338
+ @junit_artifacts ||= Buildr.artifacts(JUNIT_REQUIRES).each { |task| task.invoke }.map(&:to_s)
335
339
  end
336
340
 
337
341
  end
@@ -360,7 +364,6 @@ module Buildr
360
364
 
361
365
  end
362
366
 
363
- class Project
364
- include Java
365
- end
367
+ include Java
368
+
366
369
  end
@@ -106,7 +106,7 @@ module Buildr
106
106
  def []=(key, value) #:nodoc:
107
107
  case key.to_sym
108
108
  when :libs
109
- self.include artifacts(value), :path=>"WEB-INF/lib"
109
+ self.include Buildr.artifacts(value), :path=>"WEB-INF/lib"
110
110
  when :classes
111
111
  self.include value, :path=>"WEB-INF/classes", :as=>"."
112
112
  else
@@ -241,17 +241,22 @@ module Buildr
241
241
  # Make it an artifact using the specifications, and tell it how to create a POM.
242
242
  package.extend ActsAsArtifact
243
243
  package.send :apply_spec, options
244
- package.pom.enhance do |pom|
244
+ # Another task to create the POM file.
245
+ pom_spec = package.to_spec_hash.merge(:type=>:pom)
246
+ pom = file(Buildr.repositories.locate(pom_spec))
247
+ pom.extend ActsAsArtifact
248
+ pom.send :apply_spec, pom_spec
249
+ pom.enhance do
245
250
  mkpath File.dirname(pom.name), :verbose=>false
246
251
  File.open(pom.name, "w") do |file|
247
252
  xml = Builder::XmlMarkup.new(:target=>file, :indent=>2)
248
253
  xml.instruct!
249
254
  xml.project do
250
255
  xml.modelVersion "4.0.0"
251
- xml.groupId options[:group]
252
- xml.artifactId options[:id]
253
- xml.version options[:version]
254
- xml.classifier options[:classifier] if options[:classifier]
256
+ xml.groupId pom.group
257
+ xml.artifactId pom.id
258
+ xml.version pom.version
259
+ xml.classifier pom.classifier if pom.classifier
255
260
  end
256
261
  end
257
262
  end
@@ -264,27 +269,27 @@ module Buildr
264
269
  # in the target directory, we need to copy it into the local repository. However, the
265
270
  # POM artifact (created by calling artifact on its spec) is already mapped to its right
266
271
  # place in the local repository, so we only need to invoke it.
267
- installed = file(repositories.locate(package)=>package) { |task|
272
+ installed = file(Buildr.repositories.locate(package)=>package) { |task|
268
273
  verbose(Rake.application.options.trace || false) do
269
274
  mkpath File.dirname(task.name), :verbose=>false
270
275
  cp package.name, task.name
271
276
  end
272
277
  puts "Installed #{task.name}" if verbose
273
278
  }
274
- task "install"=>[installed, package.pom]
279
+ task "install"=>[installed, pom]
275
280
  task "uninstall" do |task|
276
281
  verbose(Rake.application.options.trace || false) do
277
- [ installed, package.pom ].map(&:to_s).each { |file| rm file if File.exist?(file) }
282
+ [ installed, pom ].map(&:to_s).each { |file| rm file if File.exist?(file) }
278
283
  end
279
284
  end
280
- task("deploy") { deploy(package, package.pom) }
285
+ task("deploy") { deploy(package, pom) }
281
286
 
282
287
  # Add the package to the list of packages created by this project, and
283
288
  # register it as an artifact. The later is required so if we look up the spec
284
289
  # we find the package in the project's target directory, instead of finding it
285
290
  # in the local repository and attempting to install it.
286
291
  packages << package
287
- Artifact.register package, package.pom
292
+ Artifact.register package, pom
288
293
  end
289
294
  end
290
295
 
@@ -21,10 +21,11 @@ module Buildr
21
21
  @paths = []
22
22
  @include = ["*Test", "*Suite"]
23
23
  @exclude = []
24
+ @options = {}
24
25
  enhance do |task|
25
26
  unless test_cases.empty?
26
27
  puts "Running tests in #{name}" if verbose
27
- passed, failed = Java.junit(test_cases, :classpath=>classpath + @paths)
28
+ passed, failed = Java.junit(test_cases, @options.merge(:classpath=>classpath + @paths))
28
29
  fail "The following tests failed:\n#{failed.join("\n")}" unless failed.empty?
29
30
  end
30
31
  end
@@ -76,7 +77,22 @@ module Buildr
76
77
  # Specify artifacts (specs, tasks, files, etc) to include in the classpath when running
77
78
  # the test cases.
78
79
  def with(*files)
79
- @classpath |= artifacts(files.flatten).uniq
80
+ @classpath |= Buildr.artifacts(files.flatten).uniq
81
+ self
82
+ end
83
+
84
+ # Returns the JUnit options.
85
+ attr_reader :options
86
+
87
+ # :call-seq:
88
+ # using(options) => self
89
+ #
90
+ # Sets the JUnit options from a hash and returns self. Right now supports passing properties to JUnit.
91
+ #
92
+ # For example:
93
+ # test.junit.using :properties=>{ "root"=>base_dir }
94
+ def using(options)
95
+ options.each { |k,v| @options[k.to_sym] = v }
80
96
  self
81
97
  end
82
98
 
@@ -168,6 +184,9 @@ module Buildr
168
184
  #
169
185
  # You can also include only specific test cases, or exclude otherwise included test cases
170
186
  # using #include and #exclude.
187
+ #
188
+ # The Java property baseDir is set to the classes directory, you can use it to pick up test resources
189
+ # that you cannot access using getResources().
171
190
  def junit()
172
191
  @project.task("test:junit")
173
192
  end
@@ -225,6 +244,7 @@ module Buildr
225
244
 
226
245
  end
227
246
 
247
+
228
248
  class Project
229
249
 
230
250
  # :call-seq:
@@ -268,7 +288,6 @@ module Buildr
268
288
  resources.filter.from project.path_to("src/test/resources")
269
289
  # Similar to the regular compile task but using different paths.
270
290
  compile = Java::CompileTask.define_task("test:compile"=>[project.compile, project.test.prepare, project.test.resources])
271
- compile.enhance { project.test.resources.invoke }
272
291
  project.path_to("src/test/java").tap { |dir| compile.from dir if File.exist?(dir) }
273
292
  compile.into project.path_to("target/test-classes")
274
293
  resources.filter.into compile.target
@@ -285,6 +304,11 @@ module Buildr
285
304
  project.test.junit.with project.test.compile.classpath
286
305
  # Tell the JUnit task where to pick the test cases from.
287
306
  project.test.junit.from project.test.compile.target
307
+ project.test.junit.options[:properties] ||= {}
308
+ project.test.junit.options.tap do |options|
309
+ options[:properties] ||= {}
310
+ options[:properties]["baseDir"] ||= project.test.compile.target.to_s
311
+ end
288
312
  end
289
313
  end
290
314
 
@@ -294,7 +318,8 @@ module Buildr
294
318
  rule /^test:.*$/ do |task|
295
319
  test = task.name.scan(/test:(.*)/)[0][0]
296
320
  Project.projects.select { |project| project.base_dir == Rake.application.original_dir }.
297
- map { |project| project.test }.each { |task| task.include("*#{test}").invoke }
321
+ map { |project| project.test }.each { |task| task.junit.instance_eval { @include = ["*#{test}"] ; @exclude.clear } }.
322
+ each(&:invoke)
298
323
  end
299
324
 
300
325
  # Require tests after build unless TEST option is off.
@@ -31,4 +31,5 @@ module Buildr
31
31
  file, deps = Rake.application.resolve_args(args)
32
32
  ConcatTask.define_task File.expand_path(file)=>deps
33
33
  end
34
+
34
35
  end
@@ -78,15 +78,11 @@ module Buildr
78
78
 
79
79
  # Documented in ZipTask.
80
80
  def merge(*files)
81
- if Hash === files.last
82
- options = files.pop
83
- else
84
- options = {}
85
- end
81
+ options = files.pop if Hash === files.last
86
82
 
87
- if options[:path]
83
+ if options && options[:path]
88
84
  path(options[:path]).merge *files +[ options.reject { |k,v| k == :path } ]
89
- elsif options.keys.empty?
85
+ elsif options.nil? || options.keys.empty?
90
86
  files.collect do |file|
91
87
  @sources << proc { file.to_s }
92
88
  expander = ZipExpander.new(file)