Skip to content

Commit 8dcdf5e

Browse files
committed
SpringBootFatJarMain only dependency is spring-boot-loader
Closes gh-91
1 parent bcabf13 commit 8dcdf5e

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

spring-boot-testjars/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ checkstyle {
2222

2323
dependencies {
2424
checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.40")
25+
compileOnly 'org.springframework.boot:spring-boot-loader'
2526
implementation platform('org.springframework.boot:spring-boot-dependencies:3.4.0')
2627
implementation 'org.apache.commons:commons-exec:1.3'
2728
implementation 'org.springframework:spring-test'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.experimental.boot.server.exec.detector;
18+
19+
import org.springframework.lang.Nullable;
20+
21+
final class ClassUtils {
22+
23+
/**
24+
* Wrapper for Class.forName to allow mocking in tests. Note that we cannot use
25+
* Spring's ClassUtils because it will not be available on the classpath.
26+
* @param name the name of the Class
27+
* @param classLoader the class loader to use (can be {@code null}, which indicates
28+
* the default class loader)
29+
* @return a class instance for the supplied name
30+
* @throws ClassNotFoundException if the class was not found
31+
* @throws LinkageError if the class file could not be loaded
32+
* @see Class#forName(String, boolean, ClassLoader)
33+
*/
34+
static Class<?> forName(String name, @Nullable ClassLoader classLoader)
35+
throws ClassNotFoundException, LinkageError {
36+
37+
if (name == null) {
38+
throw new IllegalArgumentException("name must not be null");
39+
}
40+
41+
return Class.forName(name, false, classLoader);
42+
}
43+
44+
private ClassUtils() {
45+
}
46+
47+
}

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/detector/SpringBootFatJarMain.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818

1919
import java.lang.reflect.InvocationTargetException;
2020

21-
import org.apache.commons.logging.Log;
22-
import org.apache.commons.logging.LogFactory;
23-
24-
import org.springframework.core.log.LogMessage;
25-
import org.springframework.experimental.boot.server.exec.imports.GenericSpringBootApplicationMain;
26-
import org.springframework.util.ClassUtils;
21+
import org.springframework.boot.loader.log.DebugLogger;
2722

2823
/**
2924
* Detect which JarLauncher main class to use, and call its {@code main(String[] args)}
@@ -37,7 +32,7 @@
3732
*/
3833
public class SpringBootFatJarMain {
3934

40-
private static Log log = LogFactory.getLog(SpringBootFatJarMain.class);
35+
private static DebugLogger debug = DebugLogger.get(SpringBootFatJarMain.class);
4136

4237
static final String SPRING_BOOT_32_PLUS_LAUNCHER_CLASSNAME = "org.springframework.boot.loader.launch.JarLauncher";
4338

@@ -59,17 +54,16 @@ public static void main(String[] args) {
5954
}
6055

6156
private static boolean runMain(String className, String[] args, String description) {
62-
log.debug(LogMessage.format("Trying to run as %s using %s.main(String[])", description, className));
57+
debug.log("Trying to run as %s using %s.main(String[])", description, className);
6358
try {
64-
Class<?> jarLauncher = ClassUtils.forName(className,
65-
GenericSpringBootApplicationMain.class.getClassLoader());
59+
Class<?> jarLauncher = ClassUtils.forName(className, SpringBootFatJarMain.class.getClassLoader());
6660
var mainMethod = jarLauncher.getMethod("main", String[].class);
6761
mainMethod.invoke(null, (Object) args);
68-
log.debug(LogMessage.format("Successfully ran as %s", description));
62+
debug.log("Successfully ran as %s", description);
6963
return true;
7064
}
7165
catch (ClassNotFoundException ex) {
72-
log.debug(LogMessage.format("Failed to run as %s", description), ex);
66+
debug.log("Failed to run as %s", description, ex);
7367
}
7468
catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
7569
throw new RuntimeException(ex);

spring-boot-testjars/src/test/java/org/springframework/experimental/boot/server/exec/detector/SpringBootFatJarMainTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import org.junit.jupiter.api.Test;
2121
import org.mockito.Mockito;
2222

23-
import org.springframework.util.ClassUtils;
24-
2523
import static org.assertj.core.api.Assertions.assertThat;
2624
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2725
import static org.mockito.ArgumentMatchers.any;

0 commit comments

Comments
 (0)