SpringBootCLI command line tool

Posted by mark110384 on Tue, 05 Nov 2019 02:42:57 +0100

Spring Boot CLI is a command-line tool for rapid development of spring applications. Used to run Groovy (similar to Java style) scripts.

Spring cli does not seem to be a tool for command line parameters and behaviors of various DIY spring boot programs. Instead, it executes groovy scripts to quickly achieve some results.

Edition

SpringBoot-2.2.0.RELEASE

install

from Official website Download the spring cli installation, and then configure the bin directory to the environment variable.
After installation, you can view the version through spring version.

Use

Running applications with CLI

Create a new file (such as hello.groovy) with the following contents:

@RestController
class ThisWillActuallyRun{
  @RequestMapping("/")
  String home(){
    "Hello World!"
  }
}

Run command: spring run hello.groovy. It is slow to download various dependencies for the first time, and then it will start directly.
This example uses the default port to start a web service.
By visiting http://127.0.0.1:8080/ Get the return data.

You can also run the command: spring run hello. Groovy ----- server. Port = 9000 to specify command line parameters. Note: use -- to separate commands and parameters.

To set the JVM command line parameters, you can use Java? Opts environment variables as follows:

# linux
JAVA_OPTS=-Xmx1024m spring run hello.groovy
# windows
set "JAVA_OPTS=-Xms256m -Xmx2048m"

Inferring dependence

For example, use Spring MVC + Embedded Tomcat according to @ Controller or @ RestController or @ EnableWebMvc

Default package import statement

For brevity, many package import statements occur automatically.

Automatic derivation of main method

Unlike java applications, which need to include main methods, Groovy scripts will be created automatically by SpringApplication, which will execute your compiled code as a resource.

Custom dependency management

The default dependency management declaration in spring boot dependencies can be configured through @ DependencyManagementBom. The annotation value needs to satisfy the expression: groupId:artifactId:version

@DependencyManagementBom("io.spring.platform:platform-bom:1.1.2.RELEASE")

@DependencyManagementBom(["com.example:custom-bom:1.0.0","com.example:another-bom:1.0.0"])

Run multiple resource files

spring run *.groovy

Package application

spring java my-app.jar *.groovy

The generated jar contains the classes generated by the compiled application and all the dependencies of the application so that it can be run using java jar. The jar file also contains entries from the application classpath. You can use -- include and -- exclude to add and remove explicit paths packaged to jars.
The resources included by default are as follows:

public/**, resources/**, static/**, templates/**, META-INF/**, *

The default exclusions are as follows:

.*, repository/**, build/**, target/**, **/*.jar, **/*.groovy

Initialize new project

The init command allows you to use the start.spring.io Create a new project, as shown in the following example:

# Project name is my project
$ spring init --dependencies=web,data-jpa my-project
Using service at https://start.spring.io
Project extracted to '/Users/developer/example/my-project

The above example creates a Maven project of my project, using spring boot starter web and spring boot starter data JPA. You can use the -- List flag to list the capabilities of the service. As follows:

$ spring init --list
=======================================
Capabilities of https://start.spring.io
=======================================

Available dependencies:
-----------------------
actuator - Actuator: Production ready features to help you monitor and manage your application
...
web - Web: Support for full-stack web development, including Tomcat and spring-webmvc
websocket - Websocket: Support for WebSocket development
ws - WS: Support for Spring Web Services

Available project types:
------------------------
gradle-build -  Gradle Config [format:build, build:gradle]
gradle-project -  Gradle Project [format:project, build:gradle]
maven-build -  Maven POM [format:build, build:maven]
maven-project -  Maven Project [format:project, build:maven] (default)

The init command supports many operations, which can be viewed through the help command. The following example is to create a Gradle project using Java 8, packaged as war

$ spring init --build=gradle --java-version=1.8 --dependencies=websocket --packaging=war sample-app.zip
Using service at https://start.spring.io
Content saved to 'sample-app.zip'

Using a cut in Shell

SpringBoot contains the command line full script for BASH and zsh shells. If you don't need to use them (using windows system), you can start the integrated shell with the shell command, as follows:

$ spring shell
Spring Boot (v2.2.0.RELEASE)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.

With the embedded shell, you can use other commands directly:

$ version
Spring CLI v2.2.0.RELEASE

The embedded shell supports ANSI color output and tab prompt. If you need to run native commands, you can use the! Prefix to exit the embedded shell and use ctrl-c.

Add extension

Use the install command to add extensions. The accepted extension format is: group:artifact:version, as follows:

$ spring install com.example:spring-boot-cli-extension:1.0.0.RELEASE

In addition to installing the target dependencies you need, all of its dependencies are also installed.

Use the uninstall command to uninstall dependencies. Use it the same way as install.
It unloads the target dependency and all dependencies of the target.
Uninstall all extension dependencies using:

$ spring uninstall --all

Using Groovy Beans DSL to develop applications

spring framework 4.0 already supports DSL natively. bean definitions can be used in the same format in Grooby applications.

Configuring CLI with settings.xml

Spring Boot CLI uses Aether (Maven's dependency resolution engine) to resolve dependencies. The CLI uses the Maven configuration in ~ /. m2/settings.xml to configure Aether. The CLI follows the following configuration:

  • Offline
  • Mirrors
  • Servers
  • Proxies
  • Profiles
    • Activation
    • Repositories
  • Active profiles

Reference document

Official document of SpringBoot
Public number: Yifei Xi (focus on in-depth study of Java domain knowledge, from source code to principle, systematic and orderly learning).

Topics: Java Spring Maven Gradle