You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by la...@apache.org on 2008/09/13 14:59:50 UTC

svn commit: r694937 - in /incubator/buildr/trunk: lib/buildr/java.rb lib/buildr/java/java.rb lib/buildr/java/rjb.rb spec/java_spec.rb

Author: lacton
Date: Sat Sep 13 05:59:50 2008
New Revision: 694937

URL: http://svn.apache.org/viewvc?rev=694937&view=rev
Log:
Added tools_jar method to Java module, to reduce code duplication.  Introduced a new java.rb file to factor common code between jruby.rb and rjb.rb.

Added:
    incubator/buildr/trunk/lib/buildr/java/java.rb
Modified:
    incubator/buildr/trunk/lib/buildr/java.rb
    incubator/buildr/trunk/lib/buildr/java/rjb.rb
    incubator/buildr/trunk/spec/java_spec.rb

Modified: incubator/buildr/trunk/lib/buildr/java.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java.rb?rev=694937&r1=694936&r2=694937&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java.rb Sat Sep 13 05:59:50 2008
@@ -14,6 +14,7 @@
 # the License.
 
 
+require 'buildr/java/java'
 require RUBY_PLATFORM == 'java' ? 'buildr/java/jruby' : 'buildr/java/rjb'
 require 'buildr/java/compilers'
 require 'buildr/java/test_frameworks'

Added: incubator/buildr/trunk/lib/buildr/java/java.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/java.rb?rev=694937&view=auto
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/java.rb (added)
+++ incubator/buildr/trunk/lib/buildr/java/java.rb Sat Sep 13 05:59:50 2008
@@ -0,0 +1,64 @@
+# 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.
+
+
+# Buildr runs along side a JVM, using either RJB or JRuby.  The Java module allows
+# you to access Java classes and create Java objects.
+#
+# Java classes are accessed as static methods on the Java module, for example:
+#   str = Java.java.lang.String.new('hai!')
+#   str.toUpperCase
+#   => 'HAI!'
+#   Java.java.lang.String.isInstance(str)
+#   => true
+#   Java.com.sun.tools.javac.Main.compile(args)
+#
+# The classpath attribute allows Buildr to add JARs and directories to the classpath,
+# for example, we use it to load Ant and various Ant tasks, code generators, test
+# frameworks, and so forth.
+#
+# When using an artifact specification, Buildr will automatically download and
+# install the artifact before adding it to the classpath.
+#
+# For example, Ant is loaded as follows:
+#   Java.classpath << 'org.apache.ant:ant:jar:1.7.0'
+#
+# Artifacts can only be downloaded after the Buildfile has loaded, giving it
+# a chance to specify which remote repositories to use, so adding to classpath
+# does not by itself load any libraries.  You must call Java.load before accessing
+# any Java classes to give Buildr a chance to load the libraries specified in the
+# classpath.
+#
+# When building an extension, make sure to follow these rules:
+# 1. Add to the classpath when the extension is loaded (i.e. in module or class
+#    definition), so the first call to Java.load anywhere in the code will include
+#    the libraries you specify.
+# 2. Call Java.load once before accessing any Java classes, allowing Buildr to
+#    set up the classpath.
+# 3. Only call Java.load when invoked, otherwise you may end up loading the JVM
+#    with a partial classpath, or before all remote repositories are listed.
+# 4. Check on a clean build with empty local repository.
+module Java
+  class << self
+    
+    # Most platforms requires tools.jar to be on the classpath, tools.jar contains the
+    # Java compiler (OS X and AIX are two exceptions we know about, may be more).
+    # Guess where tools.jar is from JAVA_HOME, which hopefully points to the JDK,
+    # but maybe the JRE.  Return nil if not found.
+    def tools_jar
+      [File.expand_path('lib/tools.jar', ENV['JAVA_HOME']), File.expand_path('../lib/tools.jar', ENV['JAVA_HOME'])].find { |path| File.exist?(path) }
+    end
+  end
+end

Modified: incubator/buildr/trunk/lib/buildr/java/rjb.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/rjb.rb?rev=694937&r1=694936&r2=694937&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/rjb.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java/rjb.rb Sat Sep 13 05:59:50 2008
@@ -97,14 +97,8 @@
     # that append to the classpath and specify which remote repositories to use.
     def load
       return self if @loaded
-      # Most platforms requires tools.jar to be on the classpath, tools.jar contains the
-      # Java compiler (OS X and AIX are two exceptions we know about, may be more).
-      # Guess where tools.jar is from JAVA_HOME, which hopefully points to the JDK,
-      # but maybe the JRE.
       ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not set.'
-      tools = [File.expand_path('lib/tools.jar', ENV['JAVA_HOME']), File.expand_path('../lib/tools.jar', ENV['JAVA_HOME'])].
-        find { |path| File.exist?(path) }
-      classpath << tools if tools
+      classpath << Java.tools_jar if Java.tools_jar
       
       cp = Buildr.artifacts(classpath).map(&:to_s).each { |path| file(path).invoke }
       java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split

Modified: incubator/buildr/trunk/spec/java_spec.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/java_spec.rb?rev=694937&r1=694936&r2=694937&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/java_spec.rb (original)
+++ incubator/buildr/trunk/spec/java_spec.rb Sat Sep 13 05:59:50 2008
@@ -37,4 +37,32 @@
       ENV['JAVA_HOME'] = @old_home
     end
   end
+end
+
+
+describe Java, 'tools_jar' do
+  before do
+    @old_home = ENV['JAVA_HOME']
+  end
+  
+  it 'should return $JAVA_HOME/lib/tools.jar if JAVA_HOME points to JDK' do
+    write 'jdk/lib/tools.jar'
+    ENV['JAVA_HOME'] = File.expand_path('jdk')
+    Java.tools_jar.should point_to_path('jdk/lib/tools.jar')
+  end
+  
+  it 'should return $JAVA_HOME/../lib/tools.jar if JAVA_HOME points to a JRE inside a JDK' do
+    write 'jdk/lib/tools.jar'
+    ENV['JAVA_HOME'] = File.expand_path('jdk/jre')
+    Java.tools_jar.should point_to_path('jdk/lib/tools.jar')
+  end
+  
+  it 'should return nil if tools.jar not found' do
+    ENV['JAVA_HOME'] = File.expand_path('jdk')
+    Java.tools_jar.should be(nil)
+  end
+  
+  after do
+    ENV['JAVA_HOME'] = @old_home
+  end
 end
\ No newline at end of file