Skip to content

Commit

Permalink
Merge pull request #456 from librariesio/mikeyoung85/handle-parent-props
Browse files Browse the repository at this point in the history
Allow Additional Properties to be Added to maven parse_pom_manifest
  • Loading branch information
mikeyoung85 authored Jun 10, 2019
2 parents 28ecfb6 + d9ba077 commit 010fb70
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
26 changes: 20 additions & 6 deletions lib/bibliothecary/parsers/maven.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ def self.parse_gradle_resolved(file_contents)
end.compact.uniq {|item| [item[:name], item[:requirement], item[:type]]}
end

def self.parse_pom_manifest(file_contents)
def self.parse_pom_manifest(file_contents, parent_properties = {})
manifest = Ox.parse file_contents
xml = manifest.respond_to?('project') ? manifest.project : manifest
[].tap do |deps|
['dependencies/dependency', 'dependencyManagement/dependencies/dependency'].each do |deps_xpath|
xml.locate(deps_xpath).each do |dep|
deps.push({
name: "#{extract_pom_dep_info(xml, dep, 'groupId')}:#{extract_pom_dep_info(xml, dep, 'artifactId')}",
requirement: extract_pom_dep_info(xml, dep, 'version'),
type: extract_pom_dep_info(xml, dep, 'scope') || 'runtime'
name: "#{extract_pom_dep_info(xml, dep, 'groupId', parent_properties)}:#{extract_pom_dep_info(xml, dep, 'artifactId', parent_properties)}",
requirement: extract_pom_dep_info(xml, dep, 'version', parent_properties),
type: extract_pom_dep_info(xml, dep, 'scope', parent_properties) || 'runtime'
})
end
end
Expand All @@ -141,16 +141,30 @@ def self.parse_gradle(manifest)
end.compact
end

def self.extract_pom_dep_info(xml, dependency, name)
def self.extract_pom_info(xml, location, parent_properties = {})
extract_pom_dep_info(xml, xml, location, parent_properties)
end

def self.extract_pom_dep_info(xml, dependency, name, parent_properties = {})
field = dependency.locate(name).first
return nil if field.nil?
value = field.nodes.first
match = value.match(/^\$\{(.+)\}/)
if match
return value unless xml.respond_to? 'properties'
# the xml root is <project> so lookup the non property name in the xml
# this converts ${project/group.id} -> ${group/id}
non_prop_name = match[1].gsub('.', '/').gsub('project/', '')
return value if !xml.respond_to?('properties') && parent_properties.empty? && !xml.locate(non_prop_name)
prop_field = xml.properties.locate(match[1]).first
parent_prop = parent_properties[match[1]]
if prop_field
return prop_field.nodes.first
elsif parent_prop
return parent_prop
elsif xml.locate(non_prop_name).first
# see if the value to look up is a field under the project
# examples are ${project.groupId} or ${project.version}
return xml.locate(non_prop_name).first.nodes.first
else
return value
end
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 = "6.7.1"
VERSION = "6.7.2"
end
11 changes: 11 additions & 0 deletions spec/fixtures/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>echo-parent</artifactId>
<version>${project.version}</version>
</dependency>
<!--JERSEY SERVLET-->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
Expand Down Expand Up @@ -261,6 +266,12 @@
<version>1.8.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.libraries</groupId>
<artifactId>bibliothecary</artifactId>
<version>${bibliothecary.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
15 changes: 14 additions & 1 deletion spec/parsers/maven_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
:platform=>"maven",
:path=>"pom.xml",
:dependencies=>[
{:name=>"org.accidia:echo-parent",
:requirement=>"0.1.23",
:type=>"runtime"},
{:name=>"org.glassfish.jersey.core:jersey-server",
:requirement=>"2.16",
:type=>"runtime"},
Expand Down Expand Up @@ -80,9 +83,10 @@
{:name=>"com.typesafe:config", :requirement=>"1.2.1", :type=>"runtime"},
{:name=>"org.testng:testng", :requirement=>"6.8.7", :type=>"test"},
{:name=>"org.mockito:mockito-all", :requirement=>"1.8.4", :type=>"test"},
{:name=>"io.libraries:bibliothecary", :requirement=>"${bibliothecary.version}", :type=>"test"},
# From dependencyManagement section
{:name=>"org.apache.ant:ant", :requirement=>"1.9.2", :type=>"runtime"},
{:name=>"commons-lang:commons-lang",:requirement=>"2.6", :type=>"runtime"}
{:name=>"commons-lang:commons-lang",:requirement=>"2.6", :type=>"runtime"},
],
kind: 'manifest',
success: true
Expand Down Expand Up @@ -301,4 +305,13 @@
expect(test_compile_classpath.length).to eq 187
expect(test_runtime_classpath.select {|item| item[:name] == "org.slf4j:jul-to-slf4j"}.length).to eq 1
end

it 'uses parent properties during resolve' do
parent_props = {"bibliothecary.version"=>"9.9.9"}
deps = described_class.parse_pom_manifest(load_fixture('pom.xml'), parent_props)
jersey_dep = deps.find do |dep|
dep[:name] == "io.libraries:bibliothecary"
end
expect(jersey_dep[:requirement]).to eq "9.9.9"
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ def load_fixture(name)
c.configure_rspec_metadata!
c.hook_into :webmock
end

require 'pry'

0 comments on commit 010fb70

Please sign in to comment.