naether 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,8 +3,8 @@ require "#{File.dirname(__FILE__)}/configuration"
3
3
  require 'yaml'
4
4
  require 'open-uri'
5
5
  require 'fileutils'
6
-
7
- class Naether
6
+
7
+ module Naether
8
8
 
9
9
  #
10
10
  # Helper for bootstrapping Naether
@@ -1,5 +1,5 @@
1
1
 
2
- class Naether
2
+ module Naether
3
3
 
4
4
  #
5
5
  # Naether runtime configuration
@@ -22,6 +22,7 @@ class Naether
22
22
 
23
23
 
24
24
  @data = {
25
+ :version => version,
25
26
  :gem_dir => gem_dir,
26
27
  :naether_jar => File.join( gem_dir, "core-#{version}.jar"),
27
28
  :platform => ($platform || RUBY_PLATFORM[/java/] || 'ruby'),
data/lib/naether/java.rb CHANGED
@@ -7,7 +7,7 @@ require "#{File.dirname(__FILE__)}/configuration"
7
7
  #
8
8
  # @author Michael Guymon
9
9
  #
10
- class Naether
10
+ module Naether
11
11
  class Java
12
12
  include Singleton
13
13
 
@@ -6,7 +6,7 @@ require "#{File.dirname(__FILE__)}/../configuration"
6
6
 
7
7
  # @author Michael Guymon
8
8
  #
9
- class Naether
9
+ module Naether
10
10
  class Java
11
11
 
12
12
  class JRuby
@@ -5,7 +5,7 @@ require "#{File.dirname(__FILE__)}/../configuration"
5
5
  #
6
6
  # @author Michael Guymon
7
7
  #
8
- class Naether
8
+ module Naether
9
9
  class Java
10
10
 
11
11
  class Ruby
data/lib/naether/maven.rb CHANGED
@@ -15,7 +15,7 @@
15
15
 
16
16
  require "#{File.dirname(__FILE__)}/java"
17
17
 
18
- class Naether
18
+ module Naether
19
19
 
20
20
  # Helper for interacting with a Maven POM
21
21
  #
@@ -1,4 +1,4 @@
1
- class Naether
1
+ module Naether
2
2
 
3
3
  #
4
4
  # Helper for handling Maven notations, supports notations:
@@ -0,0 +1,400 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require "#{File.dirname(__FILE__)}/configuration"
17
+ require "#{File.dirname(__FILE__)}/bootstrap"
18
+ require "#{File.dirname(__FILE__)}/java"
19
+
20
+ #
21
+ # Java dependency resolver
22
+ #
23
+ # @author Michael Guymon
24
+ # @see https://github.com/mguymon/naether/tree/master/core
25
+ #
26
+ module Naether
27
+ class Runtime
28
+
29
+ attr_reader :resolver
30
+
31
+ # Create new instance.
32
+ def initialize
33
+ @resolver = Naether::Java.create('com.tobedevoured.naether.impl.NaetherImpl')
34
+ end
35
+
36
+ # Clear all remote repositories
37
+ def clear_remote_repositories
38
+ @resolver.clearRemoteRepositories()
39
+ end
40
+
41
+ # Add remote repository
42
+ #
43
+ # @param [String] url of remote repo
44
+ # @param [String] username optional
45
+ # @param [String] password optioanl
46
+ def add_remote_repository( url, username = nil, password = nil )
47
+ if username
48
+ @resolver.addRemoteRepositoryByUrl( url, username, password )
49
+ else
50
+ @resolver.addRemoteRepositoryByUrl( url )
51
+ end
52
+ end
53
+
54
+ # Get remote repositories
55
+ #
56
+ # @return [Array] of remote repos
57
+ def remote_repositories
58
+ Naether::Java.convert_to_ruby_array(@resolver.getRemoteRepositories())
59
+ end
60
+
61
+ # Get remote repositories as urls
62
+ #
63
+ # @return [Array<String>] of String urls
64
+ def remote_repository_urls
65
+ Naether::Java.convert_to_ruby_array(@resolver.getRemoteRepositoryUrls(), true)
66
+ end
67
+
68
+ # Path to local maven repo
69
+ #
70
+ # @return [String] path to local repo
71
+ def local_repo_path
72
+ @resolver.getLocalRepoPath()
73
+ end
74
+
75
+ # Set path to local maven repo
76
+ #
77
+ # @param [String] path local repo
78
+ def local_repo_path=( path )
79
+ @resolver.setLocalRepoPath( path )
80
+ end
81
+
82
+ #
83
+ # Add a local Build Artifact, that will be used in the Dependency Resolution
84
+ #
85
+ # @param [String] notation
86
+ # @param [String] path to artifact
87
+ # @param [String] pom optional path to pom.xml
88
+ #
89
+ def add_build_artifact( notation, path, pom = nil )
90
+ @resolver.addBuildArtifact(notation, path, pom )
91
+ end
92
+
93
+ #
94
+ # Set local Build Artifacts, that will be used in the Dependency Resolution
95
+ #
96
+ # @param [Array<Hash>] artifacts of Hashs with { notation => path } or { notation => { :path => path, :pom => pom_path }
97
+ #
98
+ def build_artifacts=( artifacts )
99
+ @resolver.clearBuildArtifacts()
100
+
101
+ unless artifacts.is_a? Array
102
+ artifacts = [artifacts]
103
+ end
104
+
105
+ artifacts.each do |artifact|
106
+ # Hash of notation => path or notation => { :path =>, :pom => }
107
+ if artifact.is_a? Hash
108
+
109
+ notation, opts = artifact.shift
110
+
111
+ if opts.is_a? Hash
112
+ @resolver.add_build_artifact( notation, opts[:path], opts[:pom] )
113
+ else
114
+ @resolver.add_build_artifact( notation, opts )
115
+ end
116
+
117
+ else
118
+ raise "invalid build_artifacts format"
119
+ end
120
+ end
121
+ end
122
+
123
+ #
124
+ # Get Build Artifacts
125
+ #
126
+ # @return [Array] of build artifacts
127
+ #
128
+ def build_artifacts
129
+ Naether::Java.convert_to_ruby_array( @resolver.getBuildArtifacts() )
130
+ end
131
+
132
+ # Add a dependency
133
+ #
134
+ # @param [String] notation in the format of groupId:artifactId:version
135
+ # @param [String] scope valid options are compile,test,runtime
136
+ # @see https://github.com/mguymon/naether/wiki/Notations
137
+ def add_notation_dependency( notation, scope='compile' )
138
+ @resolver.addDependency( notation, scope )
139
+ end
140
+
141
+ # Add dependencies from a Maven POM
142
+ #
143
+ # @param [String] pom_path
144
+ # @param [Array<String>] scopes valid options are compile,test,runtime
145
+ def add_pom_dependencies( pom_path, scopes=['compile'] )
146
+ if Naether.platform == 'java'
147
+ @resolver.addDependencies( pom_path, scopes )
148
+ else
149
+ list = Naether::Java.convert_to_java_list( scopes )
150
+ @resolver._invoke( 'addDependencies', 'Ljava.lang.String;Ljava.util.List;', pom_path, list )
151
+ end
152
+ end
153
+
154
+ # Add a dependency of org.sonatype.aether.graph.Dependency
155
+ #
156
+ # @param [org.sonatype.aether.graph.Dependency] dependency
157
+ def add_dependency( dependency )
158
+ #@resolver.addDependency( dependency )
159
+ if Naether.platform == 'java'
160
+ @resolver.addDependency( dependency )
161
+ else
162
+ @resolver._invoke('addDependency', 'Lorg.sonatype.aether.graph.Dependency;', dependency)
163
+ end
164
+ end
165
+
166
+ # Set the dependencies
167
+ #
168
+ # The dependencies param takes an [Array] of mixed dependencies:
169
+ # * [String] Artifact notation, such as groupId:artifactId:version, e.g. 'junit:junit:4.7'
170
+ # * [Hash] of a single artifaction notation => scope - { 'junit:junit:4.7' => 'test' }
171
+ # * [String] path to a local pom - 'lib/pom.xml'
172
+ # * [Hash] of a single path to a local pom => scope - { 'lib/pom.xml' => ['compile','test'] }
173
+ #
174
+ # @param [Array] dependencies
175
+ # @see https://github.com/mguymon/naether/wiki/Notations
176
+ def dependencies=(dependencies)
177
+ @resolver.clearDependencies()
178
+
179
+ unless dependencies.is_a? Array
180
+ dependencies = [dependencies]
181
+ end
182
+
183
+ dependencies.each do |dependent|
184
+ # Hash of notation => scope
185
+ if dependent.is_a? Hash
186
+ key = dependent.keys.first
187
+
188
+ # Add pom dependencies with scopes
189
+ if key =~ /\.xml$/i
190
+ scopes = nil
191
+ if dependent[key].is_a? Array
192
+ scopes = dependent[key]
193
+ else
194
+ scopes = [dependent[key]]
195
+ end
196
+
197
+ add_pom_dependencies( key, scopes )
198
+
199
+ # Add a dependency notation with scopes
200
+ else
201
+ add_notation_dependency( key, dependent[key] )
202
+ end
203
+
204
+ elsif dependent.is_a? String
205
+
206
+ # Add pom dependencies with default scopes
207
+ if dependent =~ /\.xml$/i
208
+ add_pom_dependencies( dependent )
209
+
210
+ # Add a dependency notation with compile scope
211
+ else
212
+ add_notation_dependency( dependent, 'compile' )
213
+ end
214
+
215
+ # Add an Aether dependency
216
+ else
217
+ add_dependency( dependent )
218
+ end
219
+ end
220
+ end
221
+
222
+ # Get array of dependencies
223
+ #
224
+ # @return [Array]
225
+ def dependencies
226
+ Naether::Java.convert_to_ruby_array( @resolver.currentDependencies() )
227
+ end
228
+
229
+ # Get array of dependencies as notation
230
+ #
231
+ # @return [Array<String>] of notations
232
+ # @see https://github.com/mguymon/naether/wiki/Notations
233
+ def dependencies_notation
234
+ Naether::Java.convert_to_ruby_array(@resolver.getDependenciesNotation(), true)
235
+ end
236
+
237
+ # Hash of dependency paths
238
+ #
239
+ # @return [Array<Hash>] of { notation => path }
240
+ def dependencies_path
241
+ Naether::Java.convert_to_ruby_hash( @resolver.getDependenciesPath(), true )
242
+ end
243
+
244
+ # Convert dependencies to Classpath friendly string
245
+ #
246
+ # @return [String]
247
+ def dependencies_classpath
248
+ @resolver.getResolvedClassPath()
249
+ end
250
+
251
+ # Dependencies as a Graph of nested Hashes
252
+ #
253
+ # @return [Hash]
254
+ def dependencies_graph(nodes=nil)
255
+ nodes = @resolver.getDependenciesGraph() unless nodes
256
+
257
+ graph = {}
258
+ if Naether.platform == 'java'
259
+ nodes.each do |k,v|
260
+ deps = dependencies_graph(v)
261
+ graph[k] = Naether::Java.convert_to_ruby_hash( deps )
262
+ end
263
+ else
264
+ iterator = nodes.entrySet().iterator();
265
+ while iterator.hasNext()
266
+ entry = iterator.next()
267
+ deps = dependencies_graph(entry.getValue())
268
+ graph[entry.getKey().toString()] = Naether::Java.convert_to_ruby_hash( deps )
269
+ end
270
+ end
271
+
272
+ graph
273
+ end
274
+
275
+ # Load dependencies to Classpath
276
+ #
277
+ # @return [Array] of loaded jars
278
+ def load_dependencies_to_classpath
279
+ jars = dependencies_classpath.split(":")
280
+ Naether::Java.load_jars(jars)
281
+
282
+ jars
283
+ end
284
+
285
+ # Resolve dependencies
286
+ #
287
+ # @return [Array<String>] of notations
288
+ # @see https://github.com/mguymon/naether/wiki/Notations
289
+ def resolve_dependencies( download_artifacts = true, properties = nil )
290
+
291
+ if properties
292
+ # Convert to HashMap
293
+ map = Naether::Java.create( "java.util.HashMap" )
294
+ properties.each do |k,v|
295
+ map.put( k, v )
296
+ end
297
+ end
298
+
299
+ @resolver.resolveDependencies( download_artifacts, map );
300
+ dependencies_notation
301
+ end
302
+
303
+ # Convert notations to local paths of artifacts
304
+ #
305
+ # @param [Array<String>] notations
306
+ # @return [Array<String>] of paths to artifacts
307
+ # @see https://github.com/mguymon/naether/wiki/Notations
308
+ def to_local_paths( notations )
309
+ if Naether.platform == 'java'
310
+ Naether::Java.convert_to_ruby_array(
311
+ Naether::Java.exec_static_method(
312
+ 'com.tobedevoured.naether.util.Notation',
313
+ 'getLocalPaths',
314
+ [local_repo_path, notations ],
315
+ ['java.lang.String', 'java.util.List'] ) )
316
+ else
317
+ paths = Naether::Java.exec_static_method(
318
+ 'com.tobedevoured.naether.util.Notation',
319
+ 'getLocalPaths',
320
+ [local_repo_path, Naether::Java.convert_to_java_list(notations) ],
321
+ ['java.lang.String', 'java.util.List'] )
322
+ Naether::Java.convert_to_ruby_array( paths, true )
323
+ end
324
+
325
+ end
326
+
327
+ # Download artifacts
328
+ #
329
+ # @param [Array<String>] notations
330
+ # @return [Array<String>] of paths of downloaded artifacts
331
+ def download_artifacts( notations )
332
+ if( notations.is_a? String )
333
+ notations = [notations]
334
+ end
335
+
336
+ files = nil
337
+ if Naether.platform == 'java'
338
+ files = @resolver.downloadArtifacts( notations )
339
+ else
340
+ list = Naether::Java.convert_to_java_list( notations )
341
+ files = @resolver._invoke('downloadArtifacts', 'Ljava.util.List;', list)
342
+ end
343
+
344
+ paths = []
345
+ Naether::Java.convert_to_ruby_array(files).each do |file|
346
+ paths << file.getAbsolutePath();
347
+ end
348
+
349
+ paths
350
+ end
351
+
352
+
353
+ # Deploy artifact to remote repo url
354
+ #
355
+ # @param [String] notation
356
+ # @param [String] file_path to artifact to deploy
357
+ # @param [String] url to deploy to
358
+ # @param [Hash] opts
359
+ # @option opts [String] :pom_path path to pom.xml
360
+ # @option opts [String] :username for optional auth
361
+ # @option opts [String] :password for optional auth
362
+ # @option opts [String] :pub_key for optional auth
363
+ # @option opts [String] :pub_key_passphrase for optional auth
364
+ def deploy_artifact( notation, file_path, url, opts = {} )
365
+ artifact = Naether::Java.create( "com.tobedevoured.naether.deploy.DeployArtifact" )
366
+
367
+ artifact.setRemoteRepo( url )
368
+ artifact.setNotation( notation )
369
+ artifact.setFilePath( file_path )
370
+ if opts[:pom_path]
371
+ artifact.setPomPath( opts[:pom_path] )
372
+ end
373
+
374
+ if opts[:username] || opts[:pub_key]
375
+ artifact.setAuth(opts[:username], opts[:password], opts[:pub_key], opts[:pub_key_passphrase] )
376
+ end
377
+ if Naether.platform == 'java'
378
+ @resolver.deployArtifact(artifact)
379
+ else
380
+ @resolver._invoke( 'deployArtifact', 'Lcom.tobedevoured.naether.deploy.DeployArtifact;', artifact )
381
+ end
382
+ end
383
+
384
+ # Install artifact or pom to local repo, must specify pom_path and/or jar_path
385
+ #
386
+ # @param [String] notation
387
+ # @param [String] pom_path
388
+ # @param [String] jar_path
389
+ def install( notation, pom_path =nil, jar_path = nil )
390
+ @resolver.install(notation, pom_path, jar_path)
391
+ end
392
+
393
+ # Set Log level for Naether Java logging
394
+ #
395
+ # @param [String] level to debug, info, warn, or error
396
+ def set_log_level( level )
397
+ Naether::Java.exec_static_method('com.tobedevoured.naether.util.LogUtil', 'changeLevel', ['com.tobedevoured', level] )
398
+ end
399
+ end
400
+ end