Once you have installed the CLI you can run it by typing spring
. If you run spring
without any arguments, a simple help screen is displayed:
$ spring
usage: spring [--help] [--version]
<command> [<args>]
Available commands are:
run [options] <files> [--] [args]
Run a spring groovy script
... more command help is shown here
You can use help
to get more details about any of the supported commands. For example:
$ spring help run spring run - Run a spring groovy script usage: spring run [options] <files> [--] [args] Option Description ------ ----------- --autoconfigure [Boolean] Add autoconfigure compiler transformations (default: true) --classpath, -cp Additional classpath entries -e, --edit Open the file with the default system editor --no-guess-dependencies Do not attempt to guess dependencies --no-guess-imports Do not attempt to guess imports -q, --quiet Quiet logging -v, --verbose Verbose logging of dependency resolution --watch Watch the specified file for changes
The version
command provides a quick way to check which version of Spring Boot you are
using.
$ spring version Spring CLI v1.0.0.RC5
You can compile and run Groovy source code using the run
command. The Spring Boot CLI
is completely self contained so you don’t need any external Groovy installation.
Here is an example “hello world” web application written in Groovy:
@Controller class WebApplication { @RequestMapping("/") @ResponseBody String home() { return "Hello World!" } }
Standard Groovy includes a @Grab
annotation which allows you to declare dependencies
on a third-party libraries. This useful technique allows Groovy to download jars in the
same way as Maven or Gradle would; but without requiring you to use a build tool.
Spring Boot extends this technique further, and will attempt to deduce which libraries
to “grab” based on your code. For example, since the WebApplication
code above uses
@Controller
annotations, “Tomcat” and “Spring MVC” will be grabbed.
The following items are used as “grab hints”:
Items | Grabs |
---|---|
| JDBC Application. |
| JMS Application. |
| JUnit. |
| RabbitMQ. |
| Project Reactor. |
extends | Spock test. |
| Spring Batch. |
| Spring Integration. |
| Spring Mobile. |
| Spring MVC + Embedded Tomcat. |
| Spring Security. |
| Spring Transaction Management. |
Tip | |
---|---|
See subclasses of
|
To help reduce the size of your Groovy code, several import
statements are
automatically included. Notice how the example above refers to @Component
,
@Controller
, @RequestMapping
and @ResponseBody
without needing to use
fully-qualified names or import
statements.
Tip | |
---|---|
Many Spring annotations will work without using |
The test
command allows you to compile and run tests for your application. Typical
usage looks like this:
$ spring test app.groovy tests.groovy Total: 1, Success: 1, : Failures: 0 Passed? true
In this example, tests.groovy
contains JUnit @Test
methods or Spock Specification
classes. All the common framework annotations and static methods should be available to
you without having to import
them.
Here is the test.groovy
file that we used above:
class ApplicationTests { @Test void homeSaysHello() { assertEquals("Hello World", new WebApplication().home()) } }
Tip | |
---|---|
If you have more than one test source files, you might prefer to organize them
into a |
You can use “shell globbing” with all commands that accept file input. This allows you to easily use multiple files from a single directory, e.g.
$ spring run *.groovy
This technique can also be useful if you want to segregate your “test” or “spec” code from the main application code:
$ spring test app/*.groovy test/*.groovy
You can use the jar
command to package your application into a self-contained
executable jar file. For example:
$ spring jar my-app.jar *.groovy
The resulting jar will contain the classes produced by compiling the application and all
of the application’s dependencies so that it can then be run using java -jar
. The jar
file will also contain entries from the application’s classpath.
See the output of spring help jar
for more information.
Spring Boot includes command-line completion scripts for BASH and zsh shells. If you
don’t use either of these shells (perhaps you are a Windows user) then you can use the
shell
command to launch an integrated shell.
$ spring shell
Spring Boot (v1.0.0.RC5)
Hit TAB to complete. Type 'help' and hit RETURN for help, and 'exit' to quit.
From inside the embedded shell you can run other commands directly:
$ version Spring CLI v1.0.0.RC5
The embedded shell supports ANSI color output as well as tab
completion. If you need
to run a native command you can use the $
prefix. Hitting ctrl-c
will exit the
embedded shell.