Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,38 @@ And look at the dependencies tree
lock_jars --tree
```

As `lock_jars` uses ruby-maven to resolve the jar dependencies.
Since jar-dependencies does not declare ruby-maven as runtime dependency
(you just not need ruby-maven during runtime only when you want to
setup the project it is needed) it is advicable to have it as
development dependency in your Gemfile.
`lock_jars` resolves the jars with the Maven resolver bundled inside
jar-dependencies (Mima), so you no longer need Maven or the ruby-maven gem
installed to run it.

# Jarfile

If your project is not a gem, or you would rather keep jar versions out of the
gemspec, list them in a `Jarfile`. The `lock_jars` will read these together with
your gemspec and bundled gems and resolves everything in one go into `Jars.lock`.

A `Jarfile` is plain Ruby and reads similar to a *Gemfile*:

```ruby
jar 'org.yaml:snakeyaml', '2.7'
pom 'org.springframework.boot:spring-boot-dependencies', '3.2.5'

# using artifacts that aren't on maven central
repository 'my-nexus', 'https://nexus.example.com/repo'

# drop a transitive dependency you do not want
jar 'io.dropwizard:dropwizard-logging', '5.0.2', exclusions: ['jakarta.validation:jakarta.validation-api']

# keep some jars out of the runtime
scope :test do
jar 'org.junit.jupiter:junit-jupiter', '5.11.0'
end
```

Both coordinate styles work, `'group:artifact', 'version'` or the packed
`'group:artifact:version'`, and you can pass a `:classifier`, a `:scope`, or a
block with nested `exclusion`s. Use `snapshot_repository` for a snapshot repo
and `local 'path/to/some.jar'` to point at a jar that already sits on disk.

# Proxy and mirror setup

Expand Down
18 changes: 11 additions & 7 deletions lib/jar_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Jars
autoload :Mima, 'jars/mima'

@jars = {}
@jars_lock = false
@jars_lock = nil
@jars_locked = nil

class JarLoadError < LoadError; end
Expand Down Expand Up @@ -177,13 +177,13 @@ def lock_path(basedir = nil)

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

instance_variable_set(var, nil)
end
@jars.clear

Jars::MavenSettings.reset
@jars = {}
@jars_locked = nil
end

def maven_local_settings
Expand Down Expand Up @@ -263,11 +263,15 @@ def setup(options = nil)
end
end

# Requires *Jars.lock* (once).
# @return whether a lock was actually required by this call
def require_jars_lock
return if @jars_lock
return false unless @jars_lock.nil?

require_jars_lock!
@jars_lock ||= true # rubocop:disable Naming/MemoizedInstanceVariableName
require_jars_lock! # sets @jars_lock to the loaded lock (truthy) when there is one
required = !@jars_lock.nil?
@jars_lock = false # do not require again
required
end

def mark_as_required(group_id, artifact_id, *classifier_version)
Expand Down
1 change: 1 addition & 0 deletions lib/jars/gemspec_artifacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def key
end
end

# @return [Array[Artifact]]
attr_reader :artifacts

def initialize(spec)
Expand Down
193 changes: 193 additions & 0 deletions lib/jars/jarfile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# frozen_string_literal: true

require 'jars/gemspec_artifacts'

module Jars
# Self-contained reader for the (j)bundler Jarfile DSL.
class Jarfile
def self.load(file)
new.tap { |jarfile| jarfile.eval_file(file) }
end

Repository = Struct.new(:name, :url, :snapshot)
LocalJar = Struct.new(:group_id, :artifact_id, :version, :path) do
# a system-scoped Jars.lock entry: "group:artifact:version:system:path"
def to_lock_entry
"#{group_id}:#{artifact_id}:#{version}:system:#{path}"
end
end

def initialize
@artifacts = []
@repositories = []
@local_jars = []
end

attr_reader :artifacts, :repositories, :local_jars

def eval_file(file)
instance_eval(File.read(file), file.to_s)
self
end

# -- DSL --

def jar(*args, &block)
add(:jar, args, &block)
end

def pom(*args, &block)
add(:pom, args, &block)
end

def scope(scope)
previous = @scope
@scope = scope
yield if block_given?
ensure
@scope = previous
end

def repository(name, url = nil)
@repositories << Repository.new(name.to_s, url || name, false)
end
alias_method :source, :repository

def snapshot_repository(name, url = nil)
@repositories << Repository.new(name.to_s, url || name, true)
end

# a local jar on disk - carried through as a system-scoped dependency
def local(path)
abs = File.expand_path(path)
name = File.basename(abs).sub(/\.jar$/, '')
if (dash = name.rindex('-'))
version = name[(dash + 1)..]
name = name[0...dash]
else
version = '0'
end
@local_jars << LocalJar.new('ruby.maven-tools.jar', name, version, abs)
end

# @deprecated in maven-tools, kept as a no-op so existing Jarfiles still load
def jruby(*)
nil
end

# Normalizes a DSL exclusion list ('group:artifact' or ['group', 'artifact']) to coordinate strings
def self.normalize_exclusions(list)
return [] if list.nil?

list.map { |e| e.is_a?(Array) ? e.join(':') : e.to_s }
end

private

def add(type, args, &block)
options = args.last.is_a?(Hash) ? args.pop : {}
group_id, artifact_id, classifier, version = coordinate(args)

config = DepConfig.new(options)
config.instance_eval(&block) if block

classifier ||= config.classifier
scope = (config.scope || @scope)&.to_s
exclusions = build_exclusions(config.exclusions)
@artifacts << GemspecArtifacts::Artifact.new(
type.to_s, group_id, artifact_id, classifier, version, exclusions, scope: scope
)
end

# Normalizes the positional coordinate forms into [group, artifact, classifier, version].
# Mirrors maven-tools Artifact.from: the colon-packed string carries the classifier.
def coordinate(args)
case args.size
when 1
# 'group:artifact:version' or 'group:artifact:version:classifier'
group_id, artifact_id, version, classifier = args[0].split(':')
when 2
# 'group:artifact', 'version' or 'group:artifact:classifier', 'version'
group_id, artifact_id, classifier = args[0].split(':')
version = args[1]
when 3
if args[0].include?(':')
# 'group:artifact[:classifier]', version-constraint, version-constraint
group_id, artifact_id, classifier = args[0].split(':')
version = args[1..].join(', ')
else
# 'group', 'artifact', 'version'
group_id, artifact_id, version = args
end
end
[group_id, artifact_id, classifier, version]
end

# @param coords [Array<String>] normalized 'group:artifact' exclusion coordinates
def build_exclusions(coords)
return nil if coords.nil? || coords.empty?

GemspecArtifacts::Exclusions.new("[#{coords.join(', ')}]")
end

# Collects scope / classifier / exclusions declared in a jar/pom block, seeded by inline options.
# scope / classifier / exclusions double as DSL setters (with a value) and readers (no value).
class DepConfig
def initialize(options)
@scope = options[:scope]
@classifier = options[:classifier]
@exclusions = Jarfile.normalize_exclusions(options[:exclusions])
end

# dual getter (no arg, read after eval) / setter (DSL call with a value)
def scope(value = nil)
value.nil? ? @scope : (@scope = value)
end

def classifier(value = nil)
value.nil? ? @classifier : (@classifier = value)
end

def exclusion(*args, &block)
@exclusions << ExclusionBuilder.coordinate(*args, &block)
end

def exclusions(*list, &block)
list.each { |e| @exclusions << ExclusionBuilder.coordinate(e) }
instance_eval(&block) if block
@exclusions
end
end

# Resolves one exclusion declaration (any DSL form) to a 'group:artifact' coordinate.
module ExclusionBuilder
def self.coordinate(*args, &block)
return from_block(args, &block) if block

case args.first
when Hash then "#{args.first[:group_id]}:#{args.first[:artifact_id]}"
when Array then args.first.join(':')
else args.join(':') # 'group:artifact', or 'group', 'artifact'
end
end

def self.from_block(args, &block)
receiver = Receiver.new
receiver.group_id(args.first) if args.first.is_a?(String)
receiver.instance_eval(&block)
"#{receiver.group_id}:#{receiver.artifact_id}"
end

# Receiver for the `exclusion do group_id ..; artifact_id .. end` form
class Receiver
def group_id(value = nil)
value.nil? ? @group_id : (@group_id = value)
end

def artifact_id(value = nil)
value.nil? ? @artifact_id : (@artifact_id = value)
end
end
end
end
end
Loading
Loading