Browse Source

Add support to the Gradle plugin for disabling direct use of resources

The Maven plugin allows spring-boot:run to be configured so that
resources are loaded from their output location rather than from
src/main/resources. This commit adds an equivalent configuration
option to the Gradle plugin. To disable source resources from being
added to the classpath in place of those in the output location
the configure the bootRun tasks like this:

bootRun {
	addResources = false
}

Closes gh-2431
pull/2459/head
Andy Wilkinson 11 years ago
parent
commit
a88f27168a
  1. 20
      spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc
  2. 5
      spring-boot-integration-tests/pom.xml
  3. 69
      spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/BootRunResourceTests.java
  4. 15
      spring-boot-integration-tests/src/test/resources/boot-run-resources/build.gradle
  5. 24
      spring-boot-integration-tests/src/test/resources/boot-run-resources/src/main/java/BootRunResourcesApplication.java
  6. 0
      spring-boot-integration-tests/src/test/resources/boot-run-resources/src/main/resources/test.txt
  7. 54
      spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java

20
spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc

@ -376,12 +376,22 @@ To run a project in place without building a jar first you can use the "`bootRun @@ -376,12 +376,22 @@ To run a project in place without building a jar first you can use the "`bootRun
$ gradle bootRun
----
Running this way makes your static classpath resources (i.e. in `src/main/resources` by
default) reloadable in the live application, which can be helpful at development time.
By default, running this way makes your static classpath resources (i.e. in
`src/main/resources` by default) reloadable in the live application, which can be helpful
at development time. Making static classpath resources reloadable means that `bootRun`
does not use the output of the `processResources` task, i.e., when invoked using
`bootRun`, your application will use the resources in their unprocessed form.
NOTE: Making static classpath resources reloadable means that `bootRun` does not use the
output of the `processResources` task. When invoked using `bootRun` your application will
use the resources in their unprocessed form.
You can disable the direct use of your static classpath resources. This will mean that
the resources are no longer reloadable but the output of the `processResources` task will
be used. To do so, set `addResources` on the `bootRun` task to `false`:
[source,groovy,indent=0,subs="verbatim,attributes"]
----
bootRun {
addResources = false
}
----

5
spring-boot-integration-tests/pom.xml

@ -26,6 +26,11 @@ @@ -26,6 +26,11 @@
<version>${gradle.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependency-tools</artifactId>

69
spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/BootRunResourceTests.java

@ -0,0 +1,69 @@ @@ -0,0 +1,69 @@
/*
* 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.springframework.boot.gradle;
import java.io.IOException;
import org.gradle.tooling.ProjectConnection;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.dependency.tools.ManagedDependencies;
import org.springframework.boot.test.OutputCapture;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
/**
* Integration tests for bootRun's resource handling
*
* @author Andy Wilkinson
*/
public class BootRunResourceTests {
private static final String BOOT_VERSION = ManagedDependencies.get()
.find("spring-boot").getVersion();
private static ProjectConnection project;
@Rule
public OutputCapture output = new OutputCapture();
@BeforeClass
public static void createProject() throws IOException {
project = new ProjectCreator().createProject("boot-run-resources");
}
@Test
public void resourcesDirectlyFromSource() {
project.newBuild().forTasks("clean", "bootRun")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PaddResources=true")
.run();
assertThat(this.output.toString(), containsString("src/main/resources/test.txt"));
}
@Test
public void resourcesFromBuildOutput() {
project.newBuild().forTasks("clean", "bootRun")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PaddResources=false")
.run();
assertThat(this.output.toString(),
containsString("build/resources/main/test.txt"));
}
}

15
spring-boot-integration-tests/src/test/resources/boot-run-resources/build.gradle

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'spring-boot'
bootRun {
addResources = Boolean.valueOf(project.addResources)
}

24
spring-boot-integration-tests/src/test/resources/boot-run-resources/src/main/java/BootRunResourcesApplication.java

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
/*
* 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.
*/
public class BootRunResourcesApplication {
public static void main(String[] args) {
ClassLoader classLoader = BootRunResourcesApplication.class.getClassLoader();
System.out.println(classLoader.getResource("test.txt"));
}
}

0
spring-boot-integration-tests/src/test/resources/boot-run-resources/src/main/resources/test.txt

54
spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java

@ -35,25 +35,49 @@ import org.springframework.boot.loader.tools.FileUtils; @@ -35,25 +35,49 @@ import org.springframework.boot.loader.tools.FileUtils;
*/
public class BootRunTask extends JavaExec {
/**
* Whether or not resources (typically in {@code src/main/resources} are added
* directly to the classpath. When enabled (the default), this allows live in-place
* editing of resources. Duplicate resources are removed from the resource output
* directory to prevent them from appearing twice if
* {@code ClassLoader.getResources()} is called.
*/
private boolean addResources = true;
public boolean getAddResources() {
return this.addResources;
}
public void setAddResources(boolean addResources) {
this.addResources = addResources;
}
@Override
public void exec() {
SourceSet mainSourceSet = SourceSets.findMainSourceSet(getProject());
final File outputDir = (mainSourceSet == null ? null : mainSourceSet.getOutput()
.getResourcesDir());
final Set<File> resources = new LinkedHashSet<File>();
if (mainSourceSet != null) {
resources.addAll(mainSourceSet.getResources().getSrcDirs());
}
List<File> classPath = new ArrayList<File>(getClasspath().getFiles());
classPath.addAll(0, resources);
getLogger().info("Adding classpath: " + resources);
setClasspath(new SimpleFileCollection(classPath));
if (outputDir != null) {
for (File directory : resources) {
FileUtils.removeDuplicatesFromOutputDirectory(outputDir, directory);
addResourcesIfNecessary();
super.exec();
}
private void addResourcesIfNecessary() {
System.out.println(this.addResources);
if (this.addResources) {
SourceSet mainSourceSet = SourceSets.findMainSourceSet(getProject());
final File outputDir = (mainSourceSet == null ? null : mainSourceSet
.getOutput().getResourcesDir());
final Set<File> resources = new LinkedHashSet<File>();
if (mainSourceSet != null) {
resources.addAll(mainSourceSet.getResources().getSrcDirs());
}
List<File> classPath = new ArrayList<File>(getClasspath().getFiles());
classPath.addAll(0, resources);
getLogger().info("Adding classpath: " + resources);
setClasspath(new SimpleFileCollection(classPath));
if (outputDir != null) {
for (File directory : resources) {
FileUtils.removeDuplicatesFromOutputDirectory(outputDir, directory);
}
}
}
super.exec();
}
}

Loading…
Cancel
Save