Spring Boot CLI

Spring Boot CLI

The Spring Boot CLI is a command line tool that you can use if you want to quickly develop a Spring
application. It lets you run Groovy scripts, which means that you have a familiar Java-like syntax
without so much boilerplate code. You can also bootstrap a new project or write your own
command for it.

Installing the CLI


The Spring Boot CLI (Command-Line Interface) can be installed manually by using SDKMAN! (the
SDK Manager) or by using Homebrew or MacPorts if you are an OSX user. See Installing the Spring Boot CLI in the “Getting started” section for comprehensive installation instructions.

Using the CLI

Once you have installed the CLI, you can run it by typing spring and pressing Enter at the command
line. If you run spring without any arguments, a simple help screen is displayed, as follows:

$ spring
usage: spring [--help] [--version]
<command> [<args>]
Available commands are:
run [options] <files> [--] [args]
Run a spring groovy script

You can type spring help to get more details about any of the supported commands, as shown in the
following 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
--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, as
follows:
$ spring version
Spring CLI v2.3.0.M1

Running Applications with the CLI

You can compile and run Groovy source code by using the run command. The Spring Boot CLI is
completely self-contained, so you do not need any external Groovy installation.
The following example shows a “hello world” web application written in Groovy:

hello.groovy
@RestController
class WebApplication {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
To compile and run the application, type the following command:

$ spring run hello.groovy

To pass command-line arguments to the application, use -- to separate the commands from the
“spring” command arguments, as shown in the following example:
$ spring run hello.groovy -- --server.port=9000

To set JVM command line arguments, you can use the JAVA_OPTS environment variable, as shown in the following example:
$ JAVA_OPTS=-Xmx1024m spring run hello.groovy

When setting JAVA_OPTS on Microsoft Windows, make sure to quote the entire
instruction, such as set "JAVA_OPTS=-Xms256m -Xmx2048m". Doing so ensures the
values are properly passed to the process.

Deduced “grab” Dependencies

Standard Groovy includes a @Grab annotation, which lets you declare dependencies on third-party
libraries. This useful technique lets Groovy 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 tries to deduce which libraries to “grab” based on
your code. For example, since the WebApplication code shown previously uses @RestController
annotations, Spring Boot grabs "Tomcat" and "Spring MVC".


The following items are used as “grab hints”:

Items                                                                                 Grabs
JdbcTemplate, NamedParameterJdbcTemplate,               JDBC Application.
DataSource                                                                       
@EnableJms                                                                     JMS Application.
@EnableCaching                                                              Caching abstraction.
@Test                                                                                JUnit.
@EnableRabbit                                                                 RabbitMQ.
extends Specification                                                        Spock test.
@EnableBatchProcessing                                                Spring Batch.
@MessageEndpoint @EnableIntegration                        Spring Integration.
@Controller @RestController @EnableWebMvc           Spring MVC + Embedded Tomcat.
@EnableWebSecurity                                                      Spring Security.
@EnableTransactionManagement                                   Spring Transaction Management.

Deduced “grab” Coordinates

Spring Boot extends Groovy’s standard @Grab support by letting you specify a dependency without a group or version (for example, @Grab('freemarker')). Doing so consults Spring Boot’s default
dependency metadata to deduce the artifact’s group and version.

The default metadata is tied to the version of the CLI that you use. It changes only
when you move to a new version of the CLI, putting you in control of when the
versions of your dependencies may change. A table showing the dependencies and
their versions that are included in the default metadata can be found in the
appendix.

Default Import Statements

To help reduce the size of your Groovy code, several import statements are automatically included.
Notice how the preceding example refers to @Component, @RestController, and @RequestMapping
without needing to use fully-qualified names or import statements.

Many Spring annotations work without using import statements. Try running your
application to see what fails before adding imports.

Automatic Main Method

Unlike the equivalent Java application, you do not need to include a public static void
main(String[] args) method with your Groovy scripts. A SpringApplication is automatically created,
with your compiled code acting as the source.

Custom Dependency Management

By default, the CLI uses the dependency management declared in spring-boot-dependencies when
resolving @Grab dependencies. Additional dependency management, which overrides the default
dependency management, can be configured by using the @DependencyManagementBom annotation. The
annotation’s value should specify the coordinates (groupId:artifactId:version) of one or more
Maven BOMs.

For example, consider the following declaration:
@DependencyManagementBom("com.example.custom-bom:1.0.0")
The preceding declaration picks up custom-bom-1.0.0.pom in a Maven repository under
com/example/custom-versions/1.0.0/.

When you specify multiple BOMs, they are applied in the order in which you declare them, as
shown in the following example:
@DependencyManagementBom(["com.example.custom-bom:1.0.0",
"com.example.another-bom:1.0.0"])
The preceding example indicates that the dependency management in another-bom overrides the
dependency management in custom-bom.
You can use @DependencyManagementBom anywhere that you can use @Grab. However, to ensure
consistent ordering of the dependency management, you can use @DependencyManagementBom at most once in your application.





Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations