Class: Maven

Inherits:
Object
  • Object
show all
Defined in:
lib/it_tools/maven.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_string, options = {}) ⇒ Maven

Returns a new instance of Maven.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/it_tools/maven.rb', line 7

def initialize(xml_string, options = {})
  defaults = { :jar_with_dependencies => false, :debug => true }
  options = defaults.merge(options)
  @ops = options
  @pom = XmlSimple.xml_in xml_string
  @re = SharedTool::RegularExpression.new
  @log = Logger.new 'log.txt'
  if level = @ops[:debug_level]
    @log.level = level
  else
    @log.level = Logger::INFO
  end
  process_pom
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



6
7
8
# File 'lib/it_tools/maven.rb', line 6

def log
  @log
end

#opsObject

Returns the value of attribute ops.



6
7
8
# File 'lib/it_tools/maven.rb', line 6

def ops
  @ops
end

#pomObject

Returns the value of attribute pom.



6
7
8
# File 'lib/it_tools/maven.rb', line 6

def pom
  @pom
end

#reObject

Returns the value of attribute re.



6
7
8
# File 'lib/it_tools/maven.rb', line 6

def re
  @re
end

Instance Method Details

#build(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/it_tools/maven.rb', line 27

def build(options = {})
  defaults = { :jar_with_dependencies => false, :environment_specific => false }
  options = defaults.merge(options)
  @ops = @ops.merge(options)
  build_command = "mvn package"
  if @ops[:jar_with_dependencies]
    build_command = "mvn assembly:assembly"
  end
  if @ops[:env]
    puts "WARNING: You are building an environment specific artifact, use caution."
    profile = get_profile_name(@ops[:env])
    build_command += " -P#{profile}"
  end
  if ops[:dry_run] 
    puts "[would run command]: " + build_command
  else
    system(build_command) 
  end
end

#get_artifact_name(xml_string) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/it_tools/maven.rb', line 66

def get_artifact_name(xml_string)
  artifactId = get_artifactId(xml_string)
  packaging = get_packaging(xml_string)
  if ops[:jar_with_dependencies]
    return artifactId + "-jar-with-dependencies." + packaging
  else
    return artifactId + "." + packaging
  end
end

#get_artifactId(xml_string) ⇒ Object



58
59
60
# File 'lib/it_tools/maven.rb', line 58

def get_artifactId(xml_string)
  artifactId = re.first_occurrence( xml_string, /<artifactId>(.*)<\/artifactId>/)
end

#get_built_artifact_name_with_version(xml_string) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/it_tools/maven.rb', line 82

def get_built_artifact_name_with_version(xml_string)
  version = @ops[:version]
  prev_ver = get_prev_version(version)
  artifactId = get_artifactId(xml_string)
  packaging = get_packaging(xml_string)
  # puts "Dumping class: " + self.inspect if ops[:debug]

  if ops[:jar_with_dependencies]
    return "#{artifactId}-jar-with-dependencies-#{prev_ver}.#{packaging}"
  else
    return "#{artifactId}-#{prev_ver}.#{packaging}"
  end
end

#get_packaging(xml_string) ⇒ Object



62
63
64
# File 'lib/it_tools/maven.rb', line 62

def get_packaging(xml_string)
  packaging = re.first_occurrence( xml_string, /<packaging>(.*)<\/packaging>/)
end

#get_prev_version(curr_version) ⇒ Object



96
97
98
99
# File 'lib/it_tools/maven.rb', line 96

def get_prev_version(curr_version)
  m = /(\d+\.\d+\.)(\d+)/.match(curr_version)
  m[1].to_s + (m[2].to_i - 1).to_s
end

#get_profile_name(environment) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/it_tools/maven.rb', line 47

def get_profile_name(environment)
  profile = {
    'loc' => 'loc',
    'dev'  => 'dev',
    'stg'  => 'stage',
    'prod' => 'prod'
  }
  return profile[environment]
end

#has_assemblyObject



76
77
78
79
80
81
# File 'lib/it_tools/maven.rb', line 76

def has_assembly
  plugins = @pom['build']['pluginManagement']['plugins']
  plugins.each do |key,value|
    p key
  end
end

#process_pomObject



21
22
23
24
25
# File 'lib/it_tools/maven.rb', line 21

def process_pom
  @ops[:artifactId] = @pom['artifactId'][0]
  @ops[:version] = @pom['version'][0]
  @ops[:prev_version] = get_prev_version @ops[:version]
end