@@ -41,12 +41,14 @@ func main() {
41
41
flag .String ("target" , "." , "Path to userlib." )
42
42
flag .Bool ("clean" , false , "Turn on to actually remove the duplicate JARs." )
43
43
flag .Bool ("verbose" , false , "Turn on to see debug information." )
44
+ flag .String ("mode" , "auto" , "Jar parsing mode. Supported options: auto, strict" )
44
45
45
46
pflag .CommandLine .AddGoFlagSet (flag .CommandLine )
46
47
pflag .Parse ()
47
48
viper .BindPFlags (pflag .CommandLine )
48
49
49
50
targetDir := viper .GetString ("target" )
51
+ mode := viper .GetString ("mode" )
50
52
clean := viper .GetBool ("clean" )
51
53
verbose := viper .GetBool ("verbose" )
52
54
@@ -61,7 +63,7 @@ func main() {
61
63
logging .SetLevel (logging .INFO , "main" )
62
64
}
63
65
64
- jars := listAllJars (targetDir )
66
+ jars := listAllJars (targetDir , mode )
65
67
keepJars := computeJarsToKeep (jars )
66
68
count := cleanJars (clean , jars , keepJars )
67
69
@@ -74,7 +76,7 @@ func main() {
74
76
75
77
}
76
78
77
- func listAllJars (targetDir string ) []JarProperties {
79
+ func listAllJars (targetDir string , mode string ) []JarProperties {
78
80
log .Info ("Finding and parsing JARs" )
79
81
files , err := ioutil .ReadDir (targetDir )
80
82
if err != nil {
@@ -85,7 +87,7 @@ func listAllJars(targetDir string) []JarProperties {
85
87
if strings .HasSuffix (f .Name (), ".jar" ) {
86
88
log .Debugf ("Processing JAR: %v" , f .Name ())
87
89
filePath := filepath .Join (targetDir , f .Name ())
88
- jarProp := getJarProps (filePath )
90
+ jarProp := getJarProps (filePath , mode )
89
91
if strings .Compare (jarProp .filePath , "" ) != 0 {
90
92
jars = append (jars , jarProp )
91
93
}
@@ -94,7 +96,7 @@ func listAllJars(targetDir string) []JarProperties {
94
96
return jars
95
97
}
96
98
97
- func getJarProps (filePath string ) JarProperties {
99
+ func getJarProps (filePath string , mode string ) JarProperties {
98
100
99
101
archive , err := zip .OpenReader (filePath )
100
102
if err != nil {
@@ -138,11 +140,6 @@ func getJarProps(filePath string) JarProperties {
138
140
log .Warningf ("Unable to read file: %v" , err )
139
141
}
140
142
141
- // err = os.Remove(file.Name())
142
- // if err != nil {
143
- // log.Fatal(err)
144
- // }
145
-
146
143
// try manifest first
147
144
text := string (b )
148
145
jar1 := parseManifest (filePath , text )
@@ -156,6 +153,15 @@ func getJarProps(filePath string) JarProperties {
156
153
return jar2
157
154
}
158
155
}
156
+
157
+ if mode == "auto" {
158
+ jar3 := parseOptimistic (filePath )
159
+ if jar3 .packageName != "" {
160
+ log .Debugf ("Parsed properties optimistically: %v" , jar3 )
161
+ return jar3
162
+ }
163
+ }
164
+
159
165
log .Warningf ("Failed to parse metadata from %v" , filePath )
160
166
161
167
return JarProperties {filePath : "" }
@@ -214,6 +220,43 @@ func parsePOM(filePath string, text string) JarProperties {
214
220
return jarProp
215
221
}
216
222
223
+ func parseOptimistic (filePath string ) JarProperties {
224
+ // filePath = junit-4.11.jar
225
+ jarProp := JarProperties {filePath : filePath , packageName : "" , fileName : filepath .Base (filePath )}
226
+
227
+ // version
228
+ tokens := strings .Split (filePath , "-" )
229
+ if len (tokens ) > 1 {
230
+ jarProp .version = strings .Replace (tokens [len (tokens )- 1 ], ".jar" , "" , 1 )
231
+ jarProp .versionNumber = convertVersionToNumber (jarProp .version )
232
+ }
233
+
234
+ archive , err := zip .OpenReader (filePath )
235
+ if err != nil {
236
+ panic (err )
237
+ }
238
+ defer archive .Close ()
239
+ re := regexp .MustCompile (`(org|com)/.*\.class$` )
240
+
241
+ for _ , f := range archive .File {
242
+ if match := re .MatchString (f .Name ); match {
243
+ tokens = strings .Split (f .Name , "/" )
244
+ if len (tokens ) > 3 {
245
+ // eg. org/example/hello/MyClass.class
246
+ tokens = tokens [:3 ]
247
+ } else if len (tokens ) > 2 {
248
+ // eg. org/example/MyClass.class
249
+ tokens = tokens [:2 ]
250
+ } else {
251
+ tokens = tokens [:1 ]
252
+ }
253
+ jarProp .packageName = strings .Join (tokens , "." )
254
+ break
255
+ }
256
+ }
257
+ return jarProp
258
+ }
259
+
217
260
func computeJarsToKeep (jars []JarProperties ) map [string ]JarProperties {
218
261
log .Info ("Computing duplicates" )
219
262
var keepJars = make (map [string ]JarProperties )
0 commit comments