class PluginManagementHandler @@reactorHandler = ReactorHandler.new @@dependencyHandler = DependencyHandler.new @@mvnReactorization = MvnReactorization.new def handlePluginManagement(project_dir) commonPluginList = findCommonPlugins(project_dir) putCommonPluginInReactorPom(project_dir,commonPluginList) removePluginversionFromChildModule(project_dir,commonPluginList,"/plugins") removePluginversionFromChildModule(project_dir,commonPluginList,"/pluginManagement/") end 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 def removePluginManagementTag(pom_document) pluginManagementNode = pom_document.at("project/build/pluginManagement") if !pluginManagementNode.nil? pluginManagementNode.remove end end def executeShortPom() result = `mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort` puts result puts "All poms are sorted by sort-pom plugin" end end