Contents

Chef. Get latest version from maven metadata in Nexus

Contents

Get latest version number from maven-metadata.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
require 'nokogiri'
require 'open-uri'
require 'uri'

# Expect you have metadata maven-metadata.xml
url = 'https://mynexus/nexus/content/repositories/my-repo/com/myorg/my-unit/my-app/maven-metadata.xml'
version_number = get_version_from_nexus(url)

def get_version_from_nexus(meta_url)
  doc = Nokogiri::HTML(open(meta_url))
  responce = doc.css('metadata').css('versioning').css('release').text
  if responce.empty?
    responce = doc.css('metadata').css('versioning').css('latest').text
  end
  responce.to_s
end

Get version number from local path

1
2
3
4
5
6
7
8
9
# Dir /tmp/app_name/1.2.3.4/files
version = get_version_from_path('/tmp/app_name/')

def get_version_from_path(path)
  if Dir.exists(path)
    Dir.entries(path).max
  else
    '0.0.0.0'
end