class PropertyManager @@ppp_counter = 1 @@reactorHandler = ReactorHandler.new @@modulePomPaths = nil def restructure(project_directory_path, parent_pom_path) @@modulePomPaths = @@reactorHandler.fetchGidArtifactIdAndVersionFromChildModule(project_directory_path,false) allModuleProperties = Array.new commonProperties = Array.new reactorProperties = Array.new @@modulePomPaths.each do |modulePomPath| modulePomDoc = Nokogiri::XML(open(modulePomPath)) amp_counter = 1 modulePomDoc.css("project/properties").children.each do |property| if property.name != 'text' && property.name != 'comment' propertyNameValue = "#{property.name}:_#{property.text}" allModuleProperties.push(propertyNameValue) # puts("(#{amp_counter}) allModuleProperty : #{propertyNameValue}") amp_counter += 1 end end puts("\n\n") end #Parsing Parent POM, pushing all the Properties tag into "parent_pom_properties". parent_pom_properties = Array.new parent_pom_doc = Nokogiri::XML(open(parent_pom_path)) pppnv_counter = 1 parent_pom_doc.css("project/properties").children.each do |parent_pom_property| if parent_pom_property.name != 'text' && parent_pom_property.name != 'comment' parent_pom_property_name_value = "#{parent_pom_property.name}:_#{parent_pom_property.text}" parent_pom_properties.push(parent_pom_property_name_value) # puts("(#{pppnv_counter}) parent_pom_property_name_value : #{parent_pom_property_name_value}") pppnv_counter += 1 end end puts("\n\n") # Assumption is : the Reactor POM will only contain properties, which are common to the Module POMs. # We will find Common properties between "Parent POM" and "Common Module POM" # They(Common Properties) can be excluded before we add them into the Reactor POM by the function isAvailableInParentPomProperties(parent_pom_properties, property). p_counter = 1 u_counter = 1 allModuleProperties.uniq.each do |property| # puts("(#{u_counter}) Unique Module Property : #{property}") u_counter += 1 if allModuleProperties.count(property) == @@modulePomPaths.length # puts("(#{p_counter}) Common Module Property : #{property}") p_counter += 1 commonProperties.push(property) if !isAvailableInParentPomProperties(parent_pom_properties, property) reactorProperties.push(property) end end end puts("\n\n") writeToReactor(project_directory_path,commonProperties,reactorProperties,parent_pom_properties) end def writeToReactor(project_directory_path,commonProperties,reactorProperties,parent_pom_properties) reactorPomPath = "#{project_directory_path}/pom.xml" pom_document = @@reactorHandler.parse_xml_from_file(reactorPomPath) nokObj = Nokogiri::XML::Node projectNode = pom_document.at('project') propertiesNode = nokObj.new('properties',projectNode) reactorProperties.each do |property| propNameValue = property.split(':_') propName = propNameValue[0] propValue = propNameValue[1] propNode = nokObj.new(propName,projectNode) propNode.content = propValue propertiesNode.add_child(propNode) end dependencyHandler = DependencyHandler.new pomObj = dependencyHandler.add_node_element('project',propertiesNode,pom_document) File.write(reactorPomPath,pomObj) puts "Moved common properties to reactor pom!!!" removeCommonPropertiesfromModulePOMs(project_directory_path,commonProperties,parent_pom_properties) end def removeCommonPropertiesfromModulePOMs (project_directory_path,commonProperties,parent_pom_properties) @@modulePomPaths.each do |eachModulePath| pom_document = @@reactorHandler.parse_xml_from_file(eachModulePath) if !commonProperties.nil? commonProperties.each do |property| propNameValue = property.split(':_') propName = propNameValue[0] pom_document.css('project/properties').children.each do |eachProperty| if eachProperty.name == propName eachProperty.remove end end end File.write(eachModulePath,pom_document) end end removeParentPropertiesfromModulePOMs(project_directory_path,parent_pom_properties) end def removeParentPropertiesfromModulePOMs(project_directory_path,parent_pom_properties) @@modulePomPaths.each do |eachModulePath| pom_document = @@reactorHandler.parse_xml_from_file(eachModulePath) if !parent_pom_properties.nil? parent_pom_properties.each do |property| propNameValue = property.split(':_') propName = propNameValue[0] pom_document.css('project/properties').children.each do |eachProperty| if eachProperty.name == propName eachProperty.remove end end end File.write(eachModulePath,pom_document) end end puts "Common properties removed from child modules!!!" end def isAvailableInParentPomProperties (parent_pom_properties, common_module_pom_property) #Looping over parent POM properties and checking if that is present in one of the 'common module POM properties' # puts("Checking availability of common_module_pom_property in parent POM : #{common_module_pom_property}") parent_pom_properties.each do |parent_pom_property| if parent_pom_property == common_module_pom_property # puts("(#{@@ppp_counter}) !!! Matched !!! common_module_pom_property = #{common_module_pom_property}") # puts("parent_pom_property = #{parent_pom_property}") # puts("\n\n") @@ppp_counter += 1 return true end end return false end end