require 'mavenReactorService/BasicInfoHandler' # handle the dependencies and it's place holder class DependencyHandler @@reactorHandler = ReactorHandler.new @@basicInfoHandler = BasicInfoHandler.new @@projectURLArr= Array.new def projectPomDependencyHandler() efftive_pom_document = @@basicInfoHandler.copyTagsFromEffectivePom(Dir.getwd) alldependencies = efftive_pom_document.at("project/dependencies") alldependencyManagement = efftive_pom_document.at("project/dependencyManagement") if !alldependencies.nil? tempXml = "temp.xml" File.write(tempXml, alldependencies, File.size(tempXml), mode: 'a') puts "Dependencies are added" end if !alldependencyManagement.nil? tempXml = "temp.xml" File.write(tempXml, alldependencyManagement, File.size(tempXml), mode: 'a') puts "Dependency management is added" end end # return the dependency list of effetive-pom def getTempDependencyList() temp_pom_document = Nokogiri::XML(open("temp.xml")) dependencyList = Array.new temp_pom_document.css("project/dependencies/dependency").each do |eachDependecy| grpId = eachDependecy.at("groupId").text artifactId = eachDependecy.at("artifactId").text fullDependency = "#{grpId}:#{artifactId}" dependencyList.push(fullDependency) end return dependencyList end # return the union of dependencies in original-pom def getOriginalDependencyMap() pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd) dependencyMap = Hash.new if (!pom_document.at_css("project/dependencyManagement/dependencies/dependency").nil?) pom_document.css("project/dependencyManagement/dependencies/dependency").each do |eachDependecy| grpId = eachDependecy.at("groupId").text artifactId = eachDependecy.at("artifactId").text fullDependency = "#{grpId}:#{artifactId}" version = nil if (!eachDependecy.at("version").nil?) version = eachDependecy.at("version").text end dependencyMap[fullDependency] = version end end if (!pom_document.at_css("project/dependencies/dependency").nil?) pom_document.css("project/dependencies/dependency").each do |eachDependecy| grpId = eachDependecy.at("groupId").text artifactId = eachDependecy.at("artifactId").text fullDependency = "#{grpId}:#{artifactId}" version = nil if (!eachDependecy.at("version").nil?) version = eachDependecy.at("version").text end exsistingDependency = dependencyMap[fullDependency] if (!exsistingDependency.nil? and !version.nil?) dependencyMap[fullDependency] = version end end end return dependencyMap end # return the union of dependencies in parent def getImmediateParentDependencyMap() pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd) parent = pom_document.at("project/parent") dependencyMap = Hash.new if !parent.nil? parent_pom_document = getParentPomDomObjFromUrl(pom_document) if (!parent_pom_document.at_css("project/dependencyManagement/dependencies/dependency").nil?) parent_pom_document.css("project/dependencyManagement/dependencies/dependency").each do |eachDependecy| grpId = eachDependecy.at("groupId").text artifactId = eachDependecy.at("artifactId").text fullDependency = "#{grpId}:#{artifactId}" version = nil if (!eachDependecy.at("version").nil?) version = eachDependecy.at("version").text end dependencyMap[fullDependency] = version end end if (!parent_pom_document.at_css("project/dependencies/dependency").nil?) parent_pom_document.css("project/dependencies/dependency").each do |eachDependecy| grpId = eachDependecy.at("groupId").text artifactId = eachDependecy.at("artifactId").text fullDependency = "#{grpId}:#{artifactId}" version = nil if (!eachDependecy.at("version").nil?) version = eachDependecy.at("version").text end exsistingDependency = dependencyMap[fullDependency] if (!exsistingDependency.nil? and !version.nil?) dependencyMap[fullDependency] = version end end end end return dependencyMap end # return the union of dependencies in grand-parent def getgrandParentDependencyMap() pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd) parent = pom_document.at("project/parent") dependencyMap = Hash.new if !parent.nil? parent_pom_document = getParentPomDomObjFromUrl(pom_document) grandParent = parent_pom_document.at("project/parent") if !grandParent.nil? grand_parent_pom_document = getParentPomDomObjFromUrl(parent_pom_document) if (!grand_parent_pom_document.at_css("project/dependencyManagement/dependencies/dependency").nil?) grand_parent_pom_document.css("project/dependencyManagement/dependencies/dependency").each do |eachDependecy| grpId = eachDependecy.at("groupId").text artifactId = eachDependecy.at("artifactId").text fullDependency = "#{grpId}:#{artifactId}" version = nil if (!eachDependecy.at("version").nil?) version = eachDependecy.at("version").text end dependencyMap[fullDependency] = version end end if (!grand_parent_pom_document.at_css("project/dependencies/dependency").nil?) grand_parent_pom_document.css("project/dependencies/dependency").each do |eachDependecy| grpId = eachDependecy.at("groupId").text artifactId = eachDependecy.at("artifactId").text fullDependency = "#{grpId}:#{artifactId}" version = nil if (!eachDependecy.at("version").nil?) version = eachDependecy.at("version").text end exsistingDependency = dependencyMap[fullDependency] if (!exsistingDependency.nil? and !version.nil?) dependencyMap[fullDependency] = version end end end end end return dependencyMap end # create domobj of parent and grand parent def getParentPomDomObjFromUrl(pom_document) groupId = pom_document.css("parent/groupId").text artifactId = pom_document.css("parent/artifactId").text version = pom_document.css("parent/version").text groupId = "#{groupId.gsub(".","/")}" urlArr = getRepositories(pom_document) urlArr.each do |eachUrl| filteredUrl = "#{eachUrl}/#{groupId}/#{artifactId}/#{version}" begin pom_html_doc = Nokogiri::HTML(open(filteredUrl)) latestPom = nil pom_html_doc.css('a').each do |eachref| if (eachref.text.end_with?".pom") latestPom = eachref.text end end filteredUrl = "#{filteredUrl}/#{latestPom}" parent_pom_document = Nokogiri::XML(open(filteredUrl)) puts "External configuration POM URL found at :: #{filteredUrl}" rescue end if !parent_pom_document.nil? return parent_pom_document end end end # indentify the actual url of parent def getRepositories(pom_document) reactorProjectArr = @@reactorHandler.getReactPOMRepos() origProjectPOMDoc = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd) origProjectPOMDoc.css("repositories/repository").each do |eachRepo| eachUrl = eachRepo.at_css("url").text eachUrl = eachUrl.end_with?("/") ? eachUrl.chop : eachUrl if !(@@projectURLArr.include?eachUrl) @@projectURLArr.push(eachUrl) end end pom_document.css("repositories/repository").each do |eachRepo| eachUrl = eachRepo.at_css("url").text eachUrl = eachUrl.end_with?("/") ? eachUrl.chop : eachUrl if !(@@projectURLArr.include?eachUrl) @@projectURLArr.push(eachUrl) end end reactorProjectArr.each do |eachReactURL| if !(@@projectURLArr.include?eachReactURL) @@projectURLArr.push(eachReactURL) end end return @@projectURLArr end # This api takes decision to indentify the dependency where the place holder found. # def findPlaceHolderWithinPoms(tempDependencyList,originalDependencyMap,parentDependencyMap,grandParentDependencyMap) if (!tempDependencyList.nil?) originalVersion = nil parentVersion = nil grandParentVersion = nil tempDependencyList.each do |eachDependency| if (!originalDependencyMap.empty?) originalVersion = originalDependencyMap[eachDependency] end if (!parentDependencyMap.empty?) parentVersion = parentDependencyMap[eachDependency] end if (!grandParentDependencyMap.empty?) grandParentVersion = grandParentDependencyMap[eachDependency] end if (!originalVersion.nil? and originalVersion.include? "${") replaceVersionInTempFile(eachDependency,originalVersion) elsif (!parentVersion.nil? and parentVersion.include? "${") replaceVersionInTempFile(eachDependency,parentVersion) elsif (!grandParentVersion.nil? and grandParentVersion.include? "${") replaceVersionInTempFile(eachDependency,grandParentVersion) end end end end # This api replace version by place-holder # def replaceVersionInTempFile (eachDependency,version) dependencyArr = eachDependency.split(":") gid = dependencyArr[0] artifactId = dependencyArr[1] temp_pom_document = Nokogiri::XML(open("temp.xml")) nokObj = Nokogiri::XML::Node dependeciesNode = temp_pom_document.at("project/dependencies") versionNode = nokObj.new("version",dependeciesNode) versionNode.content=version dependeciesNode.css("dependency").each do |tempDependency| replaceDependency=nil tempGid = tempDependency.at("groupId").text tempArtifactId = tempDependency.at("artifactId").text fullDependency = "#{tempGid}:#{tempArtifactId}" if (eachDependency == fullDependency) tempDependency.remove tempDependency.at("version").remove tempDependency.add_child(versionNode) dependeciesNode.add_child(tempDependency) end end formated_pom_doc = add_node_element("project",dependeciesNode,temp_pom_document) File.write("temp.xml", formated_pom_doc.to_xml) end # This api add child node in any tag # def add_node_element(childs_parent_name, child_element, pom_nokogiri) raise 'Parent tag is empty or nil' if childs_parent_name.nil? || childs_parent_name.empty? raise 'Child element is nil' if child_element.nil? raise 'POM Document is nil' if pom_nokogiri.nil? childs_parent = pom_nokogiri.at_css(childs_parent_name) childs_parent.add_child(child_element) pom_nokogiri end end