Module: MiniAether::Bootstrap

Included in:
MiniAether
Defined in:
lib/mini_aether/bootstrap.rb

Constant Summary collapse

System =
Java::JavaLang::System

Instance Method Summary collapse

Instance Method Details

#bootstrap!Object

Load the required jar files, downloading them if necessary.

Ignores any maven config regarding repositories and attempts a direct download from repo1.maven.org using Net::HTTP.



117
118
119
120
121
# File 'lib/mini_aether/bootstrap.rb', line 117

def bootstrap!
  dependencies.each do |dep|
    require ensure_dependency(dep)
  end
end

#dependenciesObject

Pre-resolved dependencies of mini_aether.



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
# File 'lib/mini_aether/bootstrap.rb', line 14

def dependencies
  spec = Spec.new do
    group 'org.sonatype.aether' do
      version '1.13.1' do
        jar 'aether-api'
        jar 'aether-connector-asynchttpclient'
        jar 'aether-connector-file'
        jar 'aether-impl'
        jar 'aether-spi'
        jar 'aether-util'
      end
    end

    jar 'com.ning:async-http-client:1.6.5'
    jar 'org.jboss.netty:netty:3.2.5.Final'
    jar 'org.slf4j:slf4j-api:1.6.2'

    begin
      Java::OrgSlf4jImpl::StaticLoggerBinder
    rescue NameError
      # use logback when no slf4j backend exists
      jar 'ch.qos.logback:logback-core:1.0.6'
      jar 'ch.qos.logback:logback-classic:1.0.6'
      # add dir to classpath since it contains logback.xml
      $CLASSPATH << File.expand_path(File.dirname(__FILE__))
    end

    group 'org.apache.maven' do
      version '3.0.4' do
        jar 'maven-aether-provider'
        jar 'maven-model'
        jar 'maven-model-builder'
        jar 'maven-repository-metadata'
      end
    end

    group 'org.codehaus.plexus' do
      jar 'plexus-interpolation:1.14'
      jar 'plexus-component-annotations:1.5.5'
      jar 'plexus-utils:2.0.6'
    end
  end

  spec.dependencies
end

#ensure_dependency(dep) ⇒ Object



106
107
108
109
110
111
# File 'lib/mini_aether/bootstrap.rb', line 106

def ensure_dependency(dep)
  path = jar_path(dep)
  local_file = File.join(local_repository_path, path)
  install(path, local_file) unless File.exists?(local_file)
  local_file
end

#install(path, file, repo = MAVEN_CENTRAL_REPO) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mini_aether/bootstrap.rb', line 123

def install(path, file, repo = MAVEN_CENTRAL_REPO)
  print "installing #{File.basename(path)}... "
  $stdout.flush

  remote_base = File.dirname(path) + '/' + File.basename(path, File.extname(path))
  local_dir = File.dirname(file)
  local_base = File.join(local_dir, File.basename(file, File.extname(file)))

  exts = [File.extname(path), '.pom', '.pom.sha1']
  exts.each do |ext|
    uri = URI("#{repo}/#{remote_base}#{ext}")
    local_file = local_base + ext

    Net::HTTP.start(uri.host, uri.port) do |http|
      request = Net::HTTP::Get.new uri.request_uri
      
      http.request request do |response|
        unless response.code == '200'
          raise "#{response.code} #{response.message}: #{uri}"
        end

        FileUtils.mkdir_p local_dir
        open local_file, 'w' do |io|
          response.read_body do |chunk|
            io.write chunk
          end
        end
      end
    end

    print "#{ext} "
    $stdout.flush
  end
  puts
end

#interpolate(str) ⇒ Object

Interpolate variables like ${user.home} and ${env.HOME} from system properties and environment variables respectively.



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
# File 'lib/mini_aether/bootstrap.rb', line 62

def interpolate(str)
  ret = ''

  s = StringScanner.new(str)
  pos = s.pos

  while s.scan_until(/\$\{[^\s}]+\}/) # match ${stuff}
    # add the pre_match, but only starting from previous position
    ret << str.slice(pos, (s.pos - pos - s.matched.size))

    # interpolate
    var = s.matched.slice(2..-2)
    ret << case var
           when /^env\.(.*)/
             ENV[$1] || ''
           else
             System.getProperty(var) || ''
           end

    pos = s.pos
  end
  ret << s.rest
  
  ret
end

#jar_path(dep) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/mini_aether/bootstrap.rb', line 159

def jar_path(dep)
  group_id = dep[:group_id]
  group_path = group_id.gsub('.', '/')
  artifact_id = dep[:artifact_id]
  version = dep[:version]

  file_name = "#{artifact_id}-#{version}.jar"
  "#{group_path}/#{artifact_id}/#{version}/#{file_name}"
end

#local_repository_pathObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mini_aether/bootstrap.rb', line 88

def local_repository_path
  default_local_repo_path =
    File.join(System.getProperty('user.home'), '.m2', 'repository')

  if File.exists? M2_SETTINGS
    xml = File.read M2_SETTINGS
    begin
      parser = XmlParser.new(xml)
      parser.pull_to_path(:settings, :localRepository)
      interpolate(parser.pull_text_until_end.strip)
    rescue XmlParser::NotFoundError
      default_local_repo_path
    end
  else
    default_local_repo_path
  end
end