You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by as...@apache.org on 2009/03/13 08:23:48 UTC

svn commit: r753145 - in /buildr/trunk: CHANGELOG doc/languages.textile lib/buildr/scala/compiler.rb spec/scala/compiler_spec.rb

Author: assaf
Date: Fri Mar 13 07:23:48 2009
New Revision: 753145

URL: http://svn.apache.org/viewvc?rev=753145&view=rev
Log:
Added: BUILDR-136 Support Scala/Java Joint Compiler (Daniel Spiewak).

Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/doc/languages.textile
    buildr/trunk/lib/buildr/scala/compiler.rb
    buildr/trunk/spec/scala/compiler_spec.rb

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=753145&r1=753144&r2=753145&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Fri Mar 13 07:23:48 2009
@@ -1,6 +1,7 @@
 1.3.4 (Pending)
 * Added:  BUILDR-93 Add specs for ScalaCheck integration
 * Added:  BUILDR-94 Add specs for Scala Specs integration
+* Added:  BUILDR-136 Support Scala/Java Joint Compiler (Daniel Spiewak).
 * Added:  BUILDR-159 Improved 'check' to accept both tar and tgz archives.
 * Added:  BUILDR-164 New 'artifacts:sources' task to download source code
           for artifact jars.

Modified: buildr/trunk/doc/languages.textile
URL: http://svn.apache.org/viewvc/buildr/trunk/doc/languages.textile?rev=753145&r1=753144&r2=753145&view=diff
==============================================================================
--- buildr/trunk/doc/languages.textile (original)
+++ buildr/trunk/doc/languages.textile Fri Mar 13 07:23:48 2009
@@ -148,7 +148,7 @@
 
 <notextile>
 {% highlight sh %}
-> set SCALA_HOME=C:\Path\To\Scala-2.7.1
+> set SCALA_HOME=C:\Path\To\Scala-2.7.3
 {% endhighlight %}
 </notextile>
 
@@ -156,11 +156,11 @@
 
 <notextile>
 {% highlight sh %}
-> export SCALA_HOME=/path/to/scala-2.7.1
+> export SCALA_HOME=/path/to/scala-2.7.3
 {% endhighlight %}
 </notextile>
 
-The @SCALA_HOME@ base directory should be such that Scala core libraries are located directly under the "lib" subdirectory, and Scala scripts are under the "bin" directory.
+The @SCALA_HOME@ base directory should be such that Scala core libraries are located directly under the "lib" subdirectory, and Scala scripts are under the "bin" directory. This step is not necessary if you installed Scala using MacPorts (OS X).
 
 You must also require the Scala compiler in your buildfile:
 
@@ -174,7 +174,11 @@
 
 The Scala compiler looks for source files in the project's @src/main/scala@  directory, and defaults to compiling them into the @target/classes@ directory.  It looks for test cases in the project's @src/test/scala@ and defaults to  compile them into the @target/test/classes@ directory.
 
-If you point the @compile@ task at any other source directory, it will use the  Scala compiler if any of these directories contains files with the extension @.scala@.
+Any Java source files found in the @src/main/java@ directory will be compiled using the Scala/Java joint compiler into the @target/classes@ directory.  Both the Java and the Scala sources are compiled with an inclusive classpath, meaning that you may have a Java class which depends upon a Scala class which depends upon a Java class, all within the same project.  The Java sources will be compiled with the same dependencies as the Scala sources with the addition of the @scala-library.jar@ file as required for Scala interop.
+
+Note that you cannot use the Groovy *and* the Scala joint compilers in the same project.  If both are required, the Groovy joint compiler will take precedence.
++
+If you point the @compile@ task at any other source directory, it will use the  Scala compiler if any of these directories contains files with the extension @.scala@.  The joint compilation of Java sources may only be pointed at an alternative directory using the feature to redefine the @_(:src, :main, :java)@ path.
 
 When using the Scala compiler, if you don't specify the packaging type, it defaults to JAR.
 
@@ -187,6 +191,7 @@
 | @:other@        | Array of options passed to the compiler (e.g. @:other=>'-Xprint-types'@). |
 | @:target@       | Bytecode compatibility (e.g. '1.4'). |
 | @:warnings@     | Issue warnings when compiling.  True when running in verbose mode. |
+| @:javac@        | A hash of options passed to the @javac@ compiler verbatim. |
 
 h4. Fast Scala Compiler
 

Modified: buildr/trunk/lib/buildr/scala/compiler.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/scala/compiler.rb?rev=753145&r1=753144&r2=753145&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/scala/compiler.rb (original)
+++ buildr/trunk/lib/buildr/scala/compiler.rb Fri Mar 13 07:23:48 2009
@@ -18,7 +18,6 @@
 require 'buildr/core/compile'
 require 'buildr/packaging'
 
-
 module Buildr::Scala
 
   class << self
@@ -60,12 +59,24 @@
       def use_fsc
         ENV["USE_FSC"] =~ /^(yes|on|true)$/i
       end
+      
+      def applies_to?(project, task) #:nodoc:
+        paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) }
+        paths.flatten!
+        
+        # Just select if we find .scala files
+        paths.any? { |path| !Dir["#{path}/**/*.scala"].empty? }
+      end
     end
+    
+    Javac = Buildr::Compiler::Javac
 
-    OPTIONS = [:warnings, :deprecation, :optimise, :target, :debug, :other]
+    OPTIONS = [:warnings, :deprecation, :optimise, :target, :debug, :other, :javac]
+    
     Java.classpath << dependencies
 
-    specify :language=>:scala, :target=>'classes', :target_ext=>'class', :packaging=>:jar
+    specify :language=>:scala, :sources => [:scala, :java], :source_ext => [:scala, :java],
+            :target=>'classes', :target_ext=>'class', :packaging=>:jar
 
     def initialize(project, options) #:nodoc:
       super
@@ -73,6 +84,9 @@
       options[:warnings] = verbose if options[:warnings].nil?
       options[:deprecation] ||= false
       options[:optimise] ||= false
+      options[:javac] ||= {}
+      
+      @java = Javac.new(project, options[:javac])
     end
 
     def compile(sources, target, dependencies) #:nodoc:
@@ -97,10 +111,23 @@
           Java.scala.tools.nsc.Main.process(cmd_args.to_java(Java.java.lang.String))
           fail 'Failed to compile, see errors above' if Java.scala.tools.nsc.Main.reporter.hasErrors
         end
+        
+        if java_applies? sources
+          trace 'Compiling mixed Java/Scala sources'
+          
+          deps = dependencies + [ File.expand_path('lib/scala-library.jar', Scalac.scala_home),
+                                  File.expand_path(target) ]
+          @java.compile(sources, target, deps)
+        end
       end
     end
 
   private
+  
+    def java_applies?(sources)
+      not sources.flatten.map { |source| File.directory?(source) ? FileList["#{source}/**/*.java"] : source }.
+        flatten.reject { |file| File.directory?(file) }.map { |file| File.expand_path(file) }.uniq.empty?
+    end
 
     # Returns Scalac command line arguments from the set of options.
     def scalac_args #:nodoc:
@@ -120,4 +147,4 @@
 
 # Scala compiler comes first, ahead of Javac, this allows it to pick
 # projects that mix Scala and Java code by spotting Scala code first.
-Buildr::Compiler.compilers << Buildr::Scala::Scalac
+Buildr::Compiler.compilers.unshift Buildr::Scala::Scalac

Modified: buildr/trunk/spec/scala/compiler_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/scala/compiler_spec.rb?rev=753145&r1=753144&r2=753145&view=diff
==============================================================================
--- buildr/trunk/spec/scala/compiler_spec.rb (original)
+++ buildr/trunk/spec/scala/compiler_spec.rb Fri Mar 13 07:23:48 2009
@@ -80,6 +80,33 @@
     end
   end
 
+  it 'should compile scala class depending on java class in same project' do
+    write 'src/main/java/com/example/Foo.java', 'package com.example; public class Foo {}'
+    write 'src/main/scala/com/example/Bar.scala', 'package com.example; class Bar extends Foo'
+    define 'test1', :version=>'1.0' do
+      package(:jar)
+    end
+    task('test1:package').invoke
+    file('target/test1-1.0.jar').should exist
+    Zip::ZipFile.open(project('test1').package(:jar).to_s) do |zip|
+      zip.file.exist?('com/example/Foo.class').should be_true
+      zip.file.exist?('com/example/Bar.class').should be_true
+    end
+  end
+
+  it 'should compile java class depending on scala class in same project' do
+    write 'src/main/scala/com/example/Foo.scala', 'package com.example; class Foo'
+    write 'src/main/java/com/example/Bar.java',  'package com.example; public class Bar extends Foo {}'
+    define 'test1', :version=>'1.0' do
+      package(:jar)
+    end
+    task('test1:package').invoke
+    file('target/test1-1.0.jar').should exist
+    Zip::ZipFile.open(project('test1').package(:jar).to_s) do |zip|
+      zip.file.exist?('com/example/Foo.class').should be_true
+      zip.file.exist?('com/example/Bar.class').should be_true
+    end
+  end
 end