Module: Java

Defined in:
lib/buildr/java/rjb.rb,
lib/buildr/java/jruby.rb,
lib/buildr/java/commands.rb,
lib/buildr/java/deprecated.rb

Overview

Base module for all things Java.

Defined Under Namespace

Modules: Commands, Package Classes: JavaWrapper, Options

Class Method Summary collapse

Class Method Details

.apt(*args) ⇒ Object

Deprecated: Use Java::Commands.apt instead.



74
75
76
77
# File 'lib/buildr/java/deprecated.rb', line 74

def apt(*args)
  Buildr.application.deprecated 'Use Java::Commands.apt instead.'
  Commands.apt(*args)
end

.classpathObject

Returns the classpath, an array listing directories, JAR files and artifacts. Use when loading the extension to add any additional libraries used by that extension.

For example, Ant is loaded as follows:

Java.classpath << 'org.apache.ant:ant:jar:1.7.0'


87
88
89
# File 'lib/buildr/java/rjb.rb', line 87

def classpath
  @classpath ||= []
end

.homeObject

Deprecated: Use ENV instead



99
100
101
102
# File 'lib/buildr/java/deprecated.rb', line 99

def home
  Buildr.application.deprecated 'Use ENV[\'JAVA_HOME\'] instead.'
  ENV['JAVA_HOME']
end

.java(*args, &block) ⇒ Object

Deprecated: Use Java::Commands.java instead.



67
68
69
70
71
# File 'lib/buildr/java/deprecated.rb', line 67

def java(*args, &block)
  return send(:method_missing, :java) if args.empty?
  Buildr.application.deprecated 'Use Java::Commands.java instead.'
  Commands.java(*args, &block)
end

.javac(*args) ⇒ Object

Deprecated: Use Java::Commands.javac instead.



80
81
82
83
# File 'lib/buildr/java/deprecated.rb', line 80

def javac(*args)
  Buildr.application.deprecated 'Use Java::Commands.javac instead.'
  Commands.javac(*args)
end

.javadoc(*args) ⇒ Object

Deprecated: Use Java::Commands.javadoc instead.



86
87
88
89
# File 'lib/buildr/java/deprecated.rb', line 86

def javadoc(*args)
  Buildr.application.deprecated 'Use Java::Commands.javadoc instead.'
  Commands.javadoc(*args)
end

.loadObject

Loads the JVM and all the libraries listed on the classpath. Call this method before accessing any Java class, but only call it from methods used in the build, giving the Buildfile a chance to load all extensions that append to the classpath and specify which remote repositories to use.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/buildr/java/rjb.rb', line 107

def load
  return self if @loaded
  classpath << tools_jar if tools_jar
  
  classpath.map! { |path| Proc === path ? path.call : path }
  cp = Buildr.artifacts(classpath).map(&:to_s).each { |path| file(path).invoke }
  java_opts = (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split
  ::Rjb.load cp.join(File::PATH_SEPARATOR), java_opts

  props = ::Rjb.import('java.lang.System').getProperties
  enum = props.propertyNames 
  while enum.hasMoreElements
    name = enum.nextElement.toString
    ENV_JAVA[name] = props.getProperty(name)
  end
  @loaded = true
  self
end

.method_missing(sym, *args, &block) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


126
127
128
129
130
131
# File 'lib/buildr/java/rjb.rb', line 126

def method_missing(sym, *args, &block) #:nodoc:
  raise ArgumentError, 'No arguments expected' unless args.empty?
  name = sym.to_s
  return ::Rjb.import(name) if name =~ /^[[:upper:]]/
  __package__ name
end

.tools_jarObject

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.



95
96
97
98
99
100
101
# File 'lib/buildr/java/rjb.rb', line 95

def tools_jar #:nodoc:
  @tools_jar ||= begin
    home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not set.'
    ['lib/tools.jar', '../lib/tools.jar'].map { |path| File.expand_path(path, home) }.
      find { |path| File.exist?(path) }
  end
end

.versionObject

Deprecated: Use ENV_JAVA instead.



92
93
94
95
96
# File 'lib/buildr/java/deprecated.rb', line 92

def version
  Buildr.application.deprecated 'Use ENV_JAVA[\'java.version\'] instead.'
  Java.load
  ENV_JAVA['java.version']
end

.wrapperObject Also known as: rjb

Deprecated: In earlier versions, Java.wrapper served as a wrapper around RJB/JRuby. From this version forward, we apply with JRuby style for importing Java classes:

Java.java.lang.String.new('hai!')

You still need to call Java.load before using any Java code: it resolves, downloads and installs various dependencies that are required on the classpath before calling any Java code (e.g. Ant and its tasks).



110
111
112
113
114
115
116
117
118
# File 'lib/buildr/java/deprecated.rb', line 110

def wrapper
  Buildr.application.deprecated 'See documentation for new way to access Java code.'
  if block_given?
    Java.load
    yield JavaWrapper.instance
  else
    JavaWrapper.instance
  end
end