Skip to content

Commit

Permalink
Maven.extract_pom_info: add support for more lookup variable namespac…
Browse files Browse the repository at this point in the history
…es. (#567)

* Maven.extract_pom_info: add support for more lookup variables.

* Add a TODO to improve maven's parent pom inheritance.
  • Loading branch information
tiegz authored Jan 18, 2023
1 parent 6e4920d commit 343c875
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/bibliothecary/parsers/maven.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <properties>
# 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?
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/bibliothecary/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bibliothecary
VERSION = "8.5.0"
VERSION = "8.5.1"
end
17 changes: 16 additions & 1 deletion spec/parsers/maven_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:[email protected]: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!
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<scm><url>${#{lookup_var}}</url></scm>
</project>!)
scm_url = described_class.extract_pom_info(xml, "project/scm/url", parent_props)
expect(scm_url).to eq("scm:git:[email protected]:accidia/echo.git")
end
end
end

it 'returns property name for missing property values' do
Expand Down

0 comments on commit 343c875

Please sign in to comment.