Module: Jars

Defined in:
lib/jar_dependencies.rb,
lib/jars/lock.rb,
lib/jars/mima.rb,
lib/jars/version.rb,
lib/jars/classpath.rb,
lib/jars/installer.rb,
lib/jars/lock_down.rb,
lib/jars/maven_exec.rb,
lib/jars/mima/version.rb,
lib/jars/maven_settings.rb,
lib/jars/gemspec_artifacts.rb

Overview

Copyright © 2014 Christian Meier

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: Mima Classes: Classpath, GemspecArtifacts, Installer, JarDetails, JarLoadError, Lock, LockDown, MavenExec, MavenSettings, MavenVersion

Constant Summary collapse

VERSION =
'0.6.0.pre1'
MAVEN_SETTINGS =

rubocop:disable Style/RedundantFreeze

'JARS_MAVEN_SETTINGS'.freeze
LOCAL_MAVEN_REPO =
'JARS_LOCAL_MAVEN_REPO'.freeze
LOCK =

lock file to use

'JARS_LOCK'.freeze
HOME =

where the locally stored jars are search for or stored

'JARS_HOME'.freeze
SKIP =

skip the gem post install hook

'JARS_SKIP'.freeze
SKIP_LOCK =

skip Jars.lock mainly to run lock_jars

'JARS_SKIP_LOCK'.freeze
REQUIRE =

do not require any jars if set to false

'JARS_REQUIRE'.freeze
NO_REQUIRE =
'JARS_NO_REQUIRE'.freeze
QUIET =

no more warnings on conflict. this still requires jars but will not warn. it is needed to load jars from (default) gems which do contribute to any dependency manager (maven, gradle, jbundler)

'JARS_QUIET'.freeze
VERBOSE =

show resolver output

'JARS_VERBOSE'.freeze
DEBUG =

show jar-dependencies debug output

'JARS_DEBUG'.freeze
VENDOR =

vendor jars inside gem when installing gem

'JARS_VENDOR'.freeze
UNKNOWN =

string used when the version is unknown

'unknown'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.require=(value) ⇒ Object (writeonly)

Sets the attribute require

Parameters:

  • value

    the value to set the attribute require to.



115
116
117
# File 'lib/jar_dependencies.rb', line 115

def require=(value)
  @require = value
end

Class Method Details

.absolute(file) ⇒ Object



291
292
293
# File 'lib/jar_dependencies.rb', line 291

def absolute(file)
  File.expand_path(file) if file
end

.debug(msg = nil) ⇒ Object



284
285
286
287
288
289
# File 'lib/jar_dependencies.rb', line 284

def debug(msg = nil)
  return unless debug?

  msg = "#{msg.inspect}\n\t#{(msg.backtrace || []).join("\n\t")}" if msg.is_a?(Exception)
  Kernel.warn(msg || yield)
end

.debug?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/jar_dependencies.rb', line 135

def debug?
  to_boolean(DEBUG)
end

.freeze_loadingObject



143
144
145
# File 'lib/jar_dependencies.rb', line 143

def freeze_loading
  self.require = false
end

.homeObject



206
207
208
# File 'lib/jar_dependencies.rb', line 206

def home
  @home ||= absolute(to_prop(HOME)) || local_maven_repo
end

.jarfileObject



126
127
128
# File 'lib/jar_dependencies.rb', line 126

def jarfile
  ENV['JARFILE'] || ENV_JAVA['jarfile'] || ENV['JBUNDLER_JARFILE'] || ENV_JAVA['jbundler.jarfile'] || 'Jarfile'
end

.jars_lock_from_class_loaderObject



155
156
157
158
159
# File 'lib/jar_dependencies.rb', line 155

def jars_lock_from_class_loader
  return unless defined?(JRUBY_VERSION)

  JRuby::Util.class_loader_resources('Jars.lock')
end

.local_maven_repoObject



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

def local_maven_repo
  @local_maven_repo ||= absolute(to_prop(LOCAL_MAVEN_REPO)) ||
                        detect_local_repository(maven_local_settings) ||
                        detect_local_repository() ||
                        detect_local_repository(maven_global_settings) ||
                        File.join(user_home, '.m2', 'repository')
end

.lockObject



151
152
153
# File 'lib/jar_dependencies.rb', line 151

def lock
  @lock ||= to_prop(LOCK) || 'Jars.lock'
end

.lock_down(debug: nil, verbose: nil, **kwargs) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jar_dependencies.rb', line 64

def lock_down(debug: nil, verbose: nil, **kwargs)
  previous_debug = ENV[DEBUG]
  previous_verbose = ENV[VERBOSE]
  previous_skip_lock = ENV[SKIP_LOCK]
  ENV[DEBUG] = debug.to_s unless debug.nil?
  ENV[VERBOSE] = verbose.to_s unless verbose.nil?
  ENV[SKIP_LOCK] = 'true'
  require 'jars/lock_down' # do this lazy to keep things clean
  Jars::LockDown.new.lock_down(kwargs.delete(:vendor_dir), **kwargs)
ensure
  ENV[DEBUG] = previous_debug unless debug.nil?
  ENV[VERBOSE] = previous_verbose unless verbose.nil?
  ENV[SKIP_LOCK] = previous_skip_lock
end

.lock_path(basedir = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/jar_dependencies.rb', line 161

def lock_path(basedir = nil)
  return lock if File.exist?(lock)

  basedir ||= '.'
  ['.', 'jars', 'vendor/jars'].each do |dir|
    file = File.join(basedir, dir, lock)
    return file if File.exist?(file)
  end
  nil
end

.mark_as_required(group_id, artifact_id, *classifier_version) ⇒ Object



261
262
263
264
265
# File 'lib/jar_dependencies.rb', line 261

def mark_as_required(group_id, artifact_id, *classifier_version)
  require_jar_with_block(group_id, artifact_id, *classifier_version) do
    # ignore
  end
end

.maven_global_settingsObject



194
195
196
# File 'lib/jar_dependencies.rb', line 194

def maven_global_settings
  Jars::MavenSettings.global_settings
end

.maven_local_settingsObject



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

def maven_local_settings
  Jars::MavenSettings.local_settings
end

.maven_settingsObject



190
191
192
# File 'lib/jar_dependencies.rb', line 190

def maven_settings
  Jars::MavenSettings.settings
end

.maven_user_settingsObject



186
187
188
# File 'lib/jar_dependencies.rb', line 186

def 
  Jars::MavenSettings.
end

.no_more_warningsObject



122
123
124
# File 'lib/jar_dependencies.rb', line 122

def no_more_warnings
  @quiet = true
end

.quiet?Boolean

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/jar_dependencies.rb', line 117

def quiet?
  @quiet = to_boolean(QUIET) if @quiet.nil?
  @quiet
end

.require?Boolean

Returns:

  • (Boolean)


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

def require?
  if @require.nil?
    if (require = to_boolean(REQUIRE)).nil?
      no_require = to_boolean(NO_REQUIRE)
      @require = no_require.nil? || !no_require
    else
      @require = require
    end
  end
  @require
end

.require_jar(group_id, artifact_id, *classifier_version) ⇒ Object



267
268
269
270
271
272
273
274
275
276
# File 'lib/jar_dependencies.rb', line 267

def require_jar(group_id, artifact_id, *classifier_version)
  require_jars_lock unless skip_lock?
  if classifier_version.empty? && block_given?
    classifier_version = [yield].compact
    return mark_as_required(group_id, artifact_id, UNKNOWN) || false if classifier_version.empty?
  end
  require_jar_with_block(group_id, artifact_id, *classifier_version) do |gid, aid, version, classifier|
    do_require(gid, aid, version, classifier)
  end
end

.require_jars_lockObject



254
255
256
257
258
259
# File 'lib/jar_dependencies.rb', line 254

def require_jars_lock
  return if @jars_lock

  require_jars_lock!
  @jars_lock ||= true # rubocop:disable Naming/MemoizedInstanceVariableName
end

.require_jars_lock!(scope = :runtime) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/jar_dependencies.rb', line 210

def require_jars_lock!(scope = :runtime)
  urls = jars_lock_from_class_loader if to_prop(LOCK).nil?
  if urls && !urls.empty?
    @jars_lock = true

    done = []
    while done != urls
      urls.each do |url|
        next if done.member?(url)

        Jars.debug { "--- load jars from uri #{url}" }
        classpath = Jars::Classpath.new(nil, "uri:#{url}")
        classpath.require(scope)
        done << url
      end
    end
    no_more_warnings
  elsif (jars_lock = Jars.lock_path)
    Jars.debug { "--- load jars from #{jars_lock}" }
    @jars_lock = jars_lock

    classpath = Jars::Classpath.new(nil, jars_lock)
    classpath.require(scope)
    no_more_warnings
  end
  Jars.debug do
    loaded = @jars.collect { |k, v| "#{k}:#{v}" }
    "--- loaded jars ---\n\t#{loaded.join("\n\t")}"
  end
end

.resetObject



172
173
174
175
176
177
178
179
180
# File 'lib/jar_dependencies.rb', line 172

def reset
  instance_variables.each do |var|
    next if var == :@jars_lock

    instance_variable_set(var, nil)
  end
  Jars::MavenSettings.reset
  @jars = {}
end

.setup(options = nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/jar_dependencies.rb', line 241

def setup(options = nil)
  case options
  when Symbol
    require_jars_lock!(options)
  when Hash
    @home = options[:jars_home]
    @lock = options[:jars_lock]
    require_jars_lock!(options[:scope] || :runtime)
  else
    require_jars_lock!
  end
end

.skip?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/jar_dependencies.rb', line 100

def skip?
  to_boolean(SKIP)
end

.skip_lock?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/jar_dependencies.rb', line 147

def skip_lock?
  to_prop(SKIP_LOCK) || false
end

.to_boolean(key) ⇒ Object



94
95
96
97
98
# File 'lib/jar_dependencies.rb', line 94

def to_boolean(key)
  return nil if (prop = to_prop(key)).nil?

  prop.empty? || prop.eql?('true')
end

.user_homeObject



295
296
297
298
299
300
301
# File 'lib/jar_dependencies.rb', line 295

def user_home
  ENV['HOME'] || begin
    user_home = Dir.home if Dir.respond_to?(:home)
    user_home = ENV_JAVA['user.home'] if !user_home && Object.const_defined?(:ENV_JAVA)
    user_home
  end
end

.vendor?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/jar_dependencies.rb', line 139

def vendor?
  to_boolean(VENDOR)
end

.verbose?Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/jar_dependencies.rb', line 130

def verbose?
  verbose = to_boolean(VERBOSE)
  verbose.nil? ? debug? : (verbose || !!debug?)
end

.warn(msg = nil) ⇒ Object



278
279
280
281
282
# File 'lib/jar_dependencies.rb', line 278

def warn(msg = nil)
  return if quiet? && !debug?

  Kernel.warn(msg || yield)
end

Instance Method Details

#to_prop(key) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/jar_dependencies.rb', line 80

def to_prop(key)
  key = key.tr('_', '.')
  ENV_JAVA[(key.downcase!
            key)] ||
    ENV[(key.tr!('.', '_')
         key.upcase!
         key)]
end