Class: Ruby2Jar::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2jar/builder.rb

Overview

Ruby2Jar build JAR from Ruby script. It copy gems, compile sources and package JAR. It is a easy way to distribute your JRuby application or to create Applet or Java Web Start.

Examples

Build from Rake task (best production way)

require "ruby2jar"

PKG_NAME = "program"
PKG_VERSION = "0.1"

Ruby2Jar::Rake::JarTask.new do |jar|
  jar.files = FileList["lib/**/*", "bin/*"]
  jar.main = "bin/program"
  jar.name = PKG_NAME
  jar.version = PKG_VERSION
  jar.add_dependency "rspec"
end

Build with console output

require "ruby2jar"

builder = Ruby2Jar::Builder.new
console = Ruby2Jar::Console.new(builder)    # Add console error output
builder.path = "/path/to/ruby/program/"     # Set program to build
builder.files = Rake::FileList["/lib/**/*"] # Select files
builder.add_dependency "actionsupport"      # Add gems
builder.add_dependency "rspec", ">=1.0.0"   # Add gem with special version
builde.build                                # Start building

Extension

You can add some action in building work. For example, to show warning dialogs or to create special manifest file. Just add new Proc or Method object to “before_*” arrays with neccessary building step.

You can also extend Ruby2Jar::Listener to create extension, which automatically connect all function to builder.

Example

require "ruby2jar"

builder = Ruby2Jar::Builder.new
builder.path = "/path/to/ruby/program/"
builder.add_dependency "rspec"

builder.before_start  << lambda {        # Add function, which will be
  puts "Jar building is start"           # call before builder start and
}                                        # set system variables

builder.before_finish << lambda {        # Function, which will be call
  puts "Jar building is finish"          # before builder finish it work  
}                                        # and delete temporal files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Create builder instance



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruby2jar/builder.rb', line 124

def initialize
  @path = Dir.pwd
  @create_manifest = false
  @manifest = {"Manifest-Version" => "1.0"}
  @include_jruby = true
  @target = "1.6"
  @require_paths = ["lib", ""]
  @javac_args = ""
  @gems_index = Gem.source_index
  
  @before_start = []
  @before_find_gems = []
  @before_copy = []
  @before_create_init = []
  @before_compile = []
  @before_package = []
  @before_finish = []
  @on_error = []
  
  @gems = []
  @configs = {}
end

Instance Attribute Details

#before_compileObject

Returns the value of attribute before_compile.



185
186
187
# File 'lib/ruby2jar/builder.rb', line 185

def before_compile
  @before_compile
end

#before_copyObject

Returns the value of attribute before_copy.



183
184
185
# File 'lib/ruby2jar/builder.rb', line 183

def before_copy
  @before_copy
end

#before_create_initObject

Returns the value of attribute before_create_init.



184
185
186
# File 'lib/ruby2jar/builder.rb', line 184

def before_create_init
  @before_create_init
end

#before_finishObject

Returns the value of attribute before_finish.



187
188
189
# File 'lib/ruby2jar/builder.rb', line 187

def before_finish
  @before_finish
end

#before_packageObject

Returns the value of attribute before_package.



186
187
188
# File 'lib/ruby2jar/builder.rb', line 186

def before_package
  @before_package
end

#before_startObject

Returns the value of attribute before_start.



182
183
184
# File 'lib/ruby2jar/builder.rb', line 182

def before_start
  @before_start
end

#build_dirObject

Path to work dir, which be used to copy all neccessary files. It will be set automatically and has public access only for extensions.



192
193
194
# File 'lib/ruby2jar/builder.rb', line 192

def build_dir
  @build_dir
end

#configsObject

Gem’s configs with building information, which was loaded from GEM_DIR/java.yaml. It will has information after copy step.



199
200
201
# File 'lib/ruby2jar/builder.rb', line 199

def configs
  @configs
end

#create_manifestObject

Does builder must create Manifest file in JAR with main script, which will be start on JAR executing (default if true).



98
99
100
# File 'lib/ruby2jar/builder.rb', line 98

def create_manifest
  @create_manifest
end

#filesObject

List of files to be included in the JAR.



90
91
92
# File 'lib/ruby2jar/builder.rb', line 90

def files
  @files
end

#gemsObject

Gems, which using in program. Use add_dependency to add gem.



195
196
197
# File 'lib/ruby2jar/builder.rb', line 195

def gems
  @gems
end

#gems_indexObject

Gem’s source index. It available as variable for testing and hacking.



205
206
207
# File 'lib/ruby2jar/builder.rb', line 205

def gems_index
  @gems_index
end

#include_jrubyObject

Does builder must copy JRuby to JAR (default if true). With JRuby resulting JAR can be started on any Java Runtime, but file will be has bigger size.



110
111
112
# File 'lib/ruby2jar/builder.rb', line 110

def include_jruby
  @include_jruby
end

#init_require_pathsObject

Paths to add to $LOAD_PATH in init script



202
203
204
# File 'lib/ruby2jar/builder.rb', line 202

def init_require_paths
  @init_require_paths
end

#jarObject

Path to result JAR (default is path/pkg/dirname in path.jar).



105
106
107
# File 'lib/ruby2jar/builder.rb', line 105

def jar
  @jar
end

#javac_argsObject

Arguments for Java compiler



117
118
119
# File 'lib/ruby2jar/builder.rb', line 117

def javac_args
  @javac_args
end

#jrubyObject

Path to JRuby JAR (default is $JRUBY_HOME/lib/jruby/jar). Used only if include_jruby is true.



114
115
116
# File 'lib/ruby2jar/builder.rb', line 114

def jruby
  @jruby
end

#loaded_gemsObject

Gems, which is added to JAR. Contain @gems and they dependencies.



208
209
210
# File 'lib/ruby2jar/builder.rb', line 208

def loaded_gems
  @loaded_gems
end

#mainObject

Path to main script, which will be run whtn you execute JAR. If in path you set file, main script will be equal path.



94
95
96
# File 'lib/ruby2jar/builder.rb', line 94

def main
  @main
end

#manifestObject

Custom content of Manifest file in JAR as hash. If parameters will be not set, builder add Manifest-Version and Main-Class if main exist.



102
103
104
# File 'lib/ruby2jar/builder.rb', line 102

def manifest
  @manifest
end

#on_errorObject

Returns the value of attribute on_error.



188
189
190
# File 'lib/ruby2jar/builder.rb', line 188

def on_error
  @on_error
end

#pathObject

Script or directory to build JAR



87
88
89
# File 'lib/ruby2jar/builder.rb', line 87

def path
  @path
end

#require_pathsObject

Path in script, which will be added to $LOAD_PATH in init script (default is root and lib dir of application)



121
122
123
# File 'lib/ruby2jar/builder.rb', line 121

def require_paths
  @require_paths
end

Instance Method Details

#add_dependency(gem, *requirements) ⇒ Object

Add a dependency gem



148
149
150
# File 'lib/ruby2jar/builder.rb', line 148

def add_dependency(gem, *requirements)
  @gems << [gem, requirements]
end

#buildObject

Start building JAR. You must set path before call it.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby2jar/builder.rb', line 153

def build
  @stop = false
  %w{start copy create_init compile package}.each do |step|
    method("before_#{step}").call.each do |proc|
      proc.call
    end
    if @stop
      break
    end
    method(step).call
  end
rescue => error
  @error_rescued = false
  on_error.each do |proc|
    @error_rescued |= proc.call error
  end
  if not @error_rescued
    raise error
  end
ensure
  begin
    before_finish.each do |proc|
      proc.call
    end
  ensure
    finish
  end
end

#stopObject

Stop JAR building and delete temporal files. Must be runned from extension on error or when building is not necessary yet.



212
213
214
# File 'lib/ruby2jar/builder.rb', line 212

def stop
  @stop = true
end