Skip to content

Commit 6b438bc

Browse files
authored
Merge pull request #150 from ph4r05/pr/pom-fix
add pom processor to remove javacard_api from the POM
2 parents a1d6a95 + 51aaa6d commit 6b438bc

File tree

2 files changed

+209
-6
lines changed

2 files changed

+209
-6
lines changed

pom.xml

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,32 @@
234234
<version>1.4.0</version>
235235
<executions>
236236
<execution>
237+
<id>cardApi</id>
237238
<phase>process-classes</phase>
238239
<goals>
239240
<goal>java</goal>
240241
</goals>
242+
<configuration>
243+
<mainClass>com.licel.jcardsim.utils.JavaCardApiProcessor</mainClass>
244+
<arguments>
245+
<argument>${project.build.directory}/classes</argument>
246+
</arguments>
247+
</configuration>
248+
</execution>
249+
<execution>
250+
<id>pom</id>
251+
<phase>verify</phase>
252+
<goals>
253+
<goal>java</goal>
254+
</goals>
255+
<configuration>
256+
<mainClass>com.licel.jcardsim.utils.PomProcessor</mainClass>
257+
<arguments>
258+
<argument>${project.build.directory}</argument>
259+
</arguments>
260+
</configuration>
241261
</execution>
242262
</executions>
243-
<configuration>
244-
<mainClass>com.licel.jcardsim.utils.JavaCardApiProcessor</mainClass>
245-
<arguments>
246-
<argument>${project.build.directory}/classes</argument>
247-
</arguments>
248-
</configuration>
249263
</plugin>
250264
<plugin>
251265
<groupId>org.apache.maven.plugins</groupId>
@@ -265,6 +279,7 @@
265279
<artifactId>maven-shade-plugin</artifactId>
266280
<version>1.7</version>
267281
<configuration>
282+
<keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
268283
<minimizeJar>true</minimizeJar>
269284
<relocations>
270285
<relocation>
@@ -276,6 +291,12 @@
276291
<includes>
277292
<include>org.bouncycastle:*</include>
278293
</includes>
294+
<excludes>
295+
<exclude>oracle.javacard:*</exclude>
296+
<exclude>oracle.javacard:*:*:*</exclude>
297+
<exclude>*:api_classic</exclude>
298+
<exclude>oracle.javacard:api_classic</exclude>
299+
</excludes>
279300
</artifactSet>
280301
<filters>
281302
<filter>
@@ -296,11 +317,18 @@
296317
<goal>shade</goal>
297318
</goals>
298319
<configuration>
320+
<keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
299321
<minimizeJar>true</minimizeJar>
300322
<artifactSet>
301323
<includes>
302324
<include>org.bouncycastle:*</include>
303325
</includes>
326+
<excludes>
327+
<exclude>oracle.javacard:*</exclude>
328+
<exclude>oracle.javacard:*:*:*</exclude>
329+
<exclude>*:api_classic</exclude>
330+
<exclude>oracle.javacard:api_classic</exclude>
331+
</excludes>
304332
</artifactSet>
305333
<filters>
306334
<filter>
@@ -321,10 +349,17 @@
321349
<goal>shade</goal>
322350
</goals>
323351
<configuration>
352+
<keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
324353
<artifactSet>
325354
<includes>
326355
<include>org.bouncycastle:*</include>
327356
</includes>
357+
<excludes>
358+
<exclude>oracle.javacard:*</exclude>
359+
<exclude>oracle.javacard:*:*:*</exclude>
360+
<exclude>*:api_classic</exclude>
361+
<exclude>oracle.javacard:api_classic</exclude>
362+
</excludes>
328363
</artifactSet>
329364
<minimizeJar>true</minimizeJar>
330365
<shadedArtifactAttached>true</shadedArtifactAttached>
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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

Comments
 (0)