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

Constant Summary collapse

JUnit =
Buildr::JUnit
TestNG =
Buildr::TestNG

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.javadoc 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'


83
84
85
# File 'lib/buildr/java/rjb.rb', line 83

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.javadoc 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.javadoc 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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/buildr/java/rjb.rb', line 91

def load
  return self if @loaded
  unless RUBY_PLATFORM =~ /darwin/i
    home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not set.'
    tools = File.expand_path('lib/tools.jar', home)
    raise "I need tools.jar to compile, can't find it in #{home}/lib" unless File.exist?(tools)
    classpath << tools
  end
  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)


113
114
115
116
117
118
# File 'lib/buildr/java/rjb.rb', line 113

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

.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