class PluginHandler @@basicInfoHandler = BasicInfoHandler.new @@dependencyHandler = DependencyHandler.new @@reactorHandler = ReactorHandler.new @@xpathOfPlugin=nil # remove pluginmanagement section def removePluginManagementTag() efftive_pom_document = @@basicInfoHandler.copyTagsFromEffectivePom(Dir.getwd) temp_pom_document = Nokogiri::XML(open("temp.xml")) buildNode = temp_pom_document.at("project/build") if !buildNode.nil? #If condition will come buildNode.at("pluginManagement").remove formated_pom_doc = @@dependencyHandler.add_node_element("project",buildNode,temp_pom_document) File.write("temp.xml", formated_pom_doc) puts "Plugins management is removed" end end def removeExtraTag(tagName) temp_pom_document = Nokogiri::XML(open("temp.xml")) buildNode = temp_pom_document.at("project/#{tagName}") if !buildNode.nil? srcDirectoryNode = buildNode.css("sourceDirectory") scriptSourceDirectoryNode = buildNode.css("scriptSourceDirectory") testSourceDirectoryNode = buildNode.css("testSourceDirectory") outputDirectoryNode = buildNode.css("outputDirectory") testOutputDirectoryNode = buildNode.css("testOutputDirectory") resourcesNode = buildNode.css("resources") testResourcesNode = buildNode.css("testResources") directoryNode = buildNode.css("directory") finalNameNode = buildNode.css("finalName") additionalClasspathElementsNode = buildNode.css("additionalClasspathElements") sortOrderFile = buildNode.css("sortOrderFile") if !srcDirectoryNode.nil? srcDirectoryNode.each do |eachSrcDirectory| eachSrcDirectory.remove end end if !scriptSourceDirectoryNode.nil? scriptSourceDirectoryNode.each do |eachScriptSourceDirectory| eachScriptSourceDirectory.remove end end if !testSourceDirectoryNode.nil? testSourceDirectoryNode.each do |eachTestSourceDirectory| eachTestSourceDirectory.remove end end if !outputDirectoryNode.nil? outputDirectoryNode.each do |eachOutputDirectory| eachOutputDirectory.remove end end if !testOutputDirectoryNode.nil? testOutputDirectoryNode.each do |eachTestOutputDirectory| eachTestOutputDirectory.remove end end if !resourcesNode.nil? resourcesNode.each do |eachResources| eachResources.remove end end if !testResourcesNode.nil? testResourcesNode.each do |eachTestResources| eachTestResources.remove end end if !directoryNode.nil? directoryNode.each do |eachDirectory| eachDirectory.remove end end if !finalNameNode.nil? finalNameNode.each do |eachFinalName| eachFinalName.remove end end if !additionalClasspathElementsNode.nil? additionalClasspathElementsNode.each do |eachAdditionalClasspathElements| eachAdditionalClasspathElements.remove end end if !sortOrderFile.nil? sortOrderFile.each do |eachSortOrderFile| eachSortOrderFile.remove end end formated_pom_doc = @@dependencyHandler.add_node_element("project",buildNode,temp_pom_document) File.write("temp.xml", formated_pom_doc) end end def addBuildTag() efftive_pom_document = @@basicInfoHandler.copyTagsFromEffectivePom(Dir.getwd) allplugins = efftive_pom_document.at("project/build") if !allplugins.nil? tempXml = "temp.xml" File.write(tempXml, allplugins, File.size(tempXml), mode: 'a') puts "Plugins are added" end end def addReportingTag() efftive_pom_document = @@basicInfoHandler.copyTagsFromEffectivePom(Dir.getwd) allplugins = efftive_pom_document.at("project/reporting") if !allplugins.nil? tempXml = "temp.xml" File.write(tempXml, allplugins, File.size(tempXml), mode: 'a') puts "Reporting Plugins are added" end end # make a list of plugins from effective pom def getGrpAndArtifactIdOfPlugins(tagName=nil) temp_pom_document = Nokogiri::XML(open("temp.xml")) pluginList = Array.new buildPluginPath = "project/build/plugins/plugin" reportingPluginPath = "project/reporting/plugins/plugin" pluginNode = nil if (tagName == "reporting") pluginNode = temp_pom_document.css(reportingPluginPath) else pluginNode = temp_pom_document.css(buildPluginPath) end pluginNode.each do |eachPlugin| grpId=nil if (!eachPlugin.at("groupId").nil? and eachPlugin.at("groupId").parent.name == "plugin") grpId = eachPlugin.at("groupId").text else grpId = "org.apache.maven.plugins" end artifactId = eachPlugin.at("artifactId").text fullPlugin = "#{grpId}:#{artifactId}" pluginList.push(fullPlugin) end return pluginList end # findout the common plugin in original pom def compareWithOriginalPomPlugin(pluginList,tagName=nil) pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd) temp_pom_document = @@reactorHandler.parse_xml_from_file("temp.xml") pluginNode = nil pomFilePluginNode = nil toAddPath=nil if tagName == "reporting" pluginNode = temp_pom_document.at("project/reporting/plugins") pomFilePluginNode = pom_document.css("project/reporting/plugins/plugin") toAddPath = "project/reporting" else pluginNode = temp_pom_document.at("project/build/plugins") pomFilePluginNode = pom_document.css("project/build/plugins/plugin") toAddPath = "project/build" end pomFilePluginNode.each do |eachPlugin| #grpId = eachPlugin.at("groupId").text grpId=nil if (!eachPlugin.at("groupId").nil? and eachPlugin.at("groupId").parent.name == "plugin") grpId = eachPlugin.at("groupId").text else grpId = "org.apache.maven.plugins" end artifactId = eachPlugin.at("artifactId").text fullPlugin = "#{grpId}:#{artifactId}" if pluginList.include?fullPlugin handlePlaceholder(eachPlugin,fullPlugin,pluginNode) end end formated_pom_doc = @@dependencyHandler.add_node_element(toAddPath,pluginNode,temp_pom_document) File.write("temp.xml", formated_pom_doc.to_xml) end # findout the common plugin in parent def compareWithParentPomPlugin(pluginList,tagName=nil) puts "Parent plugin placeholder replacement is started" pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd) parent = pom_document.at("project/parent") if !parent.nil? parent_pom_document = @@dependencyHandler.getParentPomDomObjFromUrl(pom_document) temp_pom_document = @@reactorHandler.parse_xml_from_file("temp.xml") pluginNode = nil pomFilePluginNode = nil toAddPath=nil if tagName == "reporting" pluginNode = temp_pom_document.at("project/reporting/plugins") pomFilePluginNode = parent_pom_document.css("project/reporting/plugins/plugin") toAddPath = "project/reporting" else pluginNode = temp_pom_document.at("project/build/plugins") pomFilePluginNode = parent_pom_document.css("project/build/plugins/plugin") toAddPath = "project/build" end pomFilePluginNode.each do |eachPlugin| #grpId = eachPlugin.at("groupId").text grpId=nil if (!eachPlugin.at("groupId").nil? and eachPlugin.at("groupId").parent.name == "plugin") grpId = eachPlugin.at("groupId").text else grpId = "org.apache.maven.plugins" end artifactId = eachPlugin.at("artifactId").text fullPlugin = "#{grpId}:#{artifactId}" if pluginList.include?fullPlugin handlePlaceholder(eachPlugin,fullPlugin,pluginNode) end end formated_pom_doc = @@dependencyHandler.add_node_element(toAddPath,pluginNode,temp_pom_document) File.write("temp.xml", formated_pom_doc.to_xml) end end # findout the common plugin in grand parent def compareWithGrandParentPomPlugin(pluginList,tagName=nil) pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd) parent = pom_document.at("project/parent") if !parent.nil? parent_pom_document = @@dependencyHandler.getParentPomDomObjFromUrl(pom_document) grandParent = parent_pom_document.at("project/parent") if !grandParent.nil? grand_parent_pom_document = @@dependencyHandler.getParentPomDomObjFromUrl(parent_pom_document) temp_pom_document = @@reactorHandler.parse_xml_from_file("temp.xml") pluginNode = nil pomFilePluginNode = nil toAddPath=nil if tagName == "reporting" pluginNode = temp_pom_document.at("project/reporting/plugins") pomFilePluginNode = grand_parent_pom_document.css("project/reporting/plugins/plugin") toAddPath = "project/reporting" else pluginNode = temp_pom_document.at("project/build/plugins") pomFilePluginNode = grand_parent_pom_document.css("project/build/plugins/plugin") toAddPath = "project/build" end pomFilePluginNode.each do |eachPlugin| # issue-117 #grpId = eachPlugin.at("groupId").text if (!eachPlugin.at("groupId").nil? and eachPlugin.at("groupId").parent.name == "plugin") grpId = eachPlugin.at("groupId").text else grpId = "org.apache.maven.plugins" end artifactId = eachPlugin.at("artifactId").text fullPlugin = "#{grpId}:#{artifactId}" if pluginList.include?fullPlugin handlePlaceholder(eachPlugin,fullPlugin,pluginNode) end end formated_pom_doc = @@dependencyHandler.add_node_element(toAddPath,pluginNode,temp_pom_document) File.write("temp.xml", formated_pom_doc.to_xml) end end end # handle placeholder operation def handlePlaceholder(eachPlugin,fullPlugin,pluginNode) eachPlugin.children.each do |eachTag| if (eachTag.children.length>1) handlePlaceholder(eachTag,fullPlugin,pluginNode) else pluginVal = eachTag.text plugintag = eachTag.name if (pluginVal.include?"${" and plugintag!="#cdata-section") @@xpathOfPlugin=nil createXpathOfTag(eachTag) replaceTagValInTemp(fullPlugin,plugintag,pluginVal,pluginNode) end end end end # create xpath of placeholder tag def createXpathOfTag(eachTag) if (eachTag.parent.name != "plugin") @@xpathOfPlugin = "#{eachTag.parent.name}/#{@@xpathOfPlugin}" createXpathOfTag(eachTag.parent) end end # introduce place holder if found in original, parent or grand parent def replaceTagValInTemp(fullPlugin,tagName,tagVal,pluginNode) temp_pom_document = @@reactorHandler.parse_xml_from_file("temp.xml") pluginNode.css("plugin").each do |eachPlugin| grpId=nil if (!eachPlugin.at("groupId").nil? and eachPlugin.at("groupId").parent.name == "plugin") grpId = eachPlugin.at("groupId").text else grpId = "org.apache.maven.plugins" end artifactId = eachPlugin.at("artifactId").text tempFullPlugin = "#{grpId}:#{artifactId}" if (fullPlugin == tempFullPlugin) fullXpath = "#{@@xpathOfPlugin}#{tagName}" deleteNode = eachPlugin.at(fullXpath) deleteParent = deleteNode.parent deleteNode.remove nokObj = Nokogiri::XML::Node projectNode = temp_pom_document.at("project") tagNode = nokObj.new(tagName,projectNode) tagNode.content=tagVal deleteParent.add_child(tagNode) # eachPlugin.css("configuration").each do |eachConfig| # parentName = eachConfig.parent.name # if (parentName == "plugin") # eachConfig.remove # end # end pluginNode.add_child(eachPlugin) end end end end