|
| 1 | +/* |
| 2 | + * Copyright 2015 Licel Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.licel.jcardsim.utils; |
| 17 | + |
| 18 | +import org.w3c.dom.Document; |
| 19 | +import org.w3c.dom.Element; |
| 20 | +import org.w3c.dom.Node; |
| 21 | +import org.w3c.dom.NodeList; |
| 22 | + |
| 23 | +import javax.xml.parsers.DocumentBuilder; |
| 24 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 25 | +import javax.xml.transform.OutputKeys; |
| 26 | +import javax.xml.transform.Transformer; |
| 27 | +import javax.xml.transform.TransformerFactory; |
| 28 | +import javax.xml.transform.dom.DOMSource; |
| 29 | +import javax.xml.transform.stream.StreamResult; |
| 30 | +import java.io.File; |
| 31 | + |
| 32 | +/** |
| 33 | + * Removes redundant dependencies from the POM file |
| 34 | + * oracle.javacard:api_classic |
| 35 | + * org.ow2.asm:* |
| 36 | + */ |
| 37 | +public class PomProcessor { |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + if (args.length == 0){ |
| 41 | + throw new IllegalArgumentException("Build directory is required"); |
| 42 | + } |
| 43 | + |
| 44 | + File buildDir = new File(args[0]); |
| 45 | + if (!buildDir.exists() || !buildDir.isDirectory()) { |
| 46 | + throw new RuntimeException("Invalid directory: " + buildDir); |
| 47 | + } |
| 48 | + |
| 49 | + final File depPom = new File(buildDir, "dependency-reduced-pom.xml"); |
| 50 | + if (!depPom.exists()){ |
| 51 | + System.err.println("POM not found: " + depPom.getAbsolutePath()); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 56 | + DocumentBuilder dBuilder; |
| 57 | + |
| 58 | + try { |
| 59 | + dBuilder = dbFactory.newDocumentBuilder(); |
| 60 | + Document doc = dBuilder.parse(depPom); |
| 61 | + |
| 62 | + deleteRedundantDeps(doc); |
| 63 | + |
| 64 | + // Write the updated document to file or console |
| 65 | + doc.getDocumentElement().normalize(); |
| 66 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 67 | + Transformer transformer = transformerFactory.newTransformer(); |
| 68 | + DOMSource source = new DOMSource(doc); |
| 69 | + StreamResult result = new StreamResult(depPom); |
| 70 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 71 | + transformer.transform(source, result); |
| 72 | + System.out.println("pom.xml updated successfully"); |
| 73 | + |
| 74 | + } catch(Exception e){ |
| 75 | + System.err.println(e.getMessage()); |
| 76 | + e.printStackTrace(System.err); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void deleteRedundantDeps(Document doc) { |
| 81 | + final NodeList projects = doc.getElementsByTagName("project"); |
| 82 | + if (projects.getLength() < 1){ |
| 83 | + System.err.println("Invalid number of project elements"); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + final Node project = projects.item(0); |
| 88 | + if (project.getNodeType() != Node.ELEMENT_NODE){ |
| 89 | + System.err.println("Invalid project node type"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + final Element elProject = (Element) project; |
| 94 | + final NodeList dependencies = elProject.getElementsByTagName("dependencies"); |
| 95 | + if (dependencies.getLength() < 1){ |
| 96 | + System.err.println("Invalid num of dependencies"); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + boolean found = false; |
| 101 | + int depIdx = 0; |
| 102 | + for(depIdx = 0; depIdx < dependencies.getLength(); depIdx++){ |
| 103 | + if (dependencies.item(depIdx).getParentNode() == elProject){ |
| 104 | + found = true; |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (!found){ |
| 110 | + System.err.println("Project.dependencies not found"); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + final Node nDeps = dependencies.item(depIdx); |
| 115 | + if (nDeps.getNodeType() != Node.ELEMENT_NODE){ |
| 116 | + System.err.println("Invalid dependencies node type"); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + final Element elDeps = (Element) nDeps; |
| 121 | + final NodeList depsNodeList = elDeps.getElementsByTagName("dependency"); |
| 122 | + if (depsNodeList.getLength() < 1){ |
| 123 | + System.err.println("No deps found"); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + System.out.println(String.format("Number of dependencies found: %d", depsNodeList.getLength())); |
| 128 | + int i = 0; |
| 129 | + while(i < depsNodeList.getLength()){ |
| 130 | + final Element elDep = (Element)depsNodeList.item(i); |
| 131 | + final NodeList nArtifacts = elDep.getElementsByTagName("artifactId"); |
| 132 | + final NodeList nGroupId = elDep.getElementsByTagName("groupId"); |
| 133 | + |
| 134 | + String artifactId = null; |
| 135 | + String groupId = null; |
| 136 | + |
| 137 | + if (nArtifacts.getLength() > 0) { |
| 138 | + final Element elArtifact = (Element) nArtifacts.item(0); |
| 139 | + final Node alChild = elArtifact.getFirstChild(); |
| 140 | + artifactId = alChild.getNodeType() == Node.TEXT_NODE ? alChild.getNodeValue() : null; |
| 141 | + } |
| 142 | + |
| 143 | + if (nGroupId.getLength() > 0) { |
| 144 | + final Element elGroupId = (Element) nGroupId.item(0); |
| 145 | + final Node alChild = elGroupId.getFirstChild(); |
| 146 | + groupId = alChild.getNodeType() == Node.TEXT_NODE ? alChild.getNodeValue() : null; |
| 147 | + } |
| 148 | + |
| 149 | + System.out.println(String.format("Dependency: %s:%s", groupId, artifactId)); |
| 150 | + |
| 151 | + // Artifact ID based rule: oracle.javacard:api_classic |
| 152 | + if ("api_classic".equalsIgnoreCase(artifactId)) { |
| 153 | + System.out.println("oracle.javacard:api_classic found in dependencies, removing"); |
| 154 | + elDeps.removeChild(elDep); |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + // Group based rule: org.ow2.asm:* |
| 159 | + if ("org.ow2.asm".equalsIgnoreCase(groupId)) { |
| 160 | + System.out.println("org.ow2.asm:* found in dependencies, removing"); |
| 161 | + elDeps.removeChild(elDep); |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + i += 1; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments