Skip to content

Commit e3c2fb7

Browse files
Daniel Young and Mathew Johnsonsnicoll
authored andcommitted
Add useTestClassPath to run/start maven goals
Add `useTestClasspath` flag to add test dependencies to the classpath of the application. Works with both the `start` and `run` goals. Closes gh-2792
1 parent 8230d2c commit e3c2fb7

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>run-use-test-classpath</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
</properties>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>@project.groupId@</groupId>
15+
<artifactId>@project.artifactId@</artifactId>
16+
<version>@project.version@</version>
17+
<executions>
18+
<execution>
19+
<phase>package</phase>
20+
<goals>
21+
<goal>run</goal>
22+
</goals>
23+
<configuration>
24+
<useTestClasspath>true</useTestClasspath>
25+
</configuration>
26+
</execution>
27+
</executions>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-context</artifactId>
35+
<version>@spring.version@</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2015 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+
* http://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.test;
18+
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) {
22+
23+
Class<?> appContext = null;
24+
try {
25+
appContext = Class.forName("org.springframework.context.ApplicationContext");
26+
}
27+
catch (ClassNotFoundException e) {
28+
throw new IllegalStateException("Test dependencies not added to classpath", e);
29+
}
30+
System.out.println("I haz been run");
31+
}
32+
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def file = new File(basedir, "build.log")
2+
return file.text.contains("I haz been run")
3+

spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.maven.project.MavenProject;
3737
import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter;
3838
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
39+
3940
import org.springframework.boot.loader.tools.FileUtils;
4041
import org.springframework.boot.loader.tools.MainClassFinder;
4142

@@ -45,6 +46,7 @@
4546
* @author Phillip Webb
4647
* @author Stephane Nicoll
4748
* @author David Liu
49+
* @author Daniel Young
4850
* @see RunMojo
4951
* @see StartMojo
5052
*/
@@ -131,6 +133,13 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
131133
@Parameter(property = "fork")
132134
private Boolean fork;
133135

136+
/**
137+
* Flag to include the test classpath when running.
138+
* @since 1.3
139+
*/
140+
@Parameter(property = "useTestClasspath", defaultValue = "false")
141+
private Boolean useTestClasspath;
142+
134143
/**
135144
* Specify if the application process should be forked.
136145
* @return {@code true} if the application process should be forked
@@ -340,7 +349,7 @@ private void addProjectClasses(List<URL> urls) throws MalformedURLException {
340349

341350
private void addDependencies(List<URL> urls) throws MalformedURLException,
342351
MojoExecutionException {
343-
FilterArtifacts filters = getFilters(new TestArtifactFilter());
352+
FilterArtifacts filters = this.useTestClasspath ? getFilters() : getFilters(new TestArtifactFilter());
344353
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), filters);
345354
for (Artifact artifact : artifacts) {
346355
if (artifact.getFile() != null) {

spring-boot-tools/spring-boot-maven-plugin/src/site/apt/usage.apt.vm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ mvn spring-boot:run
151151
in such a way that any dependency that is excluded in the plugin's configuration gets excluded
152152
from the classpath as well. See {{{./examples/exclude-dependency.html}Exclude a dependency}} for
153153
more details.
154+
155+
Sometimes it is useful to include test dependencies when running the application. For example,
156+
if you want to run your application in a test mode that uses stub classes. If you wish to do this,
157+
you can set the <<<useTestClasspath>>> parameter to true. Note that this is only applied when you
158+
run an application: the <<<repackage>>> goal will not add test dependencies to the resulting JAR/WAR.
154159

155160
* Working with integration tests
156161

0 commit comments

Comments
 (0)