1
+ package maven ;
2
+ import common .CommonUtil ;
3
+ import entity .DependencyInfo ;
4
+ import org .springframework .boot .loader .jar .JarFile ; // 偷懒 直接使用springboot的
5
+ import java .io .File ;
6
+ import java .util .Enumeration ;
7
+ import java .util .List ;
8
+ import java .util .Properties ;
9
+ import java .util .jar .JarEntry ;
10
+
11
+ /**
12
+ * @author fate
13
+ * @date 2019-11-22 上午11:38
14
+ * 用于处理fat jar资源的获取
15
+ */
16
+ public class FatJarHandle {
17
+
18
+ /**
19
+ * fat jar 依赖文件的获取,多用于处理springboot打包的jar 传入的path是这样的 jar:file:/home/q/system/java/live/build/libs/live-33541.a12ed7cc.jar!/BOOT-INF/classes!/
20
+ * @param jarpath
21
+ * @param dependencyInfoList
22
+ * @return
23
+ */
24
+ public static List <DependencyInfo > getDependencyInfo (String jarpath , List <DependencyInfo > dependencyInfoList ) {
25
+
26
+ try {
27
+
28
+ JarFile jarFile = new JarFile (new File (getROOTJar (jarpath )));
29
+
30
+ Enumeration <JarEntry > jarEntryEnumeration = jarFile .entries ();
31
+
32
+ while (jarEntryEnumeration .hasMoreElements ()) {
33
+
34
+ JarEntry jarEntry = jarEntryEnumeration .nextElement ();
35
+
36
+ if (jarEntry .getName ().endsWith (".jar" )) { // 这里就暂时不匹配BOOT-INF/lib,考虑通用性
37
+
38
+ JarFile inJarFile = jarFile .getNestedJarFile (jarEntry );
39
+ DependencyInfo dependencyInfo = getJarInJardependcyInfo (inJarFile ); // 获取资源
40
+
41
+ if (dependencyInfo != null ) dependencyInfoList .add (dependencyInfo );
42
+
43
+ }
44
+ }
45
+
46
+ }
47
+ catch (Exception e ) {
48
+
49
+ CommonUtil .writeStr ("/tmp/jvm_error.txt" ,"getDependencyInfo:\t " + e .getMessage ());
50
+ }
51
+
52
+ return dependencyInfoList ;
53
+ }
54
+
55
+ /**
56
+ * 获取Jarinjar中的资源
57
+ * @param jarFile
58
+ * @return
59
+ */
60
+ public static DependencyInfo getJarInJardependcyInfo (JarFile jarFile ) {
61
+
62
+ try {
63
+
64
+ Enumeration <JarEntry > jarEntryEnumeration = jarFile .entries ();
65
+
66
+ while (jarEntryEnumeration .hasMoreElements ()) {
67
+
68
+ JarEntry jarEntry = jarEntryEnumeration .nextElement ();
69
+
70
+ if (jarEntry .getName ().endsWith ("/pom.properties" )) {
71
+
72
+ Properties prop = new Properties ();
73
+ prop .load (jarFile .getInputStream (jarEntry ));
74
+
75
+ DependencyInfo dependencyInfo = new DependencyInfo (); // 存放依赖信息
76
+ dependencyInfo .setArtifactId (prop .getProperty ("artifactId" ));
77
+ dependencyInfo .setGroupId (prop .getProperty ("groupId" ));
78
+ dependencyInfo .setVersion (prop .getProperty ("version" ));
79
+
80
+ return dependencyInfo ;
81
+ }
82
+ }
83
+
84
+ }
85
+ catch (Exception e ) {
86
+
87
+ CommonUtil .writeStr ("/tmp/jvm_error.txt" ,"getJarInJardependcyInfo:\t " + e .getMessage ());
88
+ }
89
+
90
+ return null ;
91
+
92
+ }
93
+
94
+ /**
95
+ * 获取rootjar资源路径
96
+ * @param jarPath
97
+ * @return
98
+ */
99
+ public static String getROOTJar (String jarPath ) {
100
+
101
+ jarPath = jarPath .split (".jar!/" )[0 ].replace ("jar:file:" ,"" );
102
+
103
+ return jarPath + ".jar" ;
104
+ }
105
+
106
+ }
0 commit comments