1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
require 'jars/maven_exec'
require 'jars/lock'
require 'fileutils'
module Jars
class Classpath
# convenient method
def self.require( scope = nil )
new.require( scope )
end
# convenient method
def self.classpath( scope = nil )
new.classpath( scope )
end
# convenient method
def self.classpath_string( scope = nil )
new.classpath_string( scope )
end
def initialize( spec = nil, deps = nil )
@spec = spec
@deps = deps
end
def mvn
@mvn ||= MavenExec.new( @spec )
end
def workdir( dirname )
dir = File.join( mvn.basedir, dirname )
dir if File.directory?( dir )
end
def dependencies_list
if @deps.nil?
deps = Jars.lock_path( mvn.basedir )
@deps = deps if deps && File.exist?( deps )
end
if @deps
@deps
else
resolve_dependencies
end
end
private :dependencies_list
DEPENDENCY_LIST = 'dependencies.list'
def resolve_dependencies
basedir = workdir( 'pkg' ) || workdir( 'target' ) || workdir( '' )
deps = File.join( basedir, DEPENDENCY_LIST )
mvn.resolve_dependencies_list( deps )
deps
end
private :resolve_dependencies
def require( scope = nil )
process( scope ) do |jar|
if jar.scope == :system
Kernel.require jar.path
else
require_jar( *jar.gacv )
end
end
if scope == nil || scope == :runtime
process( :provided ) do |jar|
Jars.mark_as_required( *jar.gacv )
end
end
end
def classpath( scope = nil )
classpath = []
process( scope ) do |jar|
classpath << jar.file
end
classpath
end
def process( scope, &block )
deps = dependencies_list
Lock.new( deps ).process( scope, &block )
ensure
# just delete the temporary file if it exists
FileUtils.rm_f( DEPENDENCY_LIST )
end
private :process
def classpath_string( scope = nil )
classpath( scope ).join( File::PATH_SEPARATOR )
end
end
end
|