You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by to...@apache.org on 2010/06/28 19:05:48 UTC

svn commit: r958641 - in /buildr/trunk: CHANGELOG lib/buildr/java.rb lib/buildr/java/external.rb spec/java/external_spec.rb

Author: toulmean
Date: Mon Jun 28 17:05:47 2010
New Revision: 958641

URL: http://svn.apache.org/viewvc?rev=958641&view=rev
Log:
BUILDR-420 Support external compiler

Added:
    buildr/trunk/lib/buildr/java/external.rb
    buildr/trunk/spec/java/external_spec.rb
Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/lib/buildr/java.rb

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=958641&r1=958640&r2=958641&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Mon Jun 28 17:05:47 2010
@@ -1,5 +1,6 @@
 1.4.1 (pending)
-* Changed: BUILDR-459 Update gemspec to accept json_pure ~> 1.4.3
+* Added:  BUILDR-420 Support external compiler
+* Change: BUILDR-459 Update gemspec to accept json_pure ~> 1.4.3
 
 
 1.4.0 (2010-06-18)

Modified: buildr/trunk/lib/buildr/java.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/java.rb?rev=958641&r1=958640&r2=958641&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/java.rb (original)
+++ buildr/trunk/lib/buildr/java.rb Mon Jun 28 17:05:47 2010
@@ -16,6 +16,7 @@
 
 require RUBY_PLATFORM == 'java' ? 'buildr/java/jruby' : 'buildr/java/rjb'
 require 'buildr/java/compiler'
+require 'buildr/java/external'
 require 'buildr/java/tests'
 require 'buildr/java/bdd'
 require 'buildr/java/packaging'

Added: buildr/trunk/lib/buildr/java/external.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/java/external.rb?rev=958641&view=auto
==============================================================================
--- buildr/trunk/lib/buildr/java/external.rb (added)
+++ buildr/trunk/lib/buildr/java/external.rb Mon Jun 28 17:05:47 2010
@@ -0,0 +1,71 @@
+# 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.
+
+module Buildr
+  module Compiler
+    class ExternalJavac< Buildr::Compiler::Javac
+      
+      OPTIONS = [:jvm, :warnings, :debug, :deprecation, :source, :target, :lint, :other]
+    
+      specify :language=>:java, :sources => 'java', :source_ext => 'java',
+              :target=>'classes', :target_ext=>'class', :packaging=>:jar
+              
+              
+      def compile(sources, target, dependencies) #:nodoc:
+        check_options options, OPTIONS
+        cmd_args = []
+        cmd_args << '-classpath' << dependencies.join(File::PATH_SEPARATOR) unless dependencies.empty?
+        source_paths = sources.select { |source| File.directory?(source) }
+        cmd_args << '-sourcepath' << source_paths.join(File::PATH_SEPARATOR) unless source_paths.empty?
+        cmd_args << '-d' << File.expand_path(target)
+        cmd_args += externaljavac_args
+        Tempfile.open("external") {|tmp|
+          tmp.write files_from_sources(sources).join(' ')
+          cmd_args << "@#{tmp.path}"
+        }
+        unless Buildr.application.options.dryrun
+          javac_path = File.join(options[:jvm] || ENV['JAVA_HOME'], "bin", "javac")
+          final_args = cmd_args.insert(0, javac_path).push('2>&1').join(' ')
+          trace(final_args)
+          info %x[#{final_args}]
+          fail 'Failed to compile, see errors above' unless $?.success?
+        end
+      end
+
+      private
+      
+      # See arg list here: http://publib.boulder.ibm.com/infocenter/rsahelp/v7r0m0/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm
+      def externaljavac_args #:nodoc:
+        args = []  
+        args << '-nowarn' unless options[:warnings]
+        args << '-verbose' if Buildr.application.options.trace
+        args << '-g' if options[:debug]
+        args << '-deprecation' if options[:deprecation]
+        args << '-source' << options[:source].to_s if options[:source]
+        args << '-target' << options[:target].to_s if options[:target]
+        case options[:lint]
+        when Array  then args << "-Xlint:#{options[:lint].join(',')}"
+        when String then args << "-Xlint:#{options[:lint]}"
+        when true   then args << '-Xlint'
+        end
+        args + Array(options[:other])
+      end
+      
+    end
+    
+  end
+end
+
+Buildr::Compiler.compilers << Buildr::Compiler::ExternalJavac
\ No newline at end of file

Added: buildr/trunk/spec/java/external_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/java/external_spec.rb?rev=958641&view=auto
==============================================================================
--- buildr/trunk/spec/java/external_spec.rb (added)
+++ buildr/trunk/spec/java/external_spec.rb Mon Jun 28 17:05:47 2010
@@ -0,0 +1,50 @@
+# 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.
+
+
+require File.join(File.dirname(__FILE__), '../spec_helpers')
+
+describe Buildr::Compiler::ExternalJavac do
+  
+  before :all do
+    Buildr::Compiler.compilers.delete Buildr::Compiler::Javac
+  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.join(File.dirname(__FILE__), '../spec_helpers')\n")).post_match
+    javac_spec.gsub!("javac", "externaljavac")
+    eval(javac_spec)
+  end
+  
+  it "should accept a :jvm option as JAVA_HOME" do
+    write 'src/main/java/Foo.java', 'public class Foo {}'
+    define "foo" do
+      compile.using(:externaljavac).options.jvm = "somepath"
+    end
+    begin
+      trace true #We set it true to grab the trace statement with the jvm path in it!
+      lambda {lambda {project("foo").compile.invoke}.should raise_error(RuntimeError, /Failed to compile, see errors above/)}.should show(/somepath\/bin\/javac .*/)
+    end
+    trace false
+  end
+  
+  after :all do
+    Buildr::Compiler.compilers << Buildr::Compiler::Javac
+  end
+  
+end
+
+