Class: Naether

Inherits:
Object
  • Object
show all
Defined in:
lib/naether.rb,
lib/naether/java.rb,
lib/naether/bootstrap.rb

Overview

:title:Naether::Java

Handles loading jars. Determines correct platform to use, Naether::Java::JRuby or Naether::Java::Ruby

Authors

Michael Guymon

Defined Under Namespace

Classes: Bootstrap, Java

Constant Summary collapse

JAR_LIB =

Naether jar path will default to packaged in the gem

"#{File.dirname(__FILE__)}/.."
VERSION =

VERSION file not found in gem dir, assume running from checkout

File.open( "VERSION" ) {|f| f.readline }.strip
JAR_PATH =

Use the VERSION to create jar file name

"#{JAR_LIB}/naether-#{VERSION}.jar"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNaether

Create new instance. Naether.create_from_paths and Naether.create_from_jars should be used instead of Naether.new to ensure the dependencies for Naether are set into the classpath



58
59
60
61
62
63
64
65
66
# File 'lib/naether.rb', line 58

def initialize()
  
  if Naether.platform == 'java'
    @resolver = com.slackworks.naether.Naether.new 
  else
    naetherClass = Rjb::import('com.slackworks.naether.Naether') 
    @resolver = naetherClass.new
  end
end

Class Method Details

.bootstrap_dependencies(dep_file = nil) ⇒ Object

Java dependencies needed to bootstrap Naether



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

def bootstrap_dependencies( dep_file=nil )
  Naether::Bootstrap.dependencies( dep_file )
end

.create_from_jars(jars) ⇒ Object

Loads all jars creates a new instance of Naether



49
50
51
52
53
# File 'lib/naether.rb', line 49

def create_from_jars( jars )
  Naether::Java.load_jars( jars )
  
  Naether.new
end

.create_from_paths(deps_jar_dir, naether_jar_dir = nil) ⇒ Object

Loads all jars from paths and creates a new instance of Naether



41
42
43
44
45
46
# File 'lib/naether.rb', line 41

def create_from_paths( deps_jar_dir, naether_jar_dir = nil )
  naether_jar_dir = naether_jar_dir || JAR_LIB
  Naether::Java.load_jars_dir( [deps_jar_dir, naether_jar_dir] )
  
  Naether.new
end

.platformObject

Helper to determine the platform



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

def platform
  $platform || RUBY_PLATFORM[/java/] || 'ruby'
end

Instance Method Details

#add_dependency(dependency) ⇒ Object

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



116
117
118
119
120
121
122
123
# File 'lib/naether.rb', line 116

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 in the notation: groupId:artifactId:type:version



98
99
100
# File 'lib/naether.rb', line 98

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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/naether.rb', line 103

def add_pom_dependencies( pom_path, scopes=['compile'] )
  if Naether.platform == 'java'
    @resolver.addDependencies( pom_path, scopes )
  else
    list = Rjb::import("java.util.ArrayList").new
    scopes.each do |scope|
      list.add( scope )
    end
    @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



74
75
76
77
78
79
80
# File 'lib/naether.rb', line 74

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

#build_pom(notation) ⇒ Object

Create the XML for a Maven Pom for the notation, groupId:artifactId:type:version

loads all resolved dependencies into pom



304
305
306
307
308
309
310
311
312
313
314
# File 'lib/naether.rb', line 304

def build_pom( notation )
  @project_instance = Naether::Java.create("com.slackworks.naether.maven.Project")
  @project_instance.setProjectNotation( notation )
  
  dependencies().each do |notation|
    @project_instance.addDependency( notation )
  end
  
  @project_instance.toXml()
  
end

#clear_remote_repositoriesObject

Clear all remote repositories



69
70
71
# File 'lib/naether.rb', line 69

def clear_remote_repositories
  @resolver.clearRemoteRepositories()
end

#dependenciesObject

Get array of dependencies



177
178
179
# File 'lib/naether.rb', line 177

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

#dependencies=(dependencies) ⇒ Object

Array of mixed dependencies.

* Artifact notation in the format of groupId:artifactId:version or groupId:artifactId:type:version - 'junit:junit:4.7' 
* Hash of a single artifaction notation => scope - { 'junit:junit:4.7' => 'test' }
* Path to a local pom - 'lib/pom.xml'
* Hash of a single path to a local pom => scope - { 'lib/pom.xml' => ['compile','test'] }


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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/naether.rb', line 130

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_classpathObject

Convert dependencies to Classpath friendly string



193
194
195
# File 'lib/naether.rb', line 193

def dependencies_classpath()
  @resolver.getResolvedClassPath()
end

#dependencies_notationObject Also known as: dependenciesNotation

Get array of dependencies as notation



182
183
184
# File 'lib/naether.rb', line 182

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

#dependencies_pathObject

Hash of dependency paths



188
189
190
# File 'lib/naether.rb', line 188

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/naether.rb', line 236

def deploy_artifact( notation, file_path, url, opts = {} )
  artifact = Naether::Java.create( "com.slackworks.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
  
  @resolver.deployArtifact(artifact)
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



254
255
256
# File 'lib/naether.rb', line 254

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

#load_dependencies_to_classpathObject

Load dependencies to Classpath



198
199
200
201
202
203
# File 'lib/naether.rb', line 198

def load_dependencies_to_classpath
  jars = dependencies_classpath.split(":")
  Naether::Java.load_jars(jars)
  
  jars
end

#load_pom(file_path) ⇒ Object



258
259
260
# File 'lib/naether.rb', line 258

def load_pom( file_path )
  @project_instance = Naether::Java.create("com.slackworks.naether.maven.Project", file_path )
end

#local_repo_pathObject

Path to local maven repo



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

def local_repo_path
  @resolver.getLocalRepoPath()
end

#local_repo_path=(path) ⇒ Object

Set path to local maven repo



93
94
95
# File 'lib/naether.rb', line 93

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

#pom_dependencies(file_path = nil, scopes = nil) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/naether.rb', line 262

def pom_dependencies( file_path=nil, scopes = nil)
  if file_path
    load_pom( file_path )
  end

  if Naether.platform == 'java'
    if scopes.nil?
      deps = @project_instance.getDependenciesNotation()
    else
      deps = @project_instance.getDependenciesNotation( scopes )
    end
    
  else
    list = nil
    if scopes
      list = Rjb::import("java.util.ArrayList").new
      scopes.each do |scope|
        list.add( scope )
      end
      
      deps = @project_instance._invoke('getDependenciesNotation', 'Ljava.util.List;', list)
    else
      deps = @project_instance.getDependenciesNotation()
    end
    
  end
  
  Naether::Java.convert_to_ruby_array( deps, true )
end

#pom_version(file_path = nil) ⇒ Object

Get the POM version



293
294
295
296
297
298
299
# File 'lib/naether.rb', line 293

def pom_version( file_path=nil )
  if file_path
    load_pom( file_path )
  end
  
  return @project_instance.getVersion()
end

#remote_repositoriesObject

Array of remote repositories



83
84
85
# File 'lib/naether.rb', line 83

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

#resolve_dependencies(download_artifacts = true, properties = nil) ⇒ Object

Resolve dependencies, finding related additional dependencies



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/naether.rb', line 206

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 );
  dependenciesNotation
end

#to_local_paths(notations) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/naether.rb', line 220

def to_local_paths( notations ) 
  if Naether.platform == 'java'
    Naether::Java.convert_to_ruby_array( @resolver.getLocalPaths( notations ) )
  else
    list = Rjb::import("java.util.ArrayList").new
    notations.each do |notation|
      list.add( notation )
    end
    paths = @resolver._invoke('getLocalPaths', 'Ljava.util.List;', list)
    
    Naether::Java.convert_to_ruby_array( paths, true )
  end
  
end

#write_pom(notation, file_path) ⇒ Object

notation of the pom, groupId:artifactId:type:version filePath to write the pom

loads all resolved dependencies into pom



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/naether.rb', line 320

def write_pom( notation, file_path )
  @project_instance = Naether::Java.create("com.slackworks.naether.maven.Project")
  @project_instance.setProjectNotation( notation )
  
  dependencies().each do |notation|
    @project_instance.addDependency( notation )
  end
  
  @project_instance.writePom( file_path )
  
end