Repository: buildr Updated Branches: refs/heads/master 2d716e04c -> ac9ab5bf9
BUILDR-653: Using Eclipse compiler (ECJ) Make it easier to use the Eclipse Compiler, and allow to set a custom version. Project: http://git-wip-us.apache.org/repos/asf/buildr/repo Commit: http://git-wip-us.apache.org/repos/asf/buildr/commit/7f5b25e7 Tree: http://git-wip-us.apache.org/repos/asf/buildr/tree/7f5b25e7 Diff: http://git-wip-us.apache.org/repos/asf/buildr/diff/7f5b25e7 Branch: refs/heads/master Commit: 7f5b25e7f0f502362b8c6bb139b0b6c7869374df Parents: 2d716e0 Author: Antoine Toulme <[email protected]> Authored: Mon Aug 8 21:51:51 2016 -0700 Committer: Antoine Toulme <[email protected]> Committed: Tue Aug 9 22:43:31 2016 -0700 ---------------------------------------------------------------------- CHANGELOG | 2 + buildr.gemspec | 1 + lib/buildr.rb | 1 + lib/buildr/java/ecj.rb | 19 +- spec/java/ecj_spec.rb | 287 ++++++++++++++++--- tests/.gitignore | 1 + tests/compile_with_parent/Buildfile | 2 +- tests/compile_with_parent/lib/.gitkeep | 0 tests/helloWorldEcj/Buildfile | 22 ++ tests/helloWorldEcj/buildr.yml | 1 + .../helloWorldEcj/src/main/java/HelloWorld.java | 23 ++ tests/integration_testing.rb | 12 +- 12 files changed, 319 insertions(+), 52 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/CHANGELOG ---------------------------------------------------------------------- diff --git a/CHANGELOG b/CHANGELOG index 6d2c13d..45d09bb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -8,6 +8,7 @@ * Change: Update xml-simple to 1.1.5 * Change: Update jekyll to 3.1.3 * Change: Update rdoc to 4.2.2 +* Change: Update ecj to 4.5.1 * Added: Travis badge to README.rdoc * Added: BUILDR-577 Allow remote repo to be added with http basic auth support. Submitted by Michael Guymon. * Fixed: BUILDR-207 remove 'Skipping tests' messages @@ -16,6 +17,7 @@ * Fixed: BUILDR-565 resources are not included in the war if defined after package call * Fixed: BUILDR-621 ZipTask creates zip file with entries not sorted by path causing very slow unzipping. * Fixed: BUILDR-695 transitive doesn't support ${project.parent.version} in POM. +* Fixed: BUILDR-653 Using Eclipse compiler (ECJ) * Change: Update the custom_pom addon to generate poms with exclusions section that excludes all transitive dependencies. This is required as buildr dependencies are not transitive while Maven's dependencies are transitive by default. http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/buildr.gemspec ---------------------------------------------------------------------- diff --git a/buildr.gemspec b/buildr.gemspec index 43e71c9..2786136 100644 --- a/buildr.gemspec +++ b/buildr.gemspec @@ -90,6 +90,7 @@ for those one-off tasks, with a language that's a joy to use. spec.add_development_dependency 'rspec-core', '2.14.5' spec.add_development_dependency 'rspec', '2.14.1' spec.add_development_dependency 'rspec-retry', '0.2.1' + spec.add_development_dependency 'test-unit', '3.2.1' spec.add_development_dependency 'ci_reporter', '1.9.0' # Ideally we would depend on psych when the platform has >= 1.9.2 support and jruby platform version > 1.6.6 #spec.add_development_dependency 'psych' if RUBY_VERSION >= '1.9.2' http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/lib/buildr.rb ---------------------------------------------------------------------- diff --git a/lib/buildr.rb b/lib/buildr.rb index b41a0c1..3f7cbee 100644 --- a/lib/buildr.rb +++ b/lib/buildr.rb @@ -76,6 +76,7 @@ require 'buildr/packaging/test_jar' require RUBY_PLATFORM == 'java' ? 'buildr/java/jruby' : 'buildr/java/rjb' require 'buildr/java/ant' require 'buildr/java/compiler' +require 'buildr/java/ecj' require 'buildr/java/external' require 'buildr/java/tests' require 'buildr/java/test_result' http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/lib/buildr/java/ecj.rb ---------------------------------------------------------------------- diff --git a/lib/buildr/java/ecj.rb b/lib/buildr/java/ecj.rb index b7b355c..ac08ae5 100644 --- a/lib/buildr/java/ecj.rb +++ b/lib/buildr/java/ecj.rb @@ -17,6 +17,20 @@ module Buildr #:nodoc: module Compiler #:nodoc: class Ecj < Javac + + VERSION = "4.5.1" + + class << self + # Current version of ECJ being used. + def version + Buildr.settings.build['ecj'] || VERSION + end + + # ECJ classpath dependencies. + def dependencies + @dependencies ||= ["org.eclipse.jdt.core.compiler:ecj:jar:#{version}"] + end + end OPTIONS = Buildr::Compiler::Javac::OPTIONS @@ -64,6 +78,5 @@ module Buildr #:nodoc: end end -Java.classpath << "org.eclipse.jdt.core.compiler:ecj:jar:3.5.1" -# Adding ecj before javac -Buildr::Compiler.compilers.unshift Buildr::Compiler::Ecj +Java.classpath << lambda { Buildr::Compiler::Ecj.dependencies } +Buildr::Compiler.compilers << Buildr::Compiler::Ecj http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/spec/java/ecj_spec.rb ---------------------------------------------------------------------- diff --git a/spec/java/ecj_spec.rb b/spec/java/ecj_spec.rb index 43e531e..f8c18d8 100644 --- a/spec/java/ecj_spec.rb +++ b/spec/java/ecj_spec.rb @@ -15,47 +15,248 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helpers')) -describe Buildr::Compiler::Ecj do - - before(:all) do - #Make ecj appear as a compiler that applies: - class Buildr::Compiler::Ecj - class << self - def applies_to?(project, task) - paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) } - paths.flatten.each { |path| - path = path.to_s - Find.find(path) {|found| - if (!File.directory?(found)) && found.match(/.*\.#{Array(source_ext).join('|')}/) - return true - end - } if File.exist? path - } - false - end + +describe 'ecj compiler' do + it 'should be explicitly referenced' do + write 'src/main/java/com/example/Test.java', 'package com.example; class Test {}' + define('foo').compile.using(:ecj).compiler.should eql(:ecj) + end + + it 'should report the language as :java' do + define('foo').compile.using(:ecj).language.should eql(:java) + end + + it 'should set the target directory to target/classes' do + define 'foo' do + lambda { compile.using(:ecj) }.should change { compile.target.to_s }.to(File.expand_path('target/classes')) + end + end + + it 'should not override existing target directory' do + define 'foo' do + compile.into('classes') + lambda { compile.using(:ecj) }.should_not change { compile.target } + end + end + + it 'should accept a task to compile from' do + p = define 'foo' do + project.version = '1' + f = file(_(:target, :generated, 'myjava')) do + mkdir_p _(:target, :generated, 'myjava') + File.open("#{_(:target, :generated, 'myjava')}/Foo.java", "wb") do |f| + f.write "public class Foo {}" end end + + compile.from(f) + package(:jar) + end.compile.invoke + file('target/classes/Foo.class').should exist + end + + it 'should not change existing list of sources' do + define 'foo' do + compile.from('sources') + lambda { compile.using(:ecj) }.should_not change { compile.sources } + end + end + + # Doesn't work under jdk1.5 - caused in one of the commits 1167678, 1170604, 1170605, 1180125 + if Java.java.lang.System.getProperty("java.runtime.version") >= "1.6" + it 'should include classpath dependencies' do + write 'src/dependency/Dependency.java', 'class Dependency {}' + define 'dependency', :version=>'1.0' do + compile.from('src/dependency').into('target/dependency') + package(:jar) + end + write 'src/test/DependencyTest.java', 'class DependencyTest { Dependency _var; }' + define('foo').compile.from('src/test').with(project('dependency')).invoke + file('target/classes/DependencyTest.class').should exist end + end + + it 'should include tools.jar dependency' do + write 'src/main/java/UseApt.java', <<-JAVA + import com.sun.mirror.apt.AnnotationProcessor; + public class UseApt { } + JAVA + define('foo').compile.using(:ecj).invoke + file('target/classes/UseApt.class').should exist + end + + it 'should ignore package-info.java files in up-to-date check' do + write 'src/main/java/foo/Test.java', 'package foo; class Test { public void foo() {} }' + write 'src/main/java/foo/package-info.java', 'package foo;' + lambda{ define('foo').compile.invoke }.should run_task('foo:compile') + lambda{ define('bar').compile.invoke }.should_not run_task('bar:compile') + end +end + + +describe 'ecj compiler options' do + def compile_task + @compile_task ||= define('foo').compile.using(:ecj) + end + + def ecj_args + compile_task.instance_eval { @compiler }.send(:ecj_args) + end + + it 'should set warnings option to false by default' do + compile_task.options.warnings.should be_false + end + + it 'should set warnings option to true when running with --verbose option' do + verbose true + compile_task.options.warnings.should be_false + end + + it 'should use -nowarn argument when warnings is false' do + compile_task.using(:warnings=>false) + ecj_args.should include('-warn:none') + end - it "should be the default Java compiler once loaded" do - write 'src/main/java/Foo.java', 'public class Foo {}' - foo = define('foo') - foo.compile.compiler.should == :ecj + it 'should not use -nowarn argument when warnings is true' do + compile_task.using(:warnings=>true) + ecj_args.should_not include('-warn:none') end - describe "should compile a Java project just in the same way javac does" do - javac_spec = File.read(File.join(File.dirname(__FILE__), "compiler_spec.rb")) - javac_spec = javac_spec.match(Regexp.escape("require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helpers'))\n")).post_match - javac_spec.gsub!("javac", "ecj") - javac_spec.gsub!("nowarn", "warn:none") - eval(javac_spec) + it 'should not use -verbose argument by default' do + ecj_args.should_not include('-verbose') + end + + it 'should use -verbose argument when running with --trace=ecj option' do + Buildr.application.options.trace_categories = [:ecj] + ecj_args.should include('-verbose') + end + + it 'should set debug option to true by default' do + compile_task.options.debug.should be_true + end + + it 'should set debug option to false based on Buildr.options' do + Buildr.options.debug = false + compile_task.options.debug.should be_false + end + + it 'should set debug option to false based on debug environment variable' do + ENV['debug'] = 'no' + compile_task.options.debug.should be_false + end + + it 'should set debug option to false based on DEBUG environment variable' do + ENV['DEBUG'] = 'no' + compile_task.options.debug.should be_false + end + + it 'should use -g argument when debug option is true' do + compile_task.using(:debug=>true) + ecj_args.should include('-g') + end + + it 'should not use -g argument when debug option is false' do + compile_task.using(:debug=>false) + ecj_args.should_not include('-g') + end + + it 'should set deprecation option to false by default' do + compile_task.options.deprecation.should be_false + end + + it 'should use -deprecation argument when deprecation is true' do + compile_task.using(:deprecation=>true) + ecj_args.should include('-deprecation') + end + + it 'should not use -deprecation argument when deprecation is false' do + compile_task.using(:deprecation=>false) + ecj_args.should_not include('-deprecation') + end + + it 'should not set source option by default' do + compile_task.options.source.should be_nil + ecj_args.should_not include('-source') + end + + it 'should not set target option by default' do + compile_task.options.target.should be_nil + ecj_args.should_not include('-target') + end + + it 'should use -source nn argument if source option set' do + compile_task.using(:source=>'1.5') + ecj_args.should include('-source', '1.5') + end + + it 'should use -target nn argument if target option set' do + compile_task.using(:target=>'1.5') + ecj_args.should include('-target', '1.5') + end + + it 'should set lint option to false by default' do + compile_task.options.lint.should be_false + end + + it 'should use -lint argument if lint option is true' do + compile_task.using(:lint=>true) + ecj_args.should include('-Xlint') + end + + it 'should use -lint argument with value of option' do + compile_task.using(:lint=>'all') + ecj_args.should include('-Xlint:all') + end + + it 'should use -lint argument with value of option as array' do + compile_task.using(:lint=>['path', 'serial']) + ecj_args.should include('-Xlint:path,serial') + end + + it 'should not set other option by default' do + compile_task.options.other.should be_nil + end + + it 'should pass other argument if other option is string' do + compile_task.using(:other=>'-Xprint') + ecj_args.should include('-Xprint') + end + + it 'should pass other argument if other option is array' do + compile_task.using(:other=>['-Xstdout', 'msgs']) + ecj_args.should include('-Xstdout', 'msgs') + end + + it 'should complain about options it doesn\'t know' do + write 'source/Test.java', 'class Test {}' + compile_task.using(:unknown=>'option') + lambda { compile_task.from('source').invoke }.should raise_error(ArgumentError, /no such option/i) + end + + it 'should inherit options from parent' do + define 'foo' do + compile.using(:warnings=>true, :debug=>true, :deprecation=>true, :source=>'1.5', :target=>'1.4') + define 'bar' do + compile.using(:ecj) + compile.options.warnings.should be_true + compile.options.debug.should be_true + compile.options.deprecation.should be_true + compile.options.source.should eql('1.5') + compile.options.target.should eql('1.4') + end + end + end + + after do + Buildr.options.debug = nil + ENV.delete "debug" + ENV.delete "DEBUG" end # Redirect the java error ouput, yielding so you can do something while it is # and returning the content of the error buffer. # def redirect_java_err - pending "RJB doesn't support well instantiating a class that has several constructors" unless RUBY_PLATFORM =~ /java/ err = Java.java.io.ByteArrayOutputStream.new original_err = Java.java.lang.System.err begin @@ -75,7 +276,7 @@ describe Buildr::Compiler::Ecj do compile.options.source = "1.5" compile.options.target = "1.5" } - redirect_java_err { foo.compile.invoke }.should_not match(/WARNING/) + redirect_java_err { foo.compile.invoke }.should_not match(/warning/) end it "should issue warnings for type casting when warnings are set" do @@ -85,21 +286,17 @@ describe Buildr::Compiler::Ecj do compile.options.target = "1.5" compile.options.warnings = true } - redirect_java_err { foo.compile.invoke }.should match(/WARNING/) - end - - after(:all) do - #Make ecj appear as a compiler that doesn't apply: - module Buildr - module Compiler - class Ecj - class << self - def applies_to?(project, task) - false - end - end - end - end + redirect_java_err { foo.compile.invoke }.should match(/warning/) + end + + it 'should pick Ecj version from ecj build settings' do + begin + Buildr::Compiler::Ecj.instance_eval { @dependencies = nil } + write 'build.yaml', 'ecj: 3.5.1' + Buildr::Compiler::Ecj.dependencies.should include("org.eclipse.jdt.core.compiler:ecj:jar:3.5.1") + ensure + Buildr::Compiler::Ecj.instance_eval { @dependencies = nil } end end + end http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/tests/.gitignore ---------------------------------------------------------------------- diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..f2a4093 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +**/target \ No newline at end of file http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/tests/compile_with_parent/Buildfile ---------------------------------------------------------------------- diff --git a/tests/compile_with_parent/Buildfile b/tests/compile_with_parent/Buildfile index db3814f..d1646d0 100644 --- a/tests/compile_with_parent/Buildfile +++ b/tests/compile_with_parent/Buildfile @@ -15,4 +15,4 @@ define "parent" do end end - \ No newline at end of file + http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/tests/compile_with_parent/lib/.gitkeep ---------------------------------------------------------------------- diff --git a/tests/compile_with_parent/lib/.gitkeep b/tests/compile_with_parent/lib/.gitkeep new file mode 100644 index 0000000..e69de29 http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/tests/helloWorldEcj/Buildfile ---------------------------------------------------------------------- diff --git a/tests/helloWorldEcj/Buildfile b/tests/helloWorldEcj/Buildfile new file mode 100644 index 0000000..ccce6c3 --- /dev/null +++ b/tests/helloWorldEcj/Buildfile @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with this +# work for additional information regarding copyright ownership. The ASF +# licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. + +define "helloWorld", :version => "1.0", :group => "org.buildr" do + + compile.using(:ecj) do + p Java.classpath + end + package(:jar) +end http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/tests/helloWorldEcj/buildr.yml ---------------------------------------------------------------------- diff --git a/tests/helloWorldEcj/buildr.yml b/tests/helloWorldEcj/buildr.yml new file mode 100644 index 0000000..5a9e574 --- /dev/null +++ b/tests/helloWorldEcj/buildr.yml @@ -0,0 +1 @@ +ecj: 3.5.1 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/tests/helloWorldEcj/src/main/java/HelloWorld.java ---------------------------------------------------------------------- diff --git a/tests/helloWorldEcj/src/main/java/HelloWorld.java b/tests/helloWorldEcj/src/main/java/HelloWorld.java new file mode 100644 index 0000000..eb25a00 --- /dev/null +++ b/tests/helloWorldEcj/src/main/java/HelloWorld.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +public class HelloWorld { + + public void foo() { + System.err.println("bar"); + } +} http://git-wip-us.apache.org/repos/asf/buildr/blob/7f5b25e7/tests/integration_testing.rb ---------------------------------------------------------------------- diff --git a/tests/integration_testing.rb b/tests/integration_testing.rb index 4d1139f..d4f92ca 100644 --- a/tests/integration_testing.rb +++ b/tests/integration_testing.rb @@ -17,7 +17,7 @@ TEST_DIR = File.dirname(File.expand_path(__FILE__)) BUILDR = ENV['BUILDR'] || File.expand_path("#{TEST_DIR}/../_buildr") require 'test/unit' -require 'zip/zip' +require 'zip' module Buildr module IntegrationTests @@ -30,6 +30,7 @@ module Buildr def test_#{folder.sub("-", "")} begin result = `cd #{TEST_DIR}/#{folder} ; #{BUILDR} #{cmd}` +p result assert($?.success?, 'Command success?') #{ after_block || "" } ensure @@ -50,6 +51,11 @@ module Buildr test "helloWorld", "package" + test "helloWorldEcj", "package", <<-CHECK +p result +#assert(::Buildr::Java.classpath.include?(artifact("org.eclipse.jdt.core.compiler:ecj:jar:3.5.1").to_s)) + CHECK + test "compile_with_parent", "compile" test "junit3", "test" @@ -57,7 +63,7 @@ module Buildr test "include_path", "package", <<-CHECK path = File.expand_path("#{TEST_DIR}/include_path/target/proj-1.0.zip") assert(File.exist?(path), "File exists?") -Zip::File.open(path) {|zip| +::Zip::File.open(path) {|zip| assert(!zip.get_entry("distrib/doc/index.html").nil?) assert(!zip.get_entry("distrib/lib/slf4j-api-1.6.1.jar").nil?) } @@ -66,7 +72,7 @@ assert(!zip.get_entry("distrib/lib/slf4j-api-1.6.1.jar").nil?) test "include_as", "package", <<-CHECK path = File.expand_path("#{TEST_DIR}/include_as/target/proj-1.0.zip") assert(File.exist? path) -Zip::File.open(path) {|zip| +::Zip::File.open(path) {|zip| assert(!zip.get_entry("docu/index.html").nil?) assert(!zip.get_entry("lib/logging.jar").nil?) }
