Class: JBundler::Pom

Inherits:
Object
  • Object
show all
Includes:
Maven::Tools::Coordinate
Defined in:
lib/jbundler/pom.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, version, deps, packaging = nil) ⇒ Pom

Returns a new instance of Pom


72
73
74
75
76
77
78
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jbundler/pom.rb', line 72

def initialize(name, version, deps, packaging = nil)
  unless defined? XMLOutputFactory
    java_imports
  end

  @name = name
  @packaging = packaging || 'jar'
  @version = version

  @file = File.join(temp_dir, 'pom.xml')

  out = java.io.BufferedOutputStream.new(java.io.FileOutputStream.new(@file.to_java))
  outputFactory = XMLOutputFactory.newFactory()
  xmlStreamWriter = outputFactory.createXMLStreamWriter(out)
  xmlStreamWriter.writeStartDocument
  xmlStreamWriter.writeStartElement("project")
  
  writeElement(xmlStreamWriter,"modelVersion","4.0.0")
  writeElement(xmlStreamWriter,"groupId", GROUP_ID)
  writeElement(xmlStreamWriter,"artifactId", name)
  writeElement(xmlStreamWriter,"version", version.to_s.to_java)
  writeElement(xmlStreamWriter,"packaging", packaging) if packaging
  
  xmlStreamWriter.writeStartElement("dependencies".to_java)
  
  deps.each do |line|
    if coord = to_coordinate(line)
      coords = coord.split(/:/)
      group_id = coords[0]
      artifact_id = coords[1]
      extension = coords[2]
      classifier = nil
      if coords.size == 4
        version = coords[3]
      else
        classifier = coords[3]
        version = coords[4]
      end

      xmlStreamWriter.writeStartElement("dependency".to_java)
      writeElement(xmlStreamWriter,"groupId", group_id)
      writeElement(xmlStreamWriter,"artifactId", artifact_id)
      writeElement(xmlStreamWriter,"version", version)
      
      writeElement(xmlStreamWriter,"type", extension) if extension != 'jar'
      writeElement(xmlStreamWriter,"classifier", classifier) if classifier
      xmlStreamWriter.writeEndElement #dependency
    end
  end
  xmlStreamWriter.writeEndElement #dependencies
  
  xmlStreamWriter.writeEndElement #project
  
  xmlStreamWriter.writeEndDocument
  xmlStreamWriter.close
  out.close
end

Instance Method Details

#coordinateObject


64
65
66
# File 'lib/jbundler/pom.rb', line 64

def coordinate
  @coord ||= "#{GROUP_ID}:#{@name}:#{@packaging}:#{@version}"
end

#fileObject


68
69
70
# File 'lib/jbundler/pom.rb', line 68

def file
  @file
end