Class: PluginManagementHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mavenReactorService/PluginManagementHandler.rb

Constant Summary collapse

@@reactorHandler =
ReactorHandler.new
@@dependencyHandler =
DependencyHandler.new
@@mvnReactorization =
MvnReactorization.new

Instance Method Summary collapse

Instance Method Details

#addVersionOfMergedList(pom_document, mergedList) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 86

def addVersionOfMergedList(pom_document,mergedList)
  filterMergedList = Array.new
  mergedList.each do |eachEntry|
    pluginNode =  pom_document.css ("project/build/plugins/plugin")
    pluginManagementNode =  pom_document.css ("project/build/pluginManagement/plugins/plugin")
    plugins = findVersionFromPlugin(eachEntry,pluginNode,pom_document)
    if plugins.nil?
      plugins = findVersionFromPlugin(eachEntry,pluginManagementNode,pom_document)
    end
    filterMergedList.push(plugins)
  end
  return filterMergedList
end

#comparePluginManagementFromChildModule(project_directory_path) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 219

def comparePluginManagementFromChildModule(project_directory_path)
  pomArr =  @@reactorHandler.fetchGidArtifactIdAndVersionFromChildModule(project_directory_path,false)
  if  !(pomArr.nil? || pomArr.empty?)
    pomArr.each do |eachPomPath|
      pom_document =  @@reactorHandler.parse_xml_from_file(eachPomPath)
      pom_document.css("project/build/pluginManagement/plugins/plugin").each do |eachPlugin|
        eachPlugin.children.each do |eachNode|
          if !(eachNode.name == "groupId" or eachNode.name == "artifactId" or eachNode.name == "version" or eachNode.name == "text")
            grpId=nil
            if (eachPlugin.at("groupId").nil? or eachPlugin.at("groupId").parent.name != "plugin")
              grpId="org.apache.maven.plugins"
            else
              grpId = eachPlugin.at("groupId").text
            end
            artifactId = eachPlugin.at("artifactId").text
            versionNode = eachPlugin.at("version")
            cordinate = "#{grpId}:#{artifactId}"
            searchCommonPluginInPlugins(eachPomPath,pom_document,cordinate,eachNode,versionNode)
          end
        end
      end
    end
  end
end

#copyPluginFromPluginManagement(pom_document, eachPlugin) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 271

def copyPluginFromPluginManagement(pom_document,eachPlugin)
  grpId=nil
  if (eachPlugin.at("groupId").nil? or eachPlugin.at("groupId").parent.name != "plugin")
    grpId="org.apache.maven.plugins"
  else
    grpId = eachPlugin.at("groupId").text
  end
  artifactId = eachPlugin.at("artifactId").text
  cordinate = "#{grpId}:#{artifactId}"
  searchPluginInPlugins(pom_document,cordinate)
end

#executeShortPomObject



385
386
387
388
389
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 385

def executeShortPom()
  result =  `mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort`
  puts result
  puts "All poms are sorted by sort-pom plugin"
end

#findAllPluginsOfAProject(pom_document, tag) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 45

def findAllPluginsOfAProject(pom_document,tag)
  commonPluginArr = Array.new
  pom_document.css(tag).each do |eachPlugin|
    grpId=nil
    if (eachPlugin.at("groupId").nil?)
      grpId="org.apache.maven.plugins"
    else
      grpId = eachPlugin.at("groupId").text
    end
    artifactId =  eachPlugin.at("artifactId").text
    version=nil
    if (!eachPlugin.at("version").nil? and eachPlugin.at("version").parent.name=="plugin")
      version =  eachPlugin.at("version").text
    end
    fullArtifactId = "#{grpId}:#{artifactId}:#{version}"
    if !(commonPluginArr.include?fullArtifactId)
      commonPluginArr.push(fullArtifactId)
    end
  end
  return commonPluginArr
end

#findCommonPluginFromPluginManagement(project_directory_path) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 302

def findCommonPluginFromPluginManagement(project_directory_path)
  pomArr =  @@reactorHandler.fetchGidArtifactIdAndVersionFromChildModule(project_directory_path,false)
  if  !(pomArr.nil? || pomArr.empty?)
    pomArr.each do |eachPomPath|
      pluginList = Hash.new
      pom_document =  @@reactorHandler.parse_xml_from_file(eachPomPath)
      pom_document.css("project/build/pluginManagement/plugins/plugin").each do |eachPlugin|
        grpId=nil
        if (eachPlugin.at("groupId").nil? or eachPlugin.at("groupId").parent.name != "plugin")
          grpId="org.apache.maven.plugins"
        else
          grpId = eachPlugin.at("groupId").text
        end
        artifactId = eachPlugin.at("artifactId").text
        pluginCordinate = "#{grpId}:#{artifactId}"
        pluginList[pluginCordinate] = eachPlugin
      end
      pluginsList =  findCommonPluginFromPlugins(pom_document,pluginList)
      putPluginVersionIfNotFoundInPlugins(eachPomPath,pom_document,pluginList,"project/build/plugins/plugin")
      putPluginVersionIfNotFoundInPlugins(eachPomPath,pom_document,pluginList,"project/reporting/plugins/plugin")
      putUncommonPlugin(eachPomPath,pom_document,pluginList,pluginsList,"project/build/plugins")
      removePluginManagementTag(pom_document)
      @@mvnReactorization.write_nokogiri_to_xml(eachPomPath, pom_document)
    end
  end
end

#findCommonPluginFromPlugins(pom_document, pluginList) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 352

def findCommonPluginFromPlugins(pom_document,pluginList)
  pluginsList = Array.new
  pom_document.css("project/build/plugins/plugin").each do |eachPlugin|
    grpId=nil
    if (eachPlugin.at("groupId").nil? or eachPlugin.at("groupId").parent.name != "plugin")
      grpId="org.apache.maven.plugins"
    else
      grpId = eachPlugin.at("groupId").text
    end
    artifactId = eachPlugin.at("artifactId").text
    pluginCordinate = "#{grpId}:#{artifactId}"
    pluginsList.push(pluginCordinate)
  end
  return pluginsList
end

#findCommonPlugins(project_dir) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 12

def findCommonPlugins(project_dir)
  pomArr =  @@reactorHandler.fetchGidArtifactIdAndVersionFromChildModule(project_dir,false)
  if  !(pomArr.nil? || pomArr.empty?)
    allPluginList = Array.new
    allPluginManagementList = Array.new
    masterList = Array.new
    pomArr.each do |eachPomPath|
      pom_document =  @@reactorHandler.parse_xml_from_file(eachPomPath)
      pluginList = findAllPluginsOfAProject(pom_document,"project/build/plugins/plugin")
      allPluginList.push(pluginList.uniq)
      pluginManagementList = findAllPluginsOfAProject(pom_document,"project/build/pluginManagement/plugins/plugin")
      allPluginManagementList.push(pluginManagementList.uniq)
      mergedList =  mergePlugins(pluginList,pluginManagementList)
      filterList = addVersionOfMergedList(pom_document,mergedList)
      masterList.push(filterList.uniq)
    end
    commonDependencyList = findCommonPluginsFromMasterList(masterList)
    return commonDependencyList
  end
end

#findCommonPluginsFromMasterList(masterList) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 33

def findCommonPluginsFromMasterList(masterList)
  commonDependencyList = Array.new
  masterList.each_with_index do |eachPomDependencyList,index|
    if index==0
      commonDependencyList = eachPomDependencyList
    else
      commonDependencyList = commonDependencyList & eachPomDependencyList
    end
  end
  return commonDependencyList
end

#findPropValue(propertyVal, pom_document) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 124

def findPropValue(propertyVal,pom_document)
  pom_document.css("project/properties").children.each do |prop|
    propTag =  "${#{prop.name}}"
    if propertyVal == propTag
      return prop.text
    end
  end
end

#findVersionFromPlugin(eachEntry, nodeObj, pom_document) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 100

def findVersionFromPlugin(eachEntry,nodeObj,pom_document)
  cordinatedependency = nil
  nodeObj.each do |eachNode|
    grpId=nil
    if (eachNode.at("groupId").nil?)
      grpId="org.apache.maven.plugins"
    else
      grpId = eachNode.at("groupId").text
    end
    artifactId = eachNode.at("artifactId").text
    fullDependency = "#{grpId}:#{artifactId}"
    if fullDependency == eachEntry
      if (eachNode.at("version") and eachNode.at("version").parent.name == "plugin")
        version = eachNode.at("version").text
        if version.include?"${"
          version =  findPropValue(version,pom_document)
        end
        cordinatedependency = "#{fullDependency}:#{version}"
      end
    end
  end
  return cordinatedependency
end

#handlePluginManagement(project_dir) ⇒ Object



5
6
7
8
9
10
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 5

def handlePluginManagement(project_dir)
  commonPluginList = findCommonPlugins(project_dir)
  putCommonPluginInReactorPom(project_dir,commonPluginList)
  removePluginversionFromChildModule(project_dir,commonPluginList,"/plugins")
  removePluginversionFromChildModule(project_dir,commonPluginList,"/pluginManagement/")
end

#mergePlugins(allPluginList, allPluginManagementList) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 67

def mergePlugins(allPluginList,allPluginManagementList)
  pluginListWithoutVersion = Array.new
  pluginManagementListWithoutVersion = Array.new
  allPluginList.each do |eachPlugin|
    eachPluginArr = eachPlugin.split(":")
    cordinatePlugin = "#{eachPluginArr[0]}:#{eachPluginArr[1]}"
    pluginListWithoutVersion.push(cordinatePlugin)
  end
  allPluginManagementList.each do |eachPlugin|
    eachPluginArr = eachPlugin.split(":")
    cordinatePlugin = "#{eachPluginArr[0]}:#{eachPluginArr[1]}"
    pluginManagementListWithoutVersion.push(cordinatePlugin)
  end
  commonPluginList = pluginListWithoutVersion & pluginManagementListWithoutVersion
  uncommonPluginList = (pluginListWithoutVersion+pluginManagementListWithoutVersion) - (pluginListWithoutVersion&pluginManagementListWithoutVersion)
  mergedList = commonPluginList + uncommonPluginList
  return mergedList
end

#putCommonPluginInReactorPom(project_directory, commonList) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 133

def putCommonPluginInReactorPom(project_directory,commonList)
  pomPath = "#{project_directory}/pom.xml"
  pom_document =  @@reactorHandler.parse_xml_from_file(pomPath)
  nokObj = Nokogiri::XML::Node
  projectNode = pom_document.at("project")
  buildNode=nil
  pluginManagementNode = nil
  pluginsNode=nil
  if (@@reactorHandler.tag_exists?("build/pluginManagement",pom_document) and commonList.length>0)
    buildNode = pom_document.at("project/build")
    pluginManagementNode = pom_document.at("project/build/pluginManagement")
    pluginsNode = pom_document.at("project/build/pluginManagement/plugins")
  elsif (!commonList.nil? and commonList.length>0)
    buildNode = nokObj.new("build",projectNode)
    pluginManagementNode = nokObj.new("pluginManagement",projectNode)
    pluginsNode = nokObj.new("plugins",projectNode)
  end
  if (!commonList.nil? and commonList.length>0)
    commonList.each do |eachplugin|
      pluginArr =  eachplugin.split(":")
      gid = pluginArr[0]
      artifactId =  pluginArr[1]
      version = pluginArr[2]
      pluginNode = nokObj.new("plugin",projectNode)
      grpNode = nokObj.new("groupId",projectNode)
      grpNode.content=gid
      artifactNode = nokObj.new("artifactId",projectNode)
      artifactNode.content=artifactId
      versionNode = nokObj.new("version",projectNode)
      versionNode.content=version
      pluginNode.add_child(grpNode)
      pluginNode.add_child(artifactNode)
      pluginNode.add_child(versionNode)
      pluginsNode.add_child(pluginNode)
    end
    if !buildNode.nil?
      pluginManagementNode.add_child(pluginsNode)
      buildNode.add_child(pluginManagementNode)
      @@dependencyHandler.add_node_element('//project', buildNode, pom_document)
      @@mvnReactorization.write_nokogiri_to_xml(pomPath, pom_document)
      puts "Plugin management is added in reactor pom"
    end
  end
end

#putPluginVersionIfNotFoundInPlugins(eachPomPath, pom_document, pluginList, tag) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 329

def putPluginVersionIfNotFoundInPlugins(eachPomPath,pom_document,pluginList,tag)
  pluginList.each do |key,val|
    versionNode = val.at("version")
    pom_document.css(tag).each do |eachPlugin|
      grpId=nil
      if (eachPlugin.at("groupId").nil? or eachPlugin.at("groupId").parent.name != "plugin")
        grpId="org.apache.maven.plugins"
      else
        grpId = eachPlugin.at("groupId").text
      end
      artifactId = eachPlugin.at("artifactId").text
      pluginCordinate = "#{grpId}:#{artifactId}"
      if ((!versionNode.nil?) and (key==pluginCordinate) and (eachPlugin.at("version").nil? or eachPlugin.at("version").parent.name !="plugin"))
        version = versionNode.text
        nokObj = Nokogiri::XML::Node
        newVersionNode = nokObj.new("version",eachPlugin)
        newVersionNode.content = version
        eachPlugin.add_child(newVersionNode)
      end
    end
  end
end

#putUncommonPlugin(eachPomPath, pom_document, pluginList, pluginsList, tag) ⇒ Object



368
369
370
371
372
373
374
375
376
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 368

def putUncommonPlugin(eachPomPath,pom_document,pluginList,pluginsList,tag)
  pluginsNode = pom_document.at(tag)
  pluginList.each do |key,val|
    if (!pluginsList.include?key)
      puts "Mismatch plugin found in #{eachPomPath}"
      pluginsNode.add_child(val)
    end
  end
end

#removePluginManagementTag(pom_document) ⇒ Object



378
379
380
381
382
383
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 378

def removePluginManagementTag(pom_document)
  pluginManagementNode = pom_document.at("project/build/pluginManagement")
  if !pluginManagementNode.nil?
    pluginManagementNode.remove
  end
end

#removePluginversionFromChildModule(project_directory_path, commonArr, tag) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 178

def removePluginversionFromChildModule(project_directory_path,commonArr,tag)
  commonPlugArr = Array.new
  commonArr.each do |eachPlug|
    plugArr = eachPlug.split(":")
    cordinatePlug = "#{plugArr[0]}:#{plugArr[1]}"
    commonPlugArr.push(cordinatePlug)
  end
  pomArr =  @@reactorHandler.fetchGidArtifactIdAndVersionFromChildModule(project_directory_path,false)
  if  !(pomArr.nil? || pomArr.empty?)
    pomArr.each do |eachPomPath|
      pom_document =  @@reactorHandler.parse_xml_from_file(eachPomPath)
      pluginsNode=nil
      if tag.include?"/pluginManagement/"
        pluginsNode = pom_document.at("build/pluginManagement/plugins")
      else
        pluginsNode = pom_document.at("build/plugins")
      end
      if !pluginsNode.nil?
        pluginsNode.css("plugin").each do |eachplugin|
          grpId=nil
          if (eachplugin.at("groupId").nil?)
            grpId="org.apache.maven.plugins"
          else
            grpId = eachplugin.at("groupId").text
          end
          artifactId = eachplugin.at("artifactId").text
          versionNode = eachplugin.at("version")
          if (!versionNode.nil? and versionNode.parent.name == "plugin")
            version = versionNode.text
            fullPlugin = "#{grpId}:#{artifactId}"
            if (commonPlugArr.include?fullPlugin)
              versionNode.remove
            end
          end
        end
        @@mvnReactorization.write_nokogiri_to_xml(eachPomPath, pom_document)
      end
    end
  end
end

#searchCommonPluginInPlugins(eachPomPath, pom_document, cordinate, eachNode, versionNode) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 244

def searchCommonPluginInPlugins(eachPomPath,pom_document,cordinate,eachNode,versionNode)
  pom_document.css("project/build/plugins/plugin").each do |eachPlugin|
    grpId=nil
    if (eachPlugin.at("groupId").nil? or eachPlugin.at("groupId").parent.name != "plugin")
      grpId="org.apache.maven.plugins"
    else
      grpId = eachPlugin.at("groupId").text
    end
    artifactId = eachPlugin.at("artifactId").text
    pluginCordinate = "#{grpId}:#{artifactId}"
    if (cordinate == pluginCordinate)
      nodeName = eachNode.name
      if (eachPlugin.at(nodeName).nil? or eachPlugin.at(nodeName).parent.name != "plugin")
        eachPlugin.add_child(eachNode)
        if (eachPlugin.at("version").nil? or eachPlugin.at("version").parent.name != "plugin")
          version = versionNode.text
          nokObj = Nokogiri::XML::Node
          newVersionNode = nokObj.new("version",eachPlugin)
          newVersionNode.content = version
          eachPlugin.add_child(newVersionNode)
        end
        @@mvnReactorization.write_nokogiri_to_xml(eachPomPath, pom_document)
      end
    end
  end
end

#searchPluginInPlugins(pom_document, cordinate) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/mavenReactorService/PluginManagementHandler.rb', line 283

def searchPluginInPlugins(pom_document,cordinate)
  isPluginRequired = true
  pom_document.css("project/build/plugins/plugin").each do |eachPlugin|
    grpId=nil
    if (eachPlugin.at("groupId").nil? or eachPlugin.at("groupId").parent.name != "plugin")
      grpId="org.apache.maven.plugins"
    else
      grpId = eachPlugin.at("groupId").text
    end
    artifactId = eachPlugin.at("artifactId").text
    pluginCordinate = "#{grpId}:#{artifactId}"
    if (pluginCordinate == cordinate)
      puts "Working:::::::::::::::::"
      puts pluginCordinate
      isPluginRequired=false
    end
  end
end