Class: Naether::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/naether/runtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Create new instance.



32
33
34
# File 'lib/naether/runtime.rb', line 32

def initialize
  @resolver = Naether::Java.create('com.tobedevoured.naether.impl.NaetherImpl')
end

Instance Attribute Details

#resolverObject (readonly)

Returns the value of attribute resolver.



29
30
31
# File 'lib/naether/runtime.rb', line 29

def resolver
  @resolver
end

Instance Method Details

#add_build_artifact(notation, path, pom = nil) ⇒ Object

Add a local Build Artifact, that will be used in the Dependency Resolution

Parameters:

  • notation (String)
  • path (String)

    to artifact

  • pom (String) (defaults to: nil)

    optional path to pom.xml



89
90
91
# File 'lib/naether/runtime.rb', line 89

def add_build_artifact( notation, path, pom = nil )
  @resolver.addBuildArtifact(notation, path, pom )
end

#add_dependency(dependency) ⇒ Object

Add a dependency of org.sonatype.aether.graph.Dependency

Parameters:

  • dependency (org.sonatype.aether.graph.Dependency)


157
158
159
160
161
162
163
164
# File 'lib/naether/runtime.rb', line 157

def add_dependency( dependency )
  #@resolver.addDependency( dependency )
  if Naether.platform == 'java'
    @resolver.addDependency( dependency )
  else
    @resolver._invoke('addDependency', 'Lorg.sonatype.aether.graph.Dependency;', dependency)
  end
end

#add_notation_dependency(notation, scope = 'compile') ⇒ Object

Add a dependency

Parameters:

  • notation (String)

    in the format of groupId:artifactId:version

  • scope (String) (defaults to: 'compile')

    valid options are compile,test,runtime

See Also:



137
138
139
# File 'lib/naether/runtime.rb', line 137

def add_notation_dependency( notation, scope='compile' )
  @resolver.addDependency( notation, scope )
end

#add_pom_dependencies(pom_path, scopes = ['compile']) ⇒ Object

Add dependencies from a Maven POM

Parameters:

  • pom_path (String)
  • scopes (Array<String>) (defaults to: ['compile'])

    valid options are compile,test,runtime



145
146
147
148
149
150
151
152
# File 'lib/naether/runtime.rb', line 145

def add_pom_dependencies( pom_path, scopes=['compile'] )
  if Naether.platform == 'java'
    @resolver.addDependencies( pom_path, scopes )
  else
    list = Naether::Java.convert_to_java_list( scopes )
    @resolver._invoke( 'addDependencies', 'Ljava.lang.String;Ljava.util.List;', pom_path, list )
  end
end

#add_remote_repository(url, username = nil, password = nil) ⇒ Object

Add remote repository

Parameters:

  • url (String)

    of remote repo

  • username (String) (defaults to: nil)

    optional

  • password (String) (defaults to: nil)

    optioanl



46
47
48
49
50
51
52
# File 'lib/naether/runtime.rb', line 46

def add_remote_repository( url, username = nil, password = nil )
  if username
    @resolver.addRemoteRepositoryByUrl( url, username, password )
  else
    @resolver.addRemoteRepositoryByUrl( url )
  end
end

#build_artifactsArray

Get Build Artifacts

Returns:

  • (Array)

    of build artifacts



128
129
130
# File 'lib/naether/runtime.rb', line 128

def build_artifacts
  Naether::Java.convert_to_ruby_array( @resolver.getBuildArtifacts() )
end

#build_artifacts=(artifacts) ⇒ Object

Set local Build Artifacts, that will be used in the Dependency Resolution

Parameters:

  • artifacts (Array<Hash>)

    of Hashs with { notation => path } or { notation => { :path => path, :pom => pom_path }



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/naether/runtime.rb', line 98

def build_artifacts=( artifacts )
  @resolver.clearBuildArtifacts()

  unless artifacts.is_a? Array
    artifacts = [artifacts]
  end

  artifacts.each do |artifact|
    # Hash of notation => path or notation => { :path =>, :pom => }
    if artifact.is_a? Hash

      notation, opts = artifact.shift

      if opts.is_a? Hash
        @resolver.add_build_artifact( notation, opts[:path], opts[:pom] )
      else
        @resolver.add_build_artifact( notation, opts )
      end

    else
      raise "invalid build_artifacts format"
    end
  end
end

#clear_remote_repositoriesObject

Clear all remote repositories



37
38
39
# File 'lib/naether/runtime.rb', line 37

def clear_remote_repositories
  @resolver.clearRemoteRepositories()
end

#dependenciesArray

Get array of dependencies

Returns:

  • (Array)


225
226
227
# File 'lib/naether/runtime.rb', line 225

def dependencies
  Naether::Java.convert_to_ruby_array( @resolver.currentDependencies() )
end

#dependencies=(dependencies) ⇒ Object

Set the dependencies

The dependencies param takes an [Array] of mixed dependencies:

* [String] Artifact notation, such as groupId:artifactId:version, e.g. 'junit:junit:4.7'
* [Hash] of a single artifaction notation => scope - { 'junit:junit:4.7' => 'test' }
* [String] path to a local pom - 'lib/pom.xml'
* [Hash] of a single path to a local pom => scope - { 'lib/pom.xml' => ['compile','test'] }

Parameters:

  • dependencies (Array)

See Also:



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/naether/runtime.rb', line 176

def dependencies=(dependencies)
  @resolver.clearDependencies()

  unless dependencies.is_a? Array
    dependencies = [dependencies]
  end

  dependencies.each do |dependent|
    # Hash of notation => scope
    if dependent.is_a? Hash
      key = dependent.keys.first

      # Add pom dependencies with scopes
      if key =~ /\.xml$/i
        scopes = nil
        if dependent[key].is_a? Array
          scopes = dependent[key]
        else
          scopes = [dependent[key]]
        end

        add_pom_dependencies( key, scopes )

        # Add a dependency notation with scopes
      else
        add_notation_dependency( key, dependent[key] )
      end

    elsif dependent.is_a? String

      # Add pom dependencies with default scopes
      if dependent =~ /\.xml$/i
        add_pom_dependencies( dependent )

        # Add a dependency notation with compile scope
      else
        add_notation_dependency( dependent, 'compile' )
      end

      # Add an Aether dependency
    else
      add_dependency( dependent )
    end
  end
end

#dependencies_classpathString

Convert dependencies to Classpath friendly string

Returns:

  • (String)


247
248
249
# File 'lib/naether/runtime.rb', line 247

def dependencies_classpath
  @resolver.getResolvedClassPath()
end

#dependencies_graph(nodes = nil) ⇒ Hash

Dependencies as a Graph of nested Hashes

Returns:

  • (Hash)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/naether/runtime.rb', line 254

def dependencies_graph(nodes=nil)
  nodes = @resolver.getDependenciesGraph() unless nodes

  graph = {}
  if Naether.platform == 'java'
    nodes.each do |k,v|
      deps = dependencies_graph(v)
      graph[k] = Naether::Java.convert_to_ruby_hash( deps )
    end
  else
    iterator = nodes.entrySet().iterator();
    while iterator.hasNext()
      entry = iterator.next()
      deps = dependencies_graph(entry.getValue())
      graph[entry.getKey().toString()] = Naether::Java.convert_to_ruby_hash( deps )
    end
  end

  graph
end

#dependencies_notationArray<String>

Get array of dependencies as notation

Returns:

  • (Array<String>)

    of notations

See Also:



233
234
235
# File 'lib/naether/runtime.rb', line 233

def dependencies_notation
  Naether::Java.convert_to_ruby_array(@resolver.getDependenciesNotation(), true)
end

#dependencies_pathArray<Hash>

Hash of dependency paths

Returns:

  • (Array<Hash>)

    of { notation => path }



240
241
242
# File 'lib/naether/runtime.rb', line 240

def dependencies_path
  Naether::Java.convert_to_ruby_hash( @resolver.getDependenciesPath(), true )
end

#deploy_artifact(notation, file_path, url, opts = {}) ⇒ Object

Deploy artifact to remote repo url

Parameters:

  • notation (String)
  • file_path (String)

    to artifact to deploy

  • url (String)

    to deploy to

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :pom_path (String)

    path to pom.xml

  • :username (String)

    for optional auth

  • :password (String)

    for optional auth

  • :pub_key (String)

    for optional auth

  • :pub_key_passphrase (String)

    for optional auth



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/naether/runtime.rb', line 364

def deploy_artifact( notation, file_path, url, opts = {} )
  artifact = Naether::Java.create( "com.tobedevoured.naether.deploy.DeployArtifact" )

  artifact.setRemoteRepo( url )
  artifact.setNotation( notation )
  artifact.setFilePath( file_path )
  if opts[:pom_path]
    artifact.setPomPath( opts[:pom_path] )
  end

  if opts[:username] || opts[:pub_key]
    artifact.setAuth(opts[:username], opts[:password], opts[:pub_key], opts[:pub_key_passphrase] )
  end
  if Naether.platform == 'java'
    @resolver.deployArtifact(artifact)
  else
    @resolver._invoke( 'deployArtifact', 'Lcom.tobedevoured.naether.deploy.DeployArtifact;', artifact )
  end
end

#download_artifacts(notations) ⇒ Array<String>

Download artifacts

Parameters:

  • notations (Array<String>)

Returns:

  • (Array<String>)

    of paths of downloaded artifacts



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/naether/runtime.rb', line 331

def download_artifacts( notations )
  if( notations.is_a? String )
    notations = [notations]
  end

  files = nil
  if Naether.platform == 'java'
    files = @resolver.downloadArtifacts( notations )
  else
    list = Naether::Java.convert_to_java_list( notations )
    files = @resolver._invoke('downloadArtifacts', 'Ljava.util.List;', list)
  end

  paths = []
  Naether::Java.convert_to_ruby_array(files).each do |file|
    paths << file.getAbsolutePath();
  end

  paths
end

#install(notation, pom_path = nil, jar_path = nil) ⇒ Object

Install artifact or pom to local repo, must specify pom_path and/or jar_path

Parameters:

  • notation (String)
  • pom_path (String) (defaults to: nil)
  • jar_path (String) (defaults to: nil)


389
390
391
# File 'lib/naether/runtime.rb', line 389

def install( notation, pom_path =nil, jar_path = nil )
  @resolver.install(notation, pom_path, jar_path)
end

#load_dependencies_to_classpathArray

Load dependencies to Classpath

Returns:

  • (Array)

    of loaded jars



278
279
280
281
282
283
# File 'lib/naether/runtime.rb', line 278

def load_dependencies_to_classpath
  jars = dependencies_classpath.split(File::PATH_SEPARATOR)
  Naether::Java.load_jars(jars)

  jars
end

#local_repo_pathString

Path to local maven repo

Returns:

  • (String)

    path to local repo



71
72
73
# File 'lib/naether/runtime.rb', line 71

def local_repo_path
  @resolver.getLocalRepoPath()
end

#local_repo_path=(path) ⇒ Object

Set path to local maven repo

Parameters:

  • path (String)

    local repo



78
79
80
# File 'lib/naether/runtime.rb', line 78

def local_repo_path=( path )
  @resolver.setLocalRepoPath( path )
end

#remote_repositoriesArray

Get remote repositories

Returns:

  • (Array)

    of remote repos



57
58
59
# File 'lib/naether/runtime.rb', line 57

def remote_repositories
  Naether::Java.convert_to_ruby_array(@resolver.getRemoteRepositories())
end

#remote_repository_urlsArray<String>

Get remote repositories as urls

Returns:

  • (Array<String>)

    of String urls



64
65
66
# File 'lib/naether/runtime.rb', line 64

def remote_repository_urls
  Naether::Java.convert_to_ruby_array(@resolver.getRemoteRepositoryUrls(), true)
end

#resolve_dependencies(download_artifacts = true, properties = nil) ⇒ Array<String>

Resolve dependencies

Returns:

  • (Array<String>)

    of notations

See Also:



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/naether/runtime.rb', line 289

def resolve_dependencies( download_artifacts = true, properties = nil )

  if properties
    # Convert to HashMap
    map = Naether::Java.create( "java.util.HashMap" )
    properties.each do |k,v|
      map.put( k, v )
    end
  end

  @resolver.resolveDependencies( download_artifacts, map );
  dependencies_notation
end

#set_log_level(level) ⇒ Object

Set Log level for Naether Java logging

Parameters:

  • level (String)

    to debug, info, warn, or error



396
397
398
# File 'lib/naether/runtime.rb', line 396

def set_log_level( level )
  Naether::Java.exec_static_method('com.tobedevoured.naether.util.LogUtil', 'changeLevel', ['com.tobedevoured', level] )
end

#to_local_paths(notations) ⇒ Array<String>

Convert notations to local paths of artifacts

Parameters:

  • notations (Array<String>)

Returns:

  • (Array<String>)

    of paths to artifacts

See Also:



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/naether/runtime.rb', line 308

def to_local_paths( notations )
  if Naether.platform == 'java'
    Naether::Java.convert_to_ruby_array(
        Naether::Java.exec_static_method(
            'com.tobedevoured.naether.util.Notation',
            'getLocalPaths',
            [local_repo_path, notations ],
            ['java.lang.String', 'java.util.List'] ) )
  else
    paths =  Naether::Java.exec_static_method(
        'com.tobedevoured.naether.util.Notation',
        'getLocalPaths',
        [local_repo_path, Naether::Java.convert_to_java_list(notations) ],
        ['java.lang.String', 'java.util.List'] )
    Naether::Java.convert_to_ruby_array( paths, true )
  end

end