Skip to content

Commit ecdfa92

Browse files
committed
GH-1740: fixed issue with not exactly identifying the boot version of the project
1 parent a74bd32 commit ecdfa92

File tree

5 files changed

+93
-12
lines changed

5 files changed

+93
-12
lines changed

headless-services/commons/commons-java/src/main/java/org/springframework/ide/vscode/commons/java/IClasspath.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2025 Pivotal, Inc.
2+
* Copyright (c) 2016, 2026 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -10,7 +10,6 @@
1010
*******************************************************************************/
1111
package org.springframework.ide.vscode.commons.java;
1212

13-
import java.io.File;
1413
import java.util.Collection;
1514
import java.util.Optional;
1615

@@ -43,14 +42,29 @@ public interface IClasspath {
4342

4443
/**
4544
* Finds a classpath entry among JAR libraries that start with a prefix. Prefix must typically contain the full lib name such that the match is only one.
45+
*
4646
* @param prefix the library prefix
4747
* @return the classpath entry
4848
*/
4949
default Optional<CPE> findBinaryLibrary(String prefix) {
50+
return findBinaryLibrary(prefix, false);
51+
}
52+
53+
/**
54+
* Finds a classpath entry among JAR libraries that start with a prefix. Prefix must typically contain the full lib name such that the match is only one.
55+
*
56+
* @param prefix the library prefix
57+
* @return the classpath entry
58+
*/
59+
default Optional<CPE> findBinaryLibrary(String prefix, boolean exactName) {
5060
try {
5161
for (CPE cpe : getClasspathEntries()) {
52-
if (Classpath.isBinary(cpe) && !cpe.isSystem() && !cpe.isTest() && new File(cpe.getPath()).getName().startsWith(prefix)) {
53-
return Optional.of(cpe);
62+
if (Classpath.isBinary(cpe) && !cpe.isSystem() && !cpe.isTest()) {
63+
if (exactName && cpe.getName().equals(prefix)) {
64+
return Optional.of(cpe);
65+
} else if (!exactName && cpe.getName().startsWith(prefix)) {
66+
return Optional.of(cpe);
67+
}
5468
}
5569
}
5670
} catch (Exception e) {

headless-services/commons/commons-java/src/main/java/org/springframework/ide/vscode/commons/java/SpringProjectUtil.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2023 Pivotal, Inc.
2+
* Copyright (c) 2017, 2026 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -79,8 +79,12 @@ public static Version getVersionFromGeneration(String name) throws Exception {
7979
throw new IllegalArgumentException("Invalid semver. Unable to parse major and minor version from: " + name);
8080
}
8181

82-
public static Version getDependencyVersion(IJavaProject jp, String dependency) {
83-
return jp.getClasspath().findBinaryLibrary(dependency).map(cpe -> cpe.getVersion()).orElse(null);
82+
public static Version getDependencyVersion(IJavaProject jp, String dependency) {
83+
return getDependencyVersion(jp, dependency, false);
84+
}
85+
86+
public static Version getDependencyVersion(IJavaProject jp, String dependency, boolean exactName) {
87+
return jp.getClasspath().findBinaryLibrary(dependency, exactName).map(cpe -> cpe.getVersion()).orElse(null);
8488
}
8589

8690
public static boolean hasDependencyStartingWith(IJavaProject jp, String dependency, Predicate<CPE> filter) {
@@ -109,9 +113,8 @@ public static boolean hasDependencyStartingWith(IJavaProject jp, String dependen
109113
}).isPresent();
110114
}
111115

112-
113116
public static Version getSpringBootVersion(IJavaProject jp) {
114-
return getDependencyVersion(jp, SPRING_BOOT);
117+
return getDependencyVersion(jp, SPRING_BOOT, true);
115118
}
116119

117120
public static Predicate<IJavaProject> springBootVersionGreaterOrEqual(int major, int minor, int patch) {

headless-services/commons/commons-lsp-extensions/src/main/java/org/springframework/ide/vscode/commons/protocol/java/Classpath.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018, 2019 Pivotal, Inc.
2+
* Copyright (c) 2018, 2026 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -82,6 +82,7 @@ public static class CPE {
8282
private Map<String, String> extra;
8383

8484
transient private Version version;
85+
transient private String name;
8586

8687
public CPE() {}
8788

@@ -233,6 +234,15 @@ public Version getVersion() {
233234
return version;
234235
}
235236

237+
public String getName() {
238+
if (name == null) {
239+
if (ENTRY_KIND_BINARY.equals(getKind()) && !isSystem) {
240+
name = getDependencyName(new File(getPath()).getName());
241+
}
242+
}
243+
return name;
244+
}
245+
236246
}
237247

238248
public static boolean isSource(CPE e) {
@@ -271,4 +281,25 @@ static Version getDependencyVersion(String fileName) {
271281
return null;
272282
}
273283

284+
static String getDependencyName(String fileName) {
285+
Matcher matcher = VERSION_PATTERN.matcher(fileName);
286+
if (matcher.find()) {
287+
// Extract the name part before the version
288+
int versionStart = matcher.start(1);
289+
if (versionStart > 0) {
290+
// Remove the trailing dash or dot before the version
291+
String nameWithSeparator = fileName.substring(0, versionStart);
292+
if (nameWithSeparator.endsWith("-") || nameWithSeparator.endsWith(".")) {
293+
return nameWithSeparator.substring(0, nameWithSeparator.length() - 1);
294+
}
295+
return nameWithSeparator;
296+
}
297+
}
298+
// If no version pattern found, return filename without .jar extension
299+
if (fileName.endsWith(".jar")) {
300+
return fileName.substring(0, fileName.length() - 4);
301+
}
302+
return fileName;
303+
}
304+
274305
}

headless-services/commons/commons-lsp-extensions/src/test/java/org/springframework/ide/vscode/commons/protocol/java/ClasspathTests.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 VMware, Inc.
2+
* Copyright (c) 2023, 2026 VMware, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -51,5 +51,37 @@ void testDependencyVersionCalculation2() throws Exception {
5151
assertEquals("BUILD-SNAPSHOT", version.getQualifier());
5252
}
5353

54+
@Test
55+
void testDependencyNameExtraction() throws Exception {
56+
String name = Classpath.getDependencyName("spring-boot-1.2.3.jar");
57+
assertEquals("spring-boot", name);
58+
59+
name = Classpath.getDependencyName("spring-boot-1.2.3-RELEASE.jar");
60+
assertEquals("spring-boot", name);
61+
62+
name = Classpath.getDependencyName("spring-boot-1.2.3.RELEASE.jar");
63+
assertEquals("spring-boot", name);
64+
65+
name = Classpath.getDependencyName("spring-boot-1.2.3.BUILD-SNAPSHOT.jar");
66+
assertEquals("spring-boot", name);
67+
68+
name = Classpath.getDependencyName("spring-boot-actuator-1.2.3.BUILD-SNAPSHOT.jar");
69+
assertEquals("spring-boot-actuator", name);
70+
71+
name = Classpath.getDependencyName("commons-lang3-3.12.0.jar");
72+
assertEquals("commons-lang3", name);
73+
74+
name = Classpath.getDependencyName("jackson-databind-2.13.4.jar");
75+
assertEquals("jackson-databind", name);
76+
77+
// Test case where no version pattern is found
78+
name = Classpath.getDependencyName("some-library.jar");
79+
assertEquals("some-library", name);
80+
81+
// Test case with no .jar extension
82+
name = Classpath.getDependencyName("some-library");
83+
assertEquals("some-library", name);
84+
}
85+
5486

5587
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/validation/generations/GenerationsValidator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public GenerationsValidator(DiagnosticSeverityProvider diagnosticSeverityProvide
4343

4444
public static Generation getGenerationForJavaProject(IJavaProject javaProject, ResolvedSpringProject springProject) throws Exception {
4545
List<Generation> genList = springProject.getGenerations();
46-
Version javaProjectVersion = SpringProjectUtil.getDependencyVersion(javaProject, springProject.getSlug());
46+
Version javaProjectVersion = SpringProjectUtil.getDependencyVersion(javaProject, springProject.getSlug(), true);
47+
4748
if (javaProjectVersion == null) {
4849
return null;
4950
}

0 commit comments

Comments
 (0)