Class: Ruby2Jar::Builder
- Inherits:
-
Object
- Object
- Ruby2Jar::Builder
- 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
-
#before_compile ⇒ Object
Returns the value of attribute before_compile.
-
#before_copy ⇒ Object
Returns the value of attribute before_copy.
-
#before_create_init ⇒ Object
Returns the value of attribute before_create_init.
-
#before_finish ⇒ Object
Returns the value of attribute before_finish.
-
#before_package ⇒ Object
Returns the value of attribute before_package.
-
#before_start ⇒ Object
Returns the value of attribute before_start.
-
#build_dir ⇒ Object
Path to work dir, which be used to copy all neccessary files.
-
#configs ⇒ Object
Gem’s configs with building information, which was loaded from
GEM_DIR/java.yaml
. -
#create_manifest ⇒ Object
Does builder must create Manifest file in JAR with
main
script, which will be start on JAR executing (default if true). -
#files ⇒ Object
List of files to be included in the JAR.
-
#gems ⇒ Object
Gems, which using in program.
-
#gems_index ⇒ Object
Gem’s source index.
-
#include_jruby ⇒ Object
Does builder must copy JRuby to JAR (default if true).
-
#init_require_paths ⇒ Object
Paths to add to
$LOAD_PATH
in init script. -
#jar ⇒ Object
Path to result JAR (default is
path
/pkg/dirname inpath
.jar). -
#javac_args ⇒ Object
Arguments for Java compiler.
-
#jruby ⇒ Object
Path to JRuby JAR (default is $JRUBY_HOME/lib/jruby/jar).
-
#loaded_gems ⇒ Object
Gems, which is added to JAR.
-
#main ⇒ Object
Path to main script, which will be run whtn you execute JAR.
-
#manifest ⇒ Object
Custom content of Manifest file in JAR as hash.
-
#on_error ⇒ Object
Returns the value of attribute on_error.
-
#path ⇒ Object
Script or directory to build JAR.
-
#require_paths ⇒ Object
Path in script, which will be added to
$LOAD_PATH
in init script (default is root and lib dir of application).
Instance Method Summary collapse
-
#add_dependency(gem, *requirements) ⇒ Object
Add a dependency gem.
-
#build ⇒ Object
Start building JAR.
-
#initialize ⇒ Builder
constructor
Create builder instance.
-
#stop ⇒ Object
Stop JAR building and delete temporal files.
Constructor Details
#initialize ⇒ Builder
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_compile ⇒ Object
Returns the value of attribute before_compile.
185 186 187 |
# File 'lib/ruby2jar/builder.rb', line 185 def before_compile @before_compile end |
#before_copy ⇒ Object
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_init ⇒ Object
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_finish ⇒ Object
Returns the value of attribute before_finish.
187 188 189 |
# File 'lib/ruby2jar/builder.rb', line 187 def before_finish @before_finish end |
#before_package ⇒ Object
Returns the value of attribute before_package.
186 187 188 |
# File 'lib/ruby2jar/builder.rb', line 186 def before_package @before_package end |
#before_start ⇒ Object
Returns the value of attribute before_start.
182 183 184 |
# File 'lib/ruby2jar/builder.rb', line 182 def before_start @before_start end |
#build_dir ⇒ Object
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 |
#configs ⇒ Object
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_manifest ⇒ Object
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 |
#files ⇒ Object
List of files to be included in the JAR.
90 91 92 |
# File 'lib/ruby2jar/builder.rb', line 90 def files @files end |
#gems ⇒ Object
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_index ⇒ Object
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_jruby ⇒ Object
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_paths ⇒ Object
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 |
#jar ⇒ Object
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_args ⇒ Object
Arguments for Java compiler
117 118 119 |
# File 'lib/ruby2jar/builder.rb', line 117 def javac_args @javac_args end |
#jruby ⇒ Object
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_gems ⇒ Object
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 |
#main ⇒ Object
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 |
#manifest ⇒ Object
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_error ⇒ Object
Returns the value of attribute on_error.
188 189 190 |
# File 'lib/ruby2jar/builder.rb', line 188 def on_error @on_error end |
#path ⇒ Object
Script or directory to build JAR
87 88 89 |
# File 'lib/ruby2jar/builder.rb', line 87 def path @path end |
#require_paths ⇒ Object
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 |
#build ⇒ Object
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 |
#stop ⇒ Object
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 |