2525import org .xml .sax .SAXException ;
2626
2727public class createParentPOM {
28+
2829 public static void main (String [] args ) {
2930 try {
3031 // Get target pom and source pom for properties
@@ -42,29 +43,28 @@ public static void main(String[] args) {
4243 // Create a new document for the target pom.xml file
4344 Document targetDocument = createTargetDocument (targetPath );
4445
45- // Remove content under dependencies and Repositories tag from target document
46- removeExistingDependencies ( targetDocument );
47- removeExistingRepositories (targetDocument );
48- removeExistingPluginRepositories (targetDocument );
49-
46+ System . out . println ( "TargetPath = " + targetPath );
47+ // Remove content under dependencies,repositories and pluginRepositories tag from target document
48+ removeExisting (targetDocument , "dependencies" );
49+ removeExisting (targetDocument , "repositories" );
50+ removeExisting ( targetDocument , "pluginRepositories" );
5051
5152 // Copy dependencies from modules pom to target pom
5253 for (int i = 2 ; i < args .length ; i ++) {
5354 String sourcePath = args [i ];
54- System .out .println ("------------" + sourcePath );
55+ System .out .println ("------------" + sourcePath );
5556 Document sourceDocument = createSourceDocument (sourcePath );
5657
5758 // Copy repositories and pluginRepositories
5859 if (args [i ].contains ("Engine" )) {
59- //copyRepositories (sourceDocument, targetDocument);
60- //removeDuplicateRepositories (targetDocument);
61- copyPluginRepositories (sourceDocument ,targetDocument );
62- removeDuplicatePluginRepositories (targetDocument );
60+ copyChilds (sourceDocument , targetDocument , "repositories" );
61+ //removeDuplicates (targetDocument,"repositories","repository" );
62+ copyChilds (sourceDocument , targetDocument , "pluginRepositories" );
63+ //removeDuplicates (targetDocument,"pluginRepositories","pluginRepository" );
6364 }
64-
6565 // Copy dependencies
66- copyDependencies (sourceDocument , targetDocument );
67- removeDuplicateDependencies (targetDocument );
66+ copyChilds (sourceDocument , targetDocument , "dependencies" );
67+ removeDuplicates (targetDocument , "dependencies" , "dependency" );
6868
6969 // Remove engine artifact
7070 removeEngineArtifact (targetDocument );
@@ -100,37 +100,21 @@ private static Document createSourceDocument(String sourcePath) throws ParserCon
100100 return document .parse (sourcePomPath );
101101 }
102102
103- private static void removeExistingDependencies (Document targetDocument ) {
104- NodeList dependencyList = targetDocument .getElementsByTagName ("dependencies" );
103+ private static void removeExisting (Document targetDocument , String tagName ) {
104+ NodeList dependencyList = targetDocument .getElementsByTagName (tagName );
105105 if (dependencyList .getLength () > 0 ) {
106106 Node dependenciesNode = dependencyList .item (0 );
107107 while (dependenciesNode .hasChildNodes ()) {
108108 dependenciesNode .removeChild (dependenciesNode .getFirstChild ());
109109 }
110110 }
111111 }
112-
113- private static void copyPluginRepositories (Document sourceDocument , Document targetDocument ) {
114- Node sourcePluginRepositoriesNode = getPluginRepositoriesNode (sourceDocument );
115- Node targetPluginRepositoriesNode = getPluginRepositoriesNode (targetDocument );
116- if (sourcePluginRepositoriesNode != null && targetPluginRepositoriesNode != null ) {
117- copyNodes (sourcePluginRepositoriesNode , targetPluginRepositoriesNode , targetDocument );
118- }
119- }
120112
121- private static void copyRepositories (Document sourceDocument , Document targetDocument ) {
122- Node sourceRepositoriesNode = getRepositoriesNode (sourceDocument );
123- Node targetRepositoriesNode = getRepositoriesNode (targetDocument );
124- if (sourceRepositoriesNode != null && targetRepositoriesNode != null ) {
125- copyNodes (sourceRepositoriesNode , targetRepositoriesNode , targetDocument );
126- }
127- }
128-
129- private static void copyDependencies (Document sourceDocument , Document targetDocument ) {
130- Node sourceDependenciesNode = getDependenciesNode (sourceDocument );
131- Node targetDependenciesNode = getDependenciesNode (targetDocument );
132- if (sourceDependenciesNode != null && targetDependenciesNode != null ) {
133- copyNodes (sourceDependenciesNode , targetDependenciesNode , targetDocument );
113+ private static void copyChilds (Document sourceDocument , Document targetDocument ,String tagName ) {
114+ Node sourceNode = getNodes (sourceDocument ,tagName );
115+ Node targetNode = getNodes (targetDocument ,tagName );
116+ if (sourceNode != null && targetNode != null ) {
117+ copyNodes (sourceNode , targetNode , targetDocument );
134118 }
135119 }
136120
@@ -143,33 +127,13 @@ private static void copyNodes(Node sourceNode, Node targetNode, Document targetD
143127 }
144128 }
145129
146- private static void removeDuplicateDependencies (Document targetDocument ) {
147- Node dependenciesNode = getDependenciesNode (targetDocument );
130+ private static void removeDuplicates (Document targetDocument , String tagName , String childTagName ) {
131+ Node dependenciesNode = getNodes (targetDocument , tagName );
148132 if (dependenciesNode != null ) {
149- removeDuplicateNodes (dependenciesNode , "dependency" );
150- System .out .println ("Duplicate dependency removed successfully" );
133+ removeDuplicateNodes (dependenciesNode , childTagName );
134+ System .out .println ("Duplicate removed successfully" );
151135 } else {
152- System .out .println ("No <dependencies> section found." );
153- }
154- }
155-
156- private static void removeDuplicateRepositories (Document targetDocument ) {
157- Node repositoriesNode = getRepositoriesNode (targetDocument );
158- if (repositoriesNode != null ) {
159- removeDuplicateNodes (repositoriesNode , "repository" );
160- System .out .println ("Duplicate repository removed successfully." );
161- } else {
162- System .out .println ("No <repositories> section found." );
163- }
164- }
165-
166- private static void removeDuplicatePluginRepositories (Document targetDocument ) {
167- Node pluginRepositoriesNode = getPluginRepositoriesNode (targetDocument );
168- if (pluginRepositoriesNode != null ) {
169- removeDuplicateNodes (pluginRepositoriesNode , "pluginRepository" );
170- System .out .println ("Duplicate plugin repository removed successfully." );
171- } else {
172- System .out .println ("No <pluginRepositories> section found." );
136+ System .out .println ("No section found." );
173137 }
174138 }
175139
@@ -214,18 +178,8 @@ private static void removeEngineArtifact(Document targetDocument) throws XPathEx
214178 }
215179 }
216180
217- private static Node getDependenciesNode (Document doc ) {
218- NodeList nodeList = doc .getElementsByTagName ("dependencies" );
219- return nodeList .getLength () > 0 ? nodeList .item (0 ) : null ;
220- }
221-
222- private static Node getRepositoriesNode (Document doc ) {
223- NodeList nodeList = doc .getElementsByTagName ("repositories" );
224- return nodeList .getLength () > 0 ? nodeList .item (0 ) : null ;
225- }
226-
227- private static Node getPluginRepositoriesNode (Document doc ) {
228- NodeList nodeList = doc .getElementsByTagName ("pluginRepositories" );
181+ private static Node getNodes (Document doc , String tagName ) {
182+ NodeList nodeList = doc .getElementsByTagName (tagName );
229183 return nodeList .getLength () > 0 ? nodeList .item (0 ) : null ;
230184 }
231185
@@ -236,23 +190,5 @@ private static void saveXML(Document doc, File file) throws TransformerException
236190 StreamResult streamResult = new StreamResult (file );
237191 transformer .transform (domSource , streamResult );
238192 }
239-
240- private static void removeExistingRepositories (Document targetDocument ) {
241- NodeList dependencyList = targetDocument .getElementsByTagName ("repositories" );
242- if (dependencyList .getLength () > 0 ) {
243- Node dependenciesNode = dependencyList .item (0 );
244- while (dependenciesNode .hasChildNodes ()) {
245- dependenciesNode .removeChild (dependenciesNode .getFirstChild ());
246- }
247- }
248- }
249- private static void removeExistingPluginRepositories (Document targetDocument ) {
250- NodeList dependencyList = targetDocument .getElementsByTagName ("pluginRepositories" );
251- if (dependencyList .getLength () > 0 ) {
252- Node dependenciesNode = dependencyList .item (0 );
253- while (dependenciesNode .hasChildNodes ()) {
254- dependenciesNode .removeChild (dependenciesNode .getFirstChild ());
255- }
256- }
257- }
258- }
193+
194+ }
0 commit comments