Module: JavaClass::Classpath::Factory

Included in:
JavaClass, Dsl::Mixin
Defined in:
lib/javaclass/classpath/factory.rb

Overview

Factory methods to create different kind of classpaths.

Author

Peter Kofler

Constant Summary collapse

Classpath_types =
[EclipseClasspath, MavenClasspath, ConventionClasspath]

Instance Method Summary collapse

Instance Method Details

#classpath(path, cp = CompositeClasspath.new) ⇒ Object

Parse the given path variable path and return a chain of class path elements. The path variable is separated by : or ; depending on the platform. Adds the classpath to the optional cp element else creates a CompositeClasspath.



17
18
19
20
# File 'lib/javaclass/classpath/factory.rb', line 17

def classpath(path, cp=CompositeClasspath.new)
  path.split(File::PATH_SEPARATOR).each { |cpe| cp.add_file_name(cpe) } 
  cp
end

#environment_classpath(cp = CompositeClasspath.new) ⇒ Object

Parse and set the system classpath. Needs JAVA_HOME to be set. Uses additional environment CLASSPATH if set. Adds the classpath to the optional cp element else creates a CompositeClasspath.



24
25
26
# File 'lib/javaclass/classpath/factory.rb', line 24

def environment_classpath(cp=CompositeClasspath.new)
  full_classpath(ENV['JAVA_HOME'], ENV['CLASSPATH'], cp)
end

#full_classpath(javahome, path = nil, cp = CompositeClasspath.new) ⇒ Object

Parse the given path variable path and return a chain of class path elements together with javahome if any.



29
30
31
32
33
# File 'lib/javaclass/classpath/factory.rb', line 29

def full_classpath(javahome, path=nil, cp=CompositeClasspath.new)
  cp.add_element(JavaHomeClasspath.new(javahome)) if javahome
  cp = classpath(path, cp) if path
  cp
end

#root_folder(basepath, cp = CompositeClasspath.new) ⇒ Object

Create a classpath from a project root directory basepath by looking in the children folder for regular workspaces.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/javaclass/classpath/factory.rb', line 52

def root_folder(basepath, cp=CompositeClasspath.new)
  if FileTest.directory? basepath
  
    Dir.entries(basepath).each do |entry|
      next if entry == '.' || entry == '..'
      file = File.join(basepath, entry)
  
      Classpath_types.each do |classpath_type|
        if classpath_type.valid_location?(file)
          cp.add_element(classpath_type.new(file))
          break
        end
      end
    end
  
  end
  
  cp 
end

#workspace(basepath, cp = CompositeClasspath.new) ⇒ Object

Create a classpath from a workspace basepath which contains Eclipse or Maven projects.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/javaclass/classpath/factory.rb', line 38

def workspace(basepath, cp=CompositeClasspath.new)

  # check for a valid project in this basepath
  Classpath_types.each do |classpath_type|
    if classpath_type.valid_location?(basepath)
      cp.add_element(classpath_type.new(basepath))
      return cp
    end
  end

  root_folder(basepath, cp)
end