Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/mac dependency management #29

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,4 @@
</plugin>
</plugins>
</build>
<dependencies>

</dependencies>
</project>
185 changes: 111 additions & 74 deletions Common/src/main/java/com/ing/parent/createParentPOM.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.ing.parent;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import javax.xml.XMLConstants;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -21,21 +20,19 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class createParentPOM {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException {
try {
//Get target pom and source pom for properties
String targetPath = args[0];
System.out.println("Target Path (Copy to)" + targetPath);
System.out.println("Properties Copy to" + targetPath);
String propertiesSourcePath = args[1];
System.out.println("Properties Source Path (Copy to)" + propertiesSourcePath);

System.out.println("Properties Copy from" + propertiesSourcePath);
//copy properties from main pom to target pom
String sourcePomContent = Files.readString(Paths.get(propertiesSourcePath));
String[] pomSections = sourcePomContent.split("<properties>|</properties>");
Expand All @@ -45,89 +42,129 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE
String updatedDestinationPomContent = destinationPomContent.replace(pomSections[1], propertiesContent);
Files.write(Paths.get(targetPath), updatedDestinationPomContent.getBytes());
System.out.println("Properties copied successfully!");
String targetPomPath = targetPath;
File targetPomPath = new File(targetPath);

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder document = dbFactory.newDocumentBuilder();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

// Create a new document for the target pom.xml file
Document targetDocument = document.parse(targetPomPath);
targetDocument.getDocumentElement().normalize();

//Remove content under dependencies tag from target document
NodeList dependencyList = targetDocument.getElementsByTagName("dependencies");
if (dependencyList.getLength() > 0) {
Node dependenciesNode = dependencyList.item(0);
while (dependenciesNode.hasChildNodes()) {
dependenciesNode.removeChild(dependenciesNode.getFirstChild());
}
}

//copy dependencies from modules pom to target pom
for (int i = 2; i < args.length; i++) {

String sourcePath = args[i];
System.out.println("Source Path (Copy from)" + sourcePath);
String sourcePomPath = sourcePath;

// Load source pom.xml file
// DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//DocumentBuilder document = dbFactory.newDocumentBuilder();
//TransformerFactory transformerFactory = TransformerFactory.newInstance();
//Transformer transformer = transformerFactory.newTransformer();
//Formatting of XML
Document doc = document.parse(new File(sourcePomPath));
NodeList dependencyList = doc.getElementsByTagName("dependency");

// Create a new document for the target pom.xml file
Document targetDocument = document.parse(new File(targetPomPath));
Element dependenciesTag = (Element) targetDocument.getElementsByTagName("dependencies").item(0);

// Copy specific tags from source to target document
for (int j = 0; j < dependencyList.getLength(); j++) {
Node dependency = dependencyList.item(j);
Node copiedNode = targetDocument.importNode(dependency, true);
dependenciesTag.appendChild(copiedNode);
File sourcePomPath = new File(sourcePath);
Document doc = document.parse(sourcePomPath);
doc.getDocumentElement().normalize();
Node sourceDependenciesNode = getDependenciesNode(doc);
Node targetDependenciesNode = getDependenciesNode(targetDocument);
if (sourceDependenciesNode != null && targetDependenciesNode != null) {
NodeList dependenciesList = sourceDependenciesNode.getChildNodes();
for (int j = 0; j < dependenciesList.getLength(); j++) {
Node dependency = dependenciesList.item(j);
// Import the node from the source doc to the target doc and append
Node importedNode = targetDocument.importNode(dependency, true);
targetDependenciesNode.appendChild(importedNode);
}
}
//remove duplicate entries
Node dependenciesNode = getDependenciesNode(targetDocument);
if (dependenciesNode != null) {
removeDuplicateDependencies(dependenciesNode);
System.out.println("Duplicates removed successfully.");
} else {
System.out.println("No <dependencies> section found.");
}

// Write the target pom.xml document to file
transformer.transform(new DOMSource(targetDocument), new StreamResult(new File(targetPomPath)));

System.out.println("Tags copied from " + sourcePath + " to " + targetPath);
}
//Remove Duplicate dependency
Document finalPom = document.parse(new File(targetPomPath));
Element finalDependenciesTag = (Element) finalPom.getElementsByTagName("dependencies").item(0);
NodeList finalDependencyList = finalDependenciesTag.getElementsByTagName("dependency");
HashSet<String> uniqueEntries = new HashSet<>();
for (int k = finalDependencyList.getLength() - 1; k >= 0; k--) {
Element dependency = (Element) finalDependencyList.item(k);
String entry = "";
String groupId = dependency.getElementsByTagName("groupId").item(0).getTextContent();
String artifactId = dependency.getElementsByTagName("artifactId").item(0).getTextContent();
try{
String classifier = dependency.getElementsByTagName("classifier").item(0).getTextContent();
entry = groupId + ":" + artifactId + ":" + classifier;
//Remove engine artifact
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String artifactId = "ingenious-engine";
XPathExpression expression = xpath.compile("//dependency[artifactId='" + artifactId + "']");
NodeList dependencyNodes = (NodeList) expression.evaluate(targetDocument, XPathConstants.NODESET);
for (int k = 0; k < dependencyNodes.getLength(); k++) {
Node dependencyNode = dependencyNodes.item(k);
Node parent = dependencyNode.getParentNode();
parent.removeChild(dependencyNode);
}
catch(NullPointerException e)
{
entry = groupId + ":" + artifactId;
}
// If the entry already exists, remove the duplicate dependency
if (uniqueEntries.contains(entry)) {
finalDependenciesTag.removeChild(dependency);
saveXML(targetDocument, targetPomPath);
}

} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}

private static Node getDependenciesNode(Document doc) {
NodeList nodeList = doc.getElementsByTagName("dependencies");
if (nodeList.getLength() > 0) {
return nodeList.item(0);
}
return null;
}

private static void saveXML(Document doc, File file) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(doc);
StreamResult streamResult = new StreamResult(file);
transformer.transform(domSource, streamResult);
}

private static void removeDuplicateDependencies(Node dependenciesNode) {
Set<String> uniqueDependencies = new HashSet<>();
NodeList dependenciesList = dependenciesNode.getChildNodes();
for (int i = dependenciesList.getLength() - 1; i >= 0; i--) {
Node dependency = dependenciesList.item(i);
if (dependency.getNodeType() == Node.ELEMENT_NODE && dependency.getNodeName().equals("dependency")) {
// Extract the unique key for the dependency (groupId:artifactId:version)
String uniqueKey = getDependencyKey(dependency);
// If the key is already present, it's a duplicate, so remove the node
if (uniqueDependencies.contains(uniqueKey)) {
dependenciesNode.removeChild(dependency);
} else {
uniqueEntries.add(entry);
uniqueDependencies.add(uniqueKey);
}
}
System.out.println("Duplicate entries removed successfully.");
//Removing engine artifact
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String artifactId = "ingenious-engine";

XPathExpression expression = xpath.compile("//dependency[artifactId='" + artifactId + "']");
NodeList dependencyNodes = (NodeList) expression.evaluate(finalPom, XPathConstants.NODESET);
for (int i = 0; i < dependencyNodes.getLength(); i++) {
Node dependencyNode = dependencyNodes.item(i);
Node parent = dependencyNode.getParentNode();
parent.removeChild(dependencyNode);
}
// Save the modified POM XML file
transformer.transform(new DOMSource(finalPom), new StreamResult(new File(targetPomPath)));
System.out.println("Removed Engine artifact");
}
}

} catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
e.printStackTrace();
// Helper method to generate a unique key for a <dependency> (groupId:artifactId:version)
private static String getDependencyKey(Node dependencyNode) {
String groupId = "";
String artifactId = "";
String classifier = "";

NodeList childNodes = dependencyNode.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node child = childNodes.item(j);
if (child.getNodeType() == Node.ELEMENT_NODE) {

switch (child.getNodeName()) {
case "groupId":
groupId = child.getTextContent().trim();
break;
case "artifactId":
artifactId = child.getTextContent().trim();
break;
case "classifier":
classifier = child.getTextContent().trim();
break;
}
}
}
return groupId + ":" + artifactId + ":" + classifier;
}
}
4 changes: 2 additions & 2 deletions Engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>.classpath</include>
<!--<include>.classpath</include>-->
<include>.project</include>
</includes>
</resource>
Expand All @@ -367,7 +367,7 @@
<fileset>
<directory>${project.basedir}</directory>
<includes>
<include>.classpath</include>
<!--<include>.classpath</include>-->
<include>.project</include>
</includes>
</fileset>
Expand Down
Loading