Add testClasspath option to run and start goals

Deprecate the `useTestClasspath` flag in favor of a `testClasspath`
parameter on the `run` and `start` goals. It accepts `off`,
`dependencies`, or `all`, with `all` also adding the project's test
classes directory to the classpath so that a main class located in
`src/test/java` can be found and run, matching the `test-run` goal's
existing behaviour.

Closes gh-36115
This commit is contained in:
Stéphane Nicoll
2026-08-07 16:40:21 +02:00
parent c35043626c
commit bb23ef72b6
18 changed files with 568 additions and 30 deletions
@@ -95,8 +95,31 @@ class RunIntegrationTests {
}
@TestTemplate
@Deprecated(since = "4.2.0", forRemoval = true)
void whenUseTestClasspathIsEnabledTheApplicationHasTestDependenciesOnItsClasspath(MavenBuild mavenBuild) {
mavenBuild.project("run-use-test-classpath")
.goals("spring-boot:run")
.execute((project) -> assertThat(buildLog(project)).contains("I haz been run")
.contains("useTestClasspath is deprecated, use testClasspath instead."));
}
@TestTemplate
void whenTestClasspathIsDependenciesTheApplicationHasTestDependenciesOnItsClasspath(MavenBuild mavenBuild) {
mavenBuild.project("run-test-classpath-dependencies")
.goals("spring-boot:run")
.execute((project) -> assertThat(buildLog(project)).contains("I haz been run"));
}
@TestTemplate
void whenTestClasspathIsAllTheApplicationHasTestAndClassesDependenciesOnItsClasspath(MavenBuild mavenBuild) {
mavenBuild.project("run-test-classpath-all")
.goals("spring-boot:run")
.execute((project) -> assertThat(buildLog(project)).contains("I haz been run"));
}
@TestTemplate
void whenTestClasspathIsOffTheApplicationHasNeitherTestNorClassesDependenciesOnItsClasspath(MavenBuild mavenBuild) {
mavenBuild.project("run-test-classpath-off")
.goals("spring-boot:run")
.execute((project) -> assertThat(buildLog(project)).contains("I haz been run"));
}
@@ -34,7 +34,17 @@ class StartStopIntegrationTests {
@TestTemplate
void startStopWaitsForApplicationToBeReadyAndThenRequestsShutdown(MavenBuild mavenBuild) {
mavenBuild.project("start-stop")
testStartStop(mavenBuild, "start-stop");
}
@TestTemplate
void startStopWithTestClasspathWaitsForApplicationToBeReadyAndThenRequestsShutdown(MavenBuild mavenBuild) {
testStartStop(mavenBuild, "start-stop-test");
}
private void testStartStop(MavenBuild mavenBuild, String projectId) {
mavenBuild.project(projectId)
.goals("verify")
.execute((project) -> assertThat(buildLog(project)).contains("isReady: true")
.contains("Shutdown requested"));
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-test-classpath-all</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<testClasspath>all</testClasspath>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring-framework.version@</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,33 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
public class SampleApplication {
public static void main(String[] args) {
Class<?> appContext = null;
try {
appContext = Class.forName("org.springframework.context.ApplicationContext");
}
catch (ClassNotFoundException e) {
throw new IllegalStateException("Test dependencies not added to classpath", e);
}
System.out.println("I haz been run");
}
}
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-test-classpath-dependencies</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<testClasspath>dependencies</testClasspath>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring-framework.version@</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,38 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
public class SampleApplication {
public static void main(String[] args) {
try {
Class.forName("org.springframework.context.ApplicationContext");
}
catch (ClassNotFoundException e) {
throw new IllegalStateException("Test dependencies not added to classpath", e);
}
try {
Class.forName("org.test.SampleTest");
throw new IllegalStateException("Test classes should not have been added to classpath");
}
catch (ClassNotFoundException e) {
// OK
}
System.out.println("I haz been run");
}
}
@@ -0,0 +1,21 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
public class SampleTest {
}
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-test-classpath-off</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<testClasspath>off</testClasspath>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>@spring-framework.version@</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,39 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
public class SampleApplication {
public static void main(String[] args) {
try {
Class.forName("org.springframework.context.ApplicationContext");
throw new IllegalStateException("Test dependencies should not have been added to classpath");
}
catch (ClassNotFoundException e) {
// OK
}
try {
Class.forName("org.test.SampleTest");
throw new IllegalStateException("Test classes should not have been added to classpath");
}
catch (ClassNotFoundException e) {
// OK
}
System.out.println("I haz been run");
}
}
@@ -0,0 +1,21 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
public class SampleTest {
}
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>start-stop-test</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>@build-helper-maven-plugin.version@</version>
<executions>
<execution>
<id>reserve-jmx-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>jmx.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<jmxPort>${jmx.port}</jmxPort>
<testClasspath>all</testClasspath>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,37 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
final class SpringApplicationAdmin implements SpringApplicationAdminMXBean {
boolean ready;
boolean shutdownInvoked;
@Override
public boolean isReady() {
System.out.println("isReady: " + this.ready);
return this.ready;
}
@Override
public void shutdown() {
this.shutdownInvoked = true;
System.out.println("Shutdown requested");
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
public interface SpringApplicationAdminMXBean {
boolean isReady();
void shutdown();
}
@@ -0,0 +1,54 @@
/*
* Copyright 2012-present the original author or authors.
*
* 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
*
* https://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 org.test;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
/**
* This sample app simulates the JMX Mbean that is exposed by the Spring Boot application.
*/
public class SampleApplication {
private static final Object lock = new Object();
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName(
"org.springframework.boot:type=Admin,name=SpringApplication");
SpringApplicationAdmin mbean = new SpringApplicationAdmin();
mbs.registerMBean(mbean, name);
// Flag the app as ready
mbean.ready = true;
int waitAttempts = 0;
while (!mbean.shutdownInvoked) {
if (waitAttempts > 30) {
throw new IllegalStateException(
"Shutdown should have been invoked by now");
}
synchronized (lock) {
lock.wait(250);
}
waitAttempts++;
}
}
}
@@ -205,6 +205,16 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
@SuppressWarnings("NullAway.Init")
private File classesDirectory;
/**
* Directory containing the test classes and resource files that can be included when
* running the application.
*
* @since 4.2.0
*/
@Parameter(defaultValue = "${project.build.testOutputDirectory}", required = true)
@SuppressWarnings("NullAway.Init")
private File testClassesDirectory;
/**
* Skip the execution.
*
@@ -241,10 +251,19 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
* @since 3.1.0
*/
protected List<File> getClassesDirectories() {
return List.of(this.classesDirectory);
List<File> classesDirectories = new ArrayList<>();
if (testClasspath() == TestClasspath.ALL) {
classesDirectories.add(this.testClassesDirectory);
}
classesDirectories.add(this.classesDirectory);
return Collections.unmodifiableList(classesDirectories);
}
protected abstract boolean isUseTestClasspath();
/**
* Return the {@link TestClasspath} strategy to use.
* @return the test classpath strategy
*/
protected abstract TestClasspath testClasspath();
private void run(String startClassName) throws MojoExecutionException, MojoFailureException {
List<String> args = new ArrayList<>();
@@ -416,7 +435,9 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
private void addDependencies(List<URL> urls) throws MalformedURLException, MojoExecutionException {
Set<Artifact> artifacts = (isUseTestClasspath()) ? filterDependencies(this.project.getArtifacts())
TestClasspath testClasspath = testClasspath();
Set<Artifact> artifacts = ((testClasspath == TestClasspath.ALL || testClasspath == TestClasspath.DEPENDENCIES))
? filterDependencies(this.project.getArtifacts())
: filterDependencies(this.project.getArtifacts(), new ExcludeTestScopeArtifactFilter());
for (Artifact artifact : artifacts) {
if (artifact.getFile() != null) {
@@ -432,4 +453,27 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
}
/**
* Strategies to handle the test classpath when running an application.
*/
protected enum TestClasspath {
/**
* Do not include the test classpath at all.
*/
OFF,
/**
* Include only dependencies with test scope.
*/
DEPENDENCIES,
/**
* Include the full test classpath (test classes and dependencies with test
* scope).
*/
ALL
}
}
@@ -18,6 +18,7 @@ package org.springframework.boot.maven;
import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.inject.Inject;
@@ -30,6 +31,7 @@ import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.toolchain.ToolchainManager;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.loader.tools.RunProcess;
@@ -57,9 +59,24 @@ public class RunMojo extends AbstractRunMojo {
/**
* Flag to include the test classpath when running.
* @since 1.3.0
* @deprecated since 4.2.0 for removal in 4.4.0 in favor of {@code testClasspath}
*/
@Parameter(property = "spring-boot.run.useTestClasspath", defaultValue = "false")
private boolean useTestClasspath;
@Parameter(property = "spring-boot.run.useTestClasspath")
@Deprecated(since = "4.2.0", forRemoval = true)
private @Nullable Boolean useTestClasspath;
/**
* Strategy to determine how the test classpath should be included when running the
* application. Available values are {@code off} to not include the test classpath at
* all, {@code dependencies} to include only dependencies with test scope, and
* {@code all} to include the full test classpath (test classes and dependencies with
* test scope).
* @since 4.2.0
*/
@Parameter(property = "spring-boot.run.testClasspath", defaultValue = "off")
private String testClasspath = "off";
private @Nullable TestClasspath resolvedTestClasspath;
@Inject
public RunMojo(ToolchainManager toolchainManager) {
@@ -85,8 +102,17 @@ public class RunMojo extends AbstractRunMojo {
}
@Override
protected boolean isUseTestClasspath() {
return this.useTestClasspath;
protected TestClasspath testClasspath() {
if (this.resolvedTestClasspath == null) {
if (this.useTestClasspath != null) {
getLog().warn("useTestClasspath is deprecated, use testClasspath instead.");
this.resolvedTestClasspath = (this.useTestClasspath) ? TestClasspath.DEPENDENCIES : TestClasspath.OFF;
}
else {
this.resolvedTestClasspath = TestClasspath.valueOf(this.testClasspath.toUpperCase(Locale.ROOT));
}
}
return this.resolvedTestClasspath;
}
private static final class RunProcessKiller implements Runnable {
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
@@ -91,9 +92,24 @@ public class StartMojo extends AbstractRunMojo {
/**
* Flag to include the test classpath when running.
* @deprecated since 4.2.0 for removal in 4.4.0 in favor of {@code testClasspath}
*/
@Parameter(property = "spring-boot.run.useTestClasspath", defaultValue = "false")
private boolean useTestClasspath;
@Parameter(property = "spring-boot.run.useTestClasspath")
@Deprecated(since = "4.2.0", forRemoval = true)
private @Nullable Boolean useTestClasspath;
/**
* Strategy to determine how the test classpath should be included when running the
* application. Available values are {@code off} to not include the test classpath at
* all, {@code dependencies} to include only dependencies with test scope, and
* {@code all} to include the full test classpath (test classes and dependencies with
* test scope).
* @since 4.2.0
*/
@Parameter(property = "spring-boot.run.testClasspath", defaultValue = "off")
private String testClasspath = "off";
private @Nullable TestClasspath resolvedTestClasspath;
@Inject
public StartMojo(ToolchainManager toolchainManager) {
@@ -205,8 +221,17 @@ public class StartMojo extends AbstractRunMojo {
}
@Override
protected boolean isUseTestClasspath() {
return this.useTestClasspath;
protected TestClasspath testClasspath() {
if (this.resolvedTestClasspath == null) {
if (this.useTestClasspath != null) {
getLog().warn("useTestClasspath is deprecated, use testClasspath instead.");
this.resolvedTestClasspath = (this.useTestClasspath) ? TestClasspath.DEPENDENCIES : TestClasspath.OFF;
}
else {
this.resolvedTestClasspath = TestClasspath.valueOf(this.testClasspath.toUpperCase(Locale.ROOT));
}
}
return this.resolvedTestClasspath;
}
private class CreateJmxConnector implements Callable<@Nullable JMXConnector> {
@@ -17,7 +17,6 @@
package org.springframework.boot.maven;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -57,29 +56,14 @@ public class TestRunMojo extends AbstractRunMojo {
@Parameter(property = "spring-boot.test-run.optimizedLaunch", defaultValue = "true")
private boolean optimizedLaunch;
/**
* Directory containing the test classes and resource files that should be used to run
* the application.
*/
@Parameter(defaultValue = "${project.build.testOutputDirectory}", required = true)
@SuppressWarnings("NullAway.Init")
private File testClassesDirectory;
@Inject
public TestRunMojo(ToolchainManager toolchainManager) {
super(toolchainManager);
}
@Override
protected List<File> getClassesDirectories() {
ArrayList<File> classesDirectories = new ArrayList<>(super.getClassesDirectories());
classesDirectories.add(0, this.testClassesDirectory);
return classesDirectories;
}
@Override
protected boolean isUseTestClasspath() {
return true;
protected TestClasspath testClasspath() {
return TestClasspath.ALL;
}
@Override