diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/pom.xml b/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/pom.xml new file mode 100644 index 000000000000..cd46c4e87e06 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + org.springframework.boot.maven.it + run-use-test-classpath + 0.0.1.BUILD-SNAPSHOT + + UTF-8 + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + package + + run + + + true + + + + + + + + + org.springframework + spring-context + @spring.version@ + test + + + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/src/main/java/org/test/SampleApplication.java b/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/src/main/java/org/test/SampleApplication.java new file mode 100644 index 000000000000..22807d20922c --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/src/main/java/org/test/SampleApplication.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2015 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 + * + * 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 org.test; + +import java.lang.Class; +import java.lang.ClassNotFoundException; +import java.lang.Exception; +import java.lang.IllegalStateException; + +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"); + } + +} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/verify.groovy new file mode 100644 index 000000000000..841c4a97de58 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/run-use-test-classpath/verify.groovy @@ -0,0 +1,3 @@ +def file = new File(basedir, "build.log") +return file.text.contains("I haz been run") + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java index 684b962894f1..b38397b78b4c 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java @@ -130,6 +130,13 @@ public class RunMojo extends AbstractDependencyFilterMojo { @Parameter(defaultValue = "${project.build.outputDirectory}", required = true) private File classesDirectory; + /** + * Flag to include the test classpath when running. + * @since 1.3 + */ + @Parameter(property = "useTestClasspath", defaultValue = "false") + private Boolean useTestClasspath; + /** * Flag to indicate if the run processes should be forked. By default process forking * is only used if an agent or jvmArguments are specified. @@ -312,7 +319,7 @@ private void addProjectClasses(List urls) throws MalformedURLException { private void addDependencies(List urls) throws MalformedURLException, MojoExecutionException { - FilterArtifacts filters = getFilters(new TestArtifactFilter()); + FilterArtifacts filters = this.useTestClasspath ? getFilters() : getFilters(new TestArtifactFilter()); Set artifacts = filterDependencies(this.project.getArtifacts(), filters); for (Artifact artifact : artifacts) { if (artifact.getFile() != null) { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/usage.apt.vm b/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/usage.apt.vm index f4e983e67ecc..6dccae2621eb 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/usage.apt.vm +++ b/spring-boot-tools/spring-boot-maven-plugin/src/site/apt/usage.apt.vm @@ -140,3 +140,8 @@ mvn spring-boot:run in such a way that any dependency that is excluded in the plugin's configuration gets excluded from the classpath as well. See {{{./examples/exclude-dependency.html}Exclude a dependency}} for more details. + + Sometimes it is useful to include test dependencies when running the application. For example, + if you want to run your application in a test mode that uses stub classes. If you wish to do this, + you can set the <<>> parameter to true. This only applies to the run goal. The + repackage goal will never add test dependencies to the resulting JAR/WAR.