Skip to content

Commit 19f3572

Browse files
committed
not exclude when using maven coordinate having different group id
1 parent 8732814 commit 19f3572

File tree

2 files changed

+93
-9
lines changed

2 files changed

+93
-9
lines changed

classpath-replacer-core/src/main/java/com/freemanan/cr/core/ModifiedClassPathClassLoaderGenerator.java

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.freemanan.cr.core.action;
2+
3+
import static com.freemanan.cr.core.anno.Verb.ADD;
4+
import static com.freemanan.cr.core.anno.Verb.EXCLUDE;
5+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
8+
import com.freemanan.cr.core.anno.Action;
9+
import com.freemanan.cr.core.anno.ClasspathReplacer;
10+
import org.junit.jupiter.api.Test;
11+
12+
/**
13+
* {@link Exclude} tester.
14+
*/
15+
class ExcludeTest {
16+
17+
@Test
18+
@ClasspathReplacer({
19+
@Action(verb = ADD, value = "org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.5"),
20+
@Action(
21+
verb = EXCLUDE,
22+
value = "org.springframework:spring-cloud-starter-bootstrap:3.1.5"), // different group id
23+
})
24+
void testKnownIssue_whenMavenProjectHasDifferentGroupId_thenExcludePossibly() {
25+
// TODO: known issue, shouldn't not exclude when different group id
26+
assertThrows(ClassNotFoundException.class, () -> {
27+
Class.forName("org.springframework.cloud.bootstrap.marker.Marker");
28+
});
29+
}
30+
31+
@Test
32+
@ClasspathReplacer({
33+
@Action(verb = ADD, value = "org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.5"),
34+
@Action(verb = EXCLUDE, value = "org.spring:spring-cloud-starter-bootstrap:3.1.5"), // different group id
35+
})
36+
void notExclude_whenDifferentGroupId() {
37+
assertDoesNotThrow(() -> {
38+
Class.forName("org.springframework.cloud.bootstrap.marker.Marker");
39+
});
40+
}
41+
}

0 commit comments

Comments
 (0)