From 343c87502dc42b08be08db990f24ba0e03af48e6 Mon Sep 17 00:00:00 2001 From: Tieg Zaharia Date: Wed, 18 Jan 2023 09:35:28 -0700 Subject: [PATCH] Maven.extract_pom_info: add support for more lookup variable namespaces. (#567) * Maven.extract_pom_info: add support for more lookup variables. * Add a TODO to improve maven's parent pom inheritance. --- lib/bibliothecary/parsers/maven.rb | 8 +++++++- lib/bibliothecary/version.rb | 2 +- spec/parsers/maven_spec.rb | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/bibliothecary/parsers/maven.rb b/lib/bibliothecary/parsers/maven.rb index a081f24a..463d1c94 100644 --- a/lib/bibliothecary/parsers/maven.rb +++ b/lib/bibliothecary/parsers/maven.rb @@ -322,6 +322,9 @@ def self.extract_pom_info(xml, location, parent_properties = {}) extract_pom_dep_info(xml, xml, location, parent_properties) end + # TODO: it might be worth renaming parent_properties to parent_elements + # so that more can be inherited from the parent pom than just + # here (see https://maven.apache.org/pom.html#inheritance) def self.extract_pom_dep_info(xml, dependency, name, parent_properties = {}) field = dependency.locate(name).first return nil if field.nil? @@ -363,7 +366,10 @@ def self.property_value(xml, property_name, parent_properties) return "${#{property_name}}" if !xml.respond_to?("properties") && parent_properties.empty? && xml.locate(non_prop_name).empty? prop_field = xml.properties.locate(property_name).first if xml.respond_to?("properties") - parent_prop = parent_properties[property_name] + parent_prop = parent_properties[property_name] || # e.g. "${foo}" + parent_properties[property_name.sub(/^project\./, '')] || # e.g. "${project.foo}" + parent_properties[property_name.sub(/^project\.parent\./, '')] # e.g. "${project.parent.foo}" + if prop_field prop_field.nodes.first elsif parent_prop diff --git a/lib/bibliothecary/version.rb b/lib/bibliothecary/version.rb index 4a28e562..9a7f8dd5 100644 --- a/lib/bibliothecary/version.rb +++ b/lib/bibliothecary/version.rb @@ -1,3 +1,3 @@ module Bibliothecary - VERSION = "8.5.0" + VERSION = "8.5.1" end diff --git a/spec/parsers/maven_spec.rb b/spec/parsers/maven_spec.rb index d67b2f76..aa1c2dad 100644 --- a/spec/parsers/maven_spec.rb +++ b/spec/parsers/maven_spec.rb @@ -323,7 +323,7 @@ end describe 'parent properties' do - it 'totally ignores parent propes' do + it 'totally ignores parent props' do parent_props = {} deps = described_class.parse_pom_manifest(load_fixture('pom.xml'), parent_props) @@ -352,6 +352,21 @@ bibliothecary_dep = deps.find { |dep| dep[:name] == "io.libraries:bibliothecary" } expect(bibliothecary_dep[:requirement]).to eq("9.9.9") end + + it "can extract parent properties specified with a lookup prefix during resolve" do + parent_props = { "scm.url"=>"scm:git:git@github.com:accidia/echo.git" } + + # Esnure that all of these lookup variations resolve to the parent's relevant property. + ["project.parent.scm.url", "project.scm.url", "scm.url"].each do |lookup_var| + xml = Ox.parse(%Q! + + + ${#{lookup_var}} + !) + scm_url = described_class.extract_pom_info(xml, "project/scm/url", parent_props) + expect(scm_url).to eq("scm:git:git@github.com:accidia/echo.git") + end + end end it 'returns property name for missing property values' do