Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
2b44ad98
Commit
2b44ad98
authored
Mar 27, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rework BootRunTask to be more idiomatic and make fewer assumptions
parent
6e7e4245
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
197 additions
and
3 deletions
+197
-3
BootRun.java
...ain/java/org/springframework/boot/gradle/run/BootRun.java
+54
-0
RunPluginFeatures.java
...rg/springframework/boot/gradle/run/RunPluginFeatures.java
+5
-3
BootRunApplication.java
...-plugin/src/test/java/com/example/BootRunApplication.java
+42
-0
BootRunIntegrationTests.java
...ingframework/boot/gradle/run/BootRunIntegrationTests.java
+74
-0
GradleBuild.java
.../org/springframework/boot/gradle/testkit/GradleBuild.java
+2
-0
BootRunIntegrationTests-basicExecution.gradle
.../gradle/run/BootRunIntegrationTests-basicExecution.gradle
+8
-0
BootRunIntegrationTests-sourceResourcesCanBeUsed.gradle
...n/BootRunIntegrationTests-sourceResourcesCanBeUsed.gradle
+12
-0
No files found.
spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/run/BootRun
Task
.java
→
spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/run/BootRun.java
View file @
2b44ad98
...
@@ -16,41 +16,30 @@
...
@@ -16,41 +16,30 @@
package
org
.
springframework
.
boot
.
gradle
.
run
;
package
org
.
springframework
.
boot
.
gradle
.
run
;
import
java.io.File
;
import
org.gradle.api.file.SourceDirectorySet
;
import
java.util.ArrayList
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Set
;
import
org.gradle.api.internal.file.collections.SimpleFileCollection
;
import
org.gradle.api.tasks.JavaExec
;
import
org.gradle.api.tasks.JavaExec
;
import
org.gradle.api.tasks.SourceSet
;
import
org.gradle.api.tasks.SourceSet
;
import
org.gradle.api.tasks.SourceSetOutput
;
import
org.springframework.boot.loader.tools.FileUtils
;
/**
/**
*
Extension of the standard 'run' task with additional Spring Boot features
.
*
Custom {@link JavaExec} task for running a Spring Boot application
.
*
*
* @author Dave Syer
* @author Phillip Webb
* @author Andy Wilkinson
* @author Andy Wilkinson
*/
*/
public
class
BootRun
Task
extends
JavaExec
{
public
class
BootRun
extends
JavaExec
{
/**
/**
* Whether or not resources (typically in {@code src/main/resources} are added
* Adds the {@link SourceDirectorySet#getSrcDirs() source directories} of the given
* directly to the classpath. When enabled, this allows live in-place editing of
* {@code sourceSet's} {@link SourceSet#getResources() resources} to the start of the
* resources. Duplicate resources are removed from the resource output directory to
* classpath in place of the {@link SourceSet#getOutput output's}
* prevent them from appearing twice if {@code ClassLoader.getResources()} is called.
* {@link SourceSetOutput#getResourcesDir() resources directory}.
*
* @param sourceSet the source set
*/
*/
private
boolean
addResources
=
false
;
public
void
sourceResources
(
SourceSet
sourceSet
)
{
setClasspath
(
getProject
()
public
boolean
getAddResources
()
{
.
files
(
sourceSet
.
getResources
().
getSrcDirs
(),
getClasspath
())
return
this
.
addResources
;
.
filter
((
file
)
->
!
file
.
equals
(
sourceSet
.
getOutput
().
getResourcesDir
())));
}
public
void
setAddResources
(
boolean
addResources
)
{
this
.
addResources
=
addResources
;
}
}
@Override
@Override
...
@@ -59,29 +48,7 @@ public class BootRunTask extends JavaExec {
...
@@ -59,29 +48,7 @@ public class BootRunTask extends JavaExec {
// Record that the console is available here for AnsiOutput to detect later
// Record that the console is available here for AnsiOutput to detect later
this
.
getEnvironment
().
put
(
"spring.output.ansi.console-available"
,
true
);
this
.
getEnvironment
().
put
(
"spring.output.ansi.console-available"
,
true
);
}
}
addResourcesIfNecessary
();
super
.
exec
();
super
.
exec
();
}
}
private
void
addResourcesIfNecessary
()
{
if
(
this
.
addResources
)
{
SourceSet
mainSourceSet
=
SourceSets
.
findMainSourceSet
(
getProject
());
final
File
outputDir
=
(
mainSourceSet
==
null
?
null
:
mainSourceSet
.
getOutput
().
getResourcesDir
());
final
Set
<
File
>
resources
=
new
LinkedHashSet
<>();
if
(
mainSourceSet
!=
null
)
{
resources
.
addAll
(
mainSourceSet
.
getResources
().
getSrcDirs
());
}
List
<
File
>
classPath
=
new
ArrayList
<>(
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
);
}
}
}
}
}
}
spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/run/RunPluginFeatures.java
View file @
2b44ad98
...
@@ -22,6 +22,7 @@ import java.util.concurrent.Callable;
...
@@ -22,6 +22,7 @@ import java.util.concurrent.Callable;
import
org.gradle.api.Project
;
import
org.gradle.api.Project
;
import
org.gradle.api.plugins.JavaPlugin
;
import
org.gradle.api.plugins.JavaPlugin
;
import
org.gradle.api.plugins.JavaPluginConvention
;
import
org.gradle.api.plugins.JavaPluginConvention
;
import
org.gradle.api.tasks.SourceSet
;
import
org.springframework.boot.gradle.MainClassResolver
;
import
org.springframework.boot.gradle.MainClassResolver
;
import
org.springframework.boot.gradle.PluginFeatures
;
import
org.springframework.boot.gradle.PluginFeatures
;
...
@@ -30,6 +31,7 @@ import org.springframework.boot.gradle.PluginFeatures;
...
@@ -30,6 +31,7 @@ import org.springframework.boot.gradle.PluginFeatures;
* {@link PluginFeatures} to add run support.
* {@link PluginFeatures} to add run support.
*
*
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
*/
*/
public
class
RunPluginFeatures
implements
PluginFeatures
{
public
class
RunPluginFeatures
implements
PluginFeatures
{
...
@@ -46,12 +48,12 @@ public class RunPluginFeatures implements PluginFeatures {
...
@@ -46,12 +48,12 @@ public class RunPluginFeatures implements PluginFeatures {
final
JavaPluginConvention
javaConvention
=
project
.
getConvention
()
final
JavaPluginConvention
javaConvention
=
project
.
getConvention
()
.
getPlugin
(
JavaPluginConvention
.
class
);
.
getPlugin
(
JavaPluginConvention
.
class
);
BootRun
Task
run
=
project
.
getTasks
().
create
(
RUN_APP_TASK_NAME
,
BootRunTask
.
class
);
BootRun
run
=
project
.
getTasks
().
create
(
RUN_APP_TASK_NAME
,
BootRun
.
class
);
run
.
setDescription
(
"Run the project with support for "
run
.
setDescription
(
"Run the project with support for "
+
"auto-detecting main class and reloading static resources"
);
+
"auto-detecting main class and reloading static resources"
);
run
.
setGroup
(
"application"
);
run
.
setGroup
(
"application"
);
run
.
setClasspath
(
run
.
classpath
(
javaConvention
.
getSourceSets
()
javaConvention
.
getSourceSets
().
findByName
(
"main"
).
getRuntimeClasspath
());
.
findByName
(
SourceSet
.
MAIN_SOURCE_SET_NAME
).
getRuntimeClasspath
());
run
.
getConventionMapping
().
map
(
"jvmArgs"
,
new
Callable
<
Object
>()
{
run
.
getConventionMapping
().
map
(
"jvmArgs"
,
new
Callable
<
Object
>()
{
@Override
@Override
public
Object
call
()
throws
Exception
{
public
Object
call
()
throws
Exception
{
...
...
spring-boot-tools/spring-boot-gradle-plugin/src/test/java/com/example/BootRunApplication.java
0 → 100644
View file @
2b44ad98
/*
* Copyright 2012-2017 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
com
.
example
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
/**
* Very basic application used for testing {@code BootRun}.
*
* @author Andy Wilkinson
*/
public
class
BootRunApplication
{
protected
BootRunApplication
()
{
}
public
static
void
main
(
String
[]
args
)
{
int
i
=
1
;
for
(
URL
url
:
((
URLClassLoader
)
BootRunApplication
.
class
.
getClassLoader
())
.
getURLs
())
{
System
.
out
.
println
(
i
++
+
". "
+
url
);
}
}
}
spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/run/BootRunIntegrationTests.java
0 → 100644
View file @
2b44ad98
/*
* Copyright 2012-2017 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
.
run
;
import
java.io.File
;
import
java.io.IOException
;
import
org.gradle.testkit.runner.BuildResult
;
import
org.gradle.testkit.runner.TaskOutcome
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.springframework.boot.gradle.testkit.GradleBuild
;
import
org.springframework.util.FileSystemUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Integration tests for {@link BootRun}.
*
* @author Andy Wilkinson
*/
public
class
BootRunIntegrationTests
{
@Rule
public
final
GradleBuild
gradleBuild
=
new
GradleBuild
();
@Test
public
void
basicExecution
()
throws
IOException
{
File
output
=
new
File
(
this
.
gradleBuild
.
getProjectDir
(),
"src/main/java/com/example"
);
output
.
mkdirs
();
FileSystemUtils
.
copyRecursively
(
new
File
(
"src/test/java/com/example"
),
output
);
new
File
(
this
.
gradleBuild
.
getProjectDir
(),
"src/main/resources"
).
mkdirs
();
BuildResult
result
=
this
.
gradleBuild
.
build
(
"bootRun"
);
assertThat
(
result
.
task
(
":bootRun"
).
getOutcome
()).
isEqualTo
(
TaskOutcome
.
SUCCESS
);
assertThat
(
result
.
getOutput
()).
contains
(
"1. "
+
urlOf
(
"build/classes/main"
));
assertThat
(
result
.
getOutput
()).
contains
(
"2. "
+
urlOf
(
"build/resources/main"
));
assertThat
(
result
.
getOutput
()).
doesNotContain
(
urlOf
(
"src/main/resources"
));
}
@Test
public
void
sourceResourcesCanBeUsed
()
throws
IOException
{
File
output
=
new
File
(
this
.
gradleBuild
.
getProjectDir
(),
"src/main/java/com/example"
);
output
.
mkdirs
();
FileSystemUtils
.
copyRecursively
(
new
File
(
"src/test/java/com/example"
),
output
);
BuildResult
result
=
this
.
gradleBuild
.
build
(
"bootRun"
);
assertThat
(
result
.
task
(
":bootRun"
).
getOutcome
()).
isEqualTo
(
TaskOutcome
.
SUCCESS
);
assertThat
(
result
.
getOutput
()).
contains
(
"1. "
+
urlOf
(
"src/main/resources"
));
assertThat
(
result
.
getOutput
()).
contains
(
"2. "
+
urlOf
(
"build/classes/main"
));
assertThat
(
result
.
getOutput
()).
doesNotContain
(
urlOf
(
"build/resources/main"
));
}
private
String
urlOf
(
String
path
)
throws
IOException
{
return
new
File
(
this
.
gradleBuild
.
getProjectDir
().
getCanonicalFile
(),
path
).
toURI
()
.
toURL
().
toString
();
}
}
spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java
View file @
2b44ad98
...
@@ -39,6 +39,7 @@ import org.junit.runner.Description;
...
@@ -39,6 +39,7 @@ import org.junit.runner.Description;
import
org.junit.runners.model.Statement
;
import
org.junit.runners.model.Statement
;
import
org.xml.sax.InputSource
;
import
org.xml.sax.InputSource
;
import
org.springframework.asm.ClassVisitor
;
import
org.springframework.boot.loader.tools.LaunchScript
;
import
org.springframework.boot.loader.tools.LaunchScript
;
/**
/**
...
@@ -106,6 +107,7 @@ public class GradleBuild implements TestRule {
...
@@ -106,6 +107,7 @@ public class GradleBuild implements TestRule {
return
absolutePath
(
"bin"
)
+
","
+
absolutePath
(
"build/classes/main"
)
+
","
return
absolutePath
(
"bin"
)
+
","
+
absolutePath
(
"build/classes/main"
)
+
","
+
absolutePath
(
"build/resources/main"
)
+
","
+
absolutePath
(
"build/resources/main"
)
+
","
+
pathOfJarContaining
(
LaunchScript
.
class
)
+
","
+
pathOfJarContaining
(
LaunchScript
.
class
)
+
","
+
pathOfJarContaining
(
ClassVisitor
.
class
)
+
","
+
pathOfJarContaining
(
DependencyManagementPlugin
.
class
);
+
pathOfJarContaining
(
DependencyManagementPlugin
.
class
);
}
}
...
...
spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/run/BootRunIntegrationTests-basicExecution.gradle
0 → 100644
View file @
2b44ad98
buildscript
{
dependencies
{
classpath
files
(
pluginClasspath
.
split
(
','
))
}
}
apply
plugin:
'java'
apply
plugin:
'org.springframework.boot'
spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/run/BootRunIntegrationTests-sourceResourcesCanBeUsed.gradle
0 → 100644
View file @
2b44ad98
buildscript
{
dependencies
{
classpath
files
(
pluginClasspath
.
split
(
','
))
}
}
apply
plugin:
'java'
apply
plugin:
'org.springframework.boot'
bootRun
{
sourceResources
sourceSets
.
main
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment