forked from OpenLiberty/ci.common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringBootUtil.java
More file actions
69 lines (62 loc) · 3 KB
/
Copy pathSpringBootUtil.java
File metadata and controls
69 lines (62 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* (C) Copyright IBM Corporation 2018, 2026.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.openliberty.tools.common.plugins.util;
import java.io.File;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.jar.Manifest;
import java.util.Enumeration;
public class SpringBootUtil {
public static final String BOOT_VERSION_ATTRIBUTE = "Spring-Boot-Version";
public static final String BOOT_START_CLASS_ATTRIBUTE = "Start-Class";
// Updated regex to support both old (.RELEASE) and new versioning schemes (Spring Boot 3.0+, 4.0+)
// Matches: spring-boot-X.Y.Z.RELEASE.jar (old) or spring-boot-X.Y.Z.jar (new)
public static final String BOOT_JAR_EXPRESSION = "BOOT-INF/lib/spring-boot-\\d+[\\.]\\d+[\\.]\\d+(\\.RELEASE)?\\.jar";
public static final String BOOT_WAR_EXPRESSION = "WEB-INF/lib/spring-boot-\\d+[\\.]\\d+[\\.]\\d+(\\.RELEASE)?\\.jar";
/**
* Check whether the given artifact is a Spring Boot Uber JAR
* @param artifact
* @return true if so, false otherwise
*/
public static boolean isSpringBootUberJar(File artifact) {
if (artifact == null || !artifact.exists() || !artifact.isFile()) {
return false;
}
try (JarFile jarFile = new JarFile(artifact)) {
Manifest manifest = jarFile.getManifest();
if (manifest != null) {
Attributes attributes = manifest.getMainAttributes();
if(attributes.getValue(BOOT_VERSION_ATTRIBUTE) != null
&& attributes.getValue(BOOT_START_CLASS_ATTRIBUTE) != null) {
return true;
} else { //Checking that there is a spring-boot-VERSION.RELEASE.jar in the BOOT-INF/lib directory
//Handles the Gradle case where the spring plugin does not set the properties in the manifest
Enumeration<JarEntry> entries = jarFile.entries();
while(entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String entryName = entry.getName();
if (!entryName.startsWith("org") && (entryName.matches(BOOT_JAR_EXPRESSION) || entryName.matches(BOOT_WAR_EXPRESSION))) {
return true;
}
}
}
}
} catch (IOException e) {}
return false;
}
}