@@ -155,7 +155,12 @@ private boolean needRemove(
155155 String artifactId = gav [1 ];
156156 String version = gav [2 ];
157157 String jarName = artifactId + "-" + version + ".jar" ;
158- return jarName .equals (fileName (url ));
158+ if (!Objects .equals (jarName , fileName (url ))) {
159+ return false ;
160+ }
161+ // compare group id
162+ String [] groupArr = gav [0 ].split ("\\ ." );
163+ return isSameGroupIdWithExactMatch (url , groupArr );
159164 }
160165 return patternToJars
161166 .computeIfAbsent (pattern , s -> resolveCoordinates (new String [] {pattern }, classpathReplacer ))
@@ -213,9 +218,6 @@ private boolean needRemove(
213218 }
214219
215220 private static boolean isSameJar (URL url , URL mavenJarUrl ) {
216- // if gradle project, jar cache path is like
217- // ~/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.6/3f7e1e9e8e1b0e1e8e1b0e1b0e1b0e1b0e1b0e1b/gson-2.8.6.jar
218- // if maven project, jar cache path is like ~/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar
219221 if (url == null || mavenJarUrl == null ) {
220222 return false ;
221223 }
@@ -236,20 +238,61 @@ private static boolean isSameJar(URL url, URL mavenJarUrl) {
236238
237239 // compare group id
238240 // remove the last 3 '/' for maven jar path
239- String urlStr = url .toString ();
240241 String mavenJarStr = mavenJarUrl .toString ();
241242 String temp = mavenJarStr .substring (0 , mavenJarStr .lastIndexOf ("/" ));
242243 temp = temp .substring (0 , temp .lastIndexOf ("/" ));
243244 String [] arr = temp .substring (0 , temp .lastIndexOf ("/" )).split ("/" );
245+ return isSameGroupIdWithFuzzyMatch (url , arr );
246+ }
247+
248+ /**
249+ * Only take the last 2 elements of the group id array to compare.
250+ *
251+ * @param url jar url
252+ * @param groupIdArr maven group id array, e.g. [com, google, code, gson]
253+ * @return true if the group id is same
254+ */
255+ private static boolean isSameGroupIdWithFuzzyMatch (URL url , String [] groupIdArr ) {
256+ String urlStr = url .toString ();
257+
258+ // gradle project, jar cache path is like
259+ // ~/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.6/3f7e1e9e8e1b0e1e8e1b0e1b0e1b0e1b0e1b0e1b/gson-2.8.6.jar
260+ String gradlePartGroupId = groupIdArr [groupIdArr .length - 2 ] + "." + groupIdArr [groupIdArr .length - 1 ];
261+ if (urlStr .contains (gradlePartGroupId )) {
262+ return true ;
263+ }
244264
245- // gradle
246- String gradlePartGroupId = arr [arr .length - 2 ] + "." + arr [arr .length - 1 ];
265+ // maven project, jar cache path is like ~/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar
266+ String mavenPartGroupId = groupIdArr [groupIdArr .length - 2 ] + "/" + groupIdArr [groupIdArr .length - 1 ];
267+ if (urlStr .contains (mavenPartGroupId )) {
268+ return true ;
269+ }
270+
271+ // TODO: here to support the other project types, non maven and non gradle
272+
273+ // artifact id is the same, but group id is different
274+ return false ;
275+ }
276+
277+ /**
278+ * Url must contain all the elements in groupIdArr.
279+ *
280+ * @param url jar url
281+ * @param groupIdArr maven group id array, e.g. [com, google, code, gson]
282+ * @return true if the group id is same
283+ */
284+ private static boolean isSameGroupIdWithExactMatch (URL url , String [] groupIdArr ) {
285+ String urlStr = url .toString ();
286+
287+ // gradle project, jar cache path is like
288+ // ~/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.6/3f7e1e9e8e1b0e1e8e1b0e1b0e1b0e1b0e1b0e1b/gson-2.8.6.jar
289+ String gradlePartGroupId = "/" + String .join ("." , groupIdArr ) + "/" ;
247290 if (urlStr .contains (gradlePartGroupId )) {
248291 return true ;
249292 }
250293
251- // maven
252- String mavenPartGroupId = arr [ arr . length - 2 ] + "/" + arr [ arr . length - 1 ] ;
294+ // maven project, jar cache path is like ~/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar
295+ String mavenPartGroupId = "/" + String . join ( "/" , groupIdArr ) + "/" ;
253296 if (urlStr .contains (mavenPartGroupId )) {
254297 return true ;
255298 }
0 commit comments