From 235e3c2225494c85f5913c2b5c23b0bca7b42e5c Mon Sep 17 00:00:00 2001 From: kares Date: Thu, 15 Oct 2026 11:35:06 +0200 Subject: [PATCH 1/2] [fix] honor mvn classifier when present (#117) classpath paths from Jars.lock used classifier before version --- README.md | 7 +++++++ lib/jars/classpath.rb | 8 ++++---- lib/jars/lock.rb | 13 +++++++++---- specs/classpath_spec.rb | 29 ++++++++++++++++++++++++++--- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a2b7477..f7b5a81 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,13 @@ And look at the dependencies tree lock_jars --tree ``` +Classpaths built from `Jars.lock` use Maven's `artifact-version-classifier.jar` filename for classified artifacts. +To keep the previous `artifact-classifier-version.jar` output, pass `legacy_classifier: true`: + +```ruby +Jars::Classpath.new.classpath(legacy_classifier: true) +``` + 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 diff --git a/lib/jars/classpath.rb b/lib/jars/classpath.rb index a6d6732..6a6e2ff 100644 --- a/lib/jars/classpath.rb +++ b/lib/jars/classpath.rb @@ -68,10 +68,10 @@ def require(scope = nil) end end - def classpath(scope = nil) + def classpath(scope = nil, legacy_classifier: false) classpath = [] process(scope) do |jar| - classpath << jar.file + classpath << jar.file(legacy_classifier: legacy_classifier) end classpath end @@ -85,8 +85,8 @@ def process(scope, &block) end private :process - def classpath_string(scope = nil) - classpath(scope).join(File::PATH_SEPARATOR) + def classpath_string(scope = nil, legacy_classifier: false) + classpath(scope, legacy_classifier: legacy_classifier).join(File::PATH_SEPARATOR) end end end diff --git a/lib/jars/lock.rb b/lib/jars/lock.rb index c4fad5e..10ba611 100644 --- a/lib/jars/lock.rb +++ b/lib/jars/lock.rb @@ -8,9 +8,9 @@ def scope self[-2].to_sym end - def file + def file(legacy_classifier: false) file = self[-1].strip - file.empty? ? path : file + file.empty? ? path(legacy_classifier: legacy_classifier) : file end def group_id @@ -35,14 +35,19 @@ def gacv classifier ? self[0..3] : self[0..2] end - def path + def path(legacy_classifier: false) if scope == :system # replace maven like system properties embedded into the string self[-1].gsub(/\$\{[a-zA-Z.]+\}/) do |a| ENV_JAVA[a[2..-2]] || a end else - File.join(Jars.home, group_id.gsub(/[.]/, '/'), artifact_id, version, "#{gacv[1..].join('-')}.jar") + filename = if classifier && !legacy_classifier + "#{artifact_id}-#{version}-#{classifier}.jar" + else + "#{gacv[1..].join('-')}.jar" + end + File.join(Jars.home, group_id.gsub('.', '/'), artifact_id, version, filename) end end end diff --git a/specs/classpath_spec.rb b/specs/classpath_spec.rb index a2eee99..51c49ae 100644 --- a/specs/classpath_spec.rb +++ b/specs/classpath_spec.rb @@ -3,6 +3,7 @@ require File.expand_path('setup', File.dirname(__FILE__)) require 'yaml' +require 'tmpdir' module Helper def self.prepare!(array) @@ -85,14 +86,14 @@ def self.prepare(array) ['org/apache/maven/maven-repository-metadata/3.1.0/maven-repository-metadata-3.1.0.jar', 'com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar', 'org/apache/httpcomponents/httpclient/4.2.3/httpclient-4.2.3.jar', - 'org/sonatype/sisu/sisu-guice/3.1.0/sisu-guice-no_aop-3.1.0.jar', + 'org/sonatype/sisu/sisu-guice/3.1.0/sisu-guice-3.1.0-no_aop.jar', '${java.home}/../lib/tools.jar', 'org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar'] end let(:lock_expected_runtime) do ['org/apache/maven/maven-repository-metadata/3.1.0/maven-repository-metadata-3.1.0.jar', - 'org/sonatype/sisu/sisu-guice/3.1.0/sisu-guice-no_aop-3.1.0.jar', + 'org/sonatype/sisu/sisu-guice/3.1.0/sisu-guice-3.1.0-no_aop.jar', '${java.home}/../lib/tools.jar', 'org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar'] end @@ -101,7 +102,7 @@ def self.prepare(array) ['org/apache/maven/maven-repository-metadata/3.1.0/maven-repository-metadata-3.1.0.jar', 'com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar', 'org/apache/httpcomponents/httpclient/4.2.3/httpclient-4.2.3.jar', - 'org/sonatype/sisu/sisu-guice/3.1.0/sisu-guice-no_aop-3.1.0.jar', + 'org/sonatype/sisu/sisu-guice/3.1.0/sisu-guice-3.1.0-no_aop.jar', '${java.home}/../lib/tools.jar', 'org/slf4j/slf4j-api/1.6.2/slf4j-api-1.6.2.jar', 'org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar'] @@ -207,6 +208,28 @@ def self.prepare(array) _(Helper.prepare(subject.classpath(:runtime))).must_equal Helper.prepare(lock_expected_runtime) end + it 'uses Maven classifier paths by default and permits legacy order' do + Dir.mktmpdir do |home| + lock = File.join(home, 'Jars.lock') + File.write(lock, "org.xmlresolver:xmlresolver:data:5.3.3:compile:\n") + dir = File.join(home, 'org/xmlresolver/xmlresolver/5.3.3') + maven_path = File.join(dir, 'xmlresolver-5.3.3-data.jar') + legacy_path = File.join(dir, 'xmlresolver-data-5.3.3.jar') + + Jars.stub(:home, home) do + classpath = Jars::Classpath.new(nil, lock) + _(classpath.classpath).must_equal [maven_path] + _(classpath.classpath(legacy_classifier: true)).must_equal [legacy_path] + _(classpath.classpath_string).must_equal maven_path + + explicit_path = File.join(home, 'explicit.jar') + File.binwrite(lock, "org.xmlresolver:xmlresolver:data:5.3.3:compile:#{explicit_path}\n") + _(classpath.classpath).must_equal [explicit_path] + _(classpath.classpath(legacy_classifier: true)).must_equal [explicit_path] + end + end + end + it 'processes Jars.lock and block loading of jars' do subject.instance_variable_set(:@deps, jars_no_jline_lock) From 1ac28621f3b879e7161aecef271dc6f19bb97551 Mon Sep 17 00:00:00 2001 From: kares Date: Thu, 15 Oct 2026 15:57:43 +0200 Subject: [PATCH 2/2] [refactor] use String#tr to satisfy rubocop --- lib/jars/lock.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jars/lock.rb b/lib/jars/lock.rb index 10ba611..b23fa6f 100644 --- a/lib/jars/lock.rb +++ b/lib/jars/lock.rb @@ -47,7 +47,7 @@ def path(legacy_classifier: false) else "#{gacv[1..].join('-')}.jar" end - File.join(Jars.home, group_id.gsub('.', '/'), artifact_id, version, filename) + File.join(Jars.home, group_id.tr('.', '/'), artifact_id, version, filename) end end end