Class: Buildr::POM

Inherits:
Object show all
Defined in:
lib/buildr/java/pom.rb

Constant Summary collapse

POM_TO_SPEC_MAP =
{ :group=>"groupId", :id=>"artifactId", :type=>"type",
:version=>"version", :classifier=>"classifier", :scope=>"scope" }
SCOPES_TRANSITIVE =
[nil, "compile", "runtime"]
SCOPES_WE_USE =
SCOPES_TRANSITIVE + ["provided"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ POM

:nodoc:



68
69
70
71
# File 'lib/buildr/java/pom.rb', line 68

def initialize(xml) #:nodoc:
  @project = XmlSimple.xml_in(xml)
  @parent = POM.load(pom_to_hash(project["parent"].first).merge(:type=>'pom')) if project['parent']
end

Instance Attribute Details

#parentObject (readonly)

Parent POM if referenced by this POM.



28
29
30
# File 'lib/buildr/java/pom.rb', line 28

def parent
  @parent
end

#projectObject (readonly)

POM project as Hash (using XmlSimple).



26
27
28
# File 'lib/buildr/java/pom.rb', line 26

def project
  @project
end

Class Method Details

.load(source) ⇒ Object

:call-seq:

POM.load(arg)

Load new POM object form various kind of sources such as artifact, hash representing spec, filename, XML.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/buildr/java/pom.rb', line 36

def load(source)
  case source
  when Hash
    load(Buildr.artifact(source).pom)
  when Artifact
    pom = source.pom
    pom.invoke
    load(pom.to_s)
  when Rake::FileTask
    source.invoke
    load(source.to_s)
  when String
    filename = File.expand_path(source)
    unless pom = cache[filename]
      trace "Loading m2 pom file from #{filename}"
      pom = POM.new(IO.read(filename))
      cache[filename] = pom
    end
    pom
  else
    raise ArgumentError, "Expecting Hash spec, Artifact, file name or file task"
  end
end

Instance Method Details

#dependencies(scopes = SCOPES_WE_USE) ⇒ Object

:call-seq:

dependencies(scopes?) => artifacts

Returns list of required dependencies as specified by the POM. You can specify which scopes to use (e.g. “compile”, “runtime”); use nil for dependencies with unspecified scope. The default scopes are nil, “compile” and “runtime” (aka SCOPES_WE_USE).



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/buildr/java/pom.rb', line 79

def dependencies(scopes = SCOPES_WE_USE)
  #try to cache dependencies also
  @depends_for_scopes ||= {}
  unless depends = @depends_for_scopes[scopes]
    declared = project["dependencies"].first["dependency"] rescue nil
    depends = (declared || []).reject { |dep| value_of(dep["optional"]) =~ /true/ }.
      map { |dep| 
        spec = pom_to_hash(dep, properties) 
        apply = managed(spec)
        spec = apply.merge(spec) if apply

        #calculate transitive dependencies
        if scopes.include?(spec[:scope])
          spec.delete(:scope)

          exclusions = dep["exclusions"]["exclusion"] rescue nil
          transitive_deps = POM.load(spec).dependencies(SCOPES_TRANSITIVE)
          transitive_deps = transitive_deps.reject{|dep| 
            exclusions.find {|ex| dep.index("#{dep['groupdId'].first}:#{dep['artifactId'].first}:") == 0}
          } if exclusions

          [Artifact.to_spec(spec)] + transitive_deps
        end
      }.flatten.compact #.uniq_by{|spec| art = spec.split(':'); "#{art[0]}:#{art[1]}"}

    @depends_for_scopes[scopes] = depends
  end
  depends
end

#managed(spec = nil) ⇒ Object

:call-seq:

managed() => hash
managed(hash) => hash

The first form returns all the managed dependencies specified by this POM in dependencyManagement. The second form uses a single spec hash and expands it from the current/parent POM. Used to determine the version number if specified in dependencyManagement instead of dependencies.



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/buildr/java/pom.rb', line 134

def managed(spec = nil)
  if spec
    managed.detect { |dep| [:group, :id, :type, :classifier].all? { |key| spec[key] == dep[key] } } ||
      (parent ? parent.managed(spec) : nil)
  else
    @managed ||= begin
      managed = project["dependencyManagement"].first["dependencies"].first["dependency"] rescue nil
      managed ? managed.map { |dep| pom_to_hash(dep, properties) } : []
    end
  end
end

#propertiesObject

:call-seq:

properties() => hash

Returns properties available to this POM as hash. Includes explicit properties and pom.xxx/project.xxx properties for groupId, artifactId, version and packaging.



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/buildr/java/pom.rb', line 114

def properties()
  @properties ||= begin
    pom = ["groupId", "artifactId", "version", "packaging"].inject({}) { |hash, key|
      value = project[key] || (parent ? parent.project[key] : nil)
      hash["pom.#{key}"] = hash["project.#{key}"] = value_of(value) if value
      hash
    }
    props = project["properties"].first rescue {}
    props = props.inject({}) { |mapped, pair| mapped[pair.first] = value_of(pair.last, pom) ; mapped }
    (parent ? parent.properties.merge(props) : props).merge(pom)
  end
end