SpringBootCLI command-line tool

Command-line tool

Spring Boot CLI is used for rapid development of Spring applications command-line tool. For running Groovy(similar to Java style) scripts.

spring-cli does not seem to be a variety of diy spring-boot command line tool parameters and behavior, but groovy script execution to quickly achieve some results.

version

SpringBoot-2.2.0.RELEASE

installation

From the official website to download spring-clito install, and then bin directory configuration environment variable.
The installation is complete by spring versionviewing version.

use

CLI run the application with

Create a file (eg: hello.groovy), as follows:

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

Run the command: spring run hello.groovyfirst download all kinds rely more slowly, and then will be directly started.
Here example uses the default port to start a web service.
By accessing http://127.0.0.1:8080/ get return data.

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

Setting JVM command line parameters, it can be JAVA_OPTSthe environment variables, as follows:

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

Inferred dependent

As: according to @Controlleror  @RestControlleror  @EnableWebMvcout of the inferredSpring MVC + Embedded Tomcat

The default guide package statement

For brevity, many guide package statement will automatically happen.

Automatic Derivation main method

Groovy script unlike java applications need to include the main method SpringApplicationis automatically created, your compiled code execution as a resource.

Custom Dependency Management

The default dependency management statement spring-boot-dependencies, you can pass @DependencyManagementBomthe configuration, you need to specify the value of the value of notes to meet 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"])

Running multiple resource files

spring run *.groovy

Packaged applications

spring java my-app.jar *.groovy

Jar containing the generated application is compiled classes and generated by the application of all the dependencies in order to use it to run java-jar. jar file also contains entries from the application classpath. You can use --includeand --excludeadd and remove packages explicit path to the jar.
Resources included by default as follows:

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

Excluded by default are as follows:

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

Initialize a new project

initYou do not have to leave the shell command allows the tool to directly use start.spring.io create a new project, the following example:

# 项目名为 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 my-projectthe Maven project, use spring-boot-starter-weband spring-boot-starter-data-jpa. You can use --listflag lists features and services. 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)

initCommand supports many operations, specifically through the helpcommand. Use the following example to create a Java 8, packaged into a war of Gradle project

$ 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'

Use plunge Shell

SpringBoot BASH and zsh shells containing the complete command-line script. If you do not need to use them (using windows system), you can shellstart an integrated 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.

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

$ version
Spring CLI v2.2.0.RELEASE

Embedded shell supports ANSI color output and tabtips. If you need to run native commands, you can use !the prefix, exiting embedded shell, use ctrl-c.

Add extension

Use installthe command add extensions. The extended format is accepted: group:artifact:versionas follows:

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

In addition to the installation target depends what you need, it's all dependencies will be installed.

Use uninstallthe command to uninstall dependence. And use installthe same.
It unload target depends, as well as all the dependencies goals.
Uninstall all extensions rely on, you can use:

$ spring uninstall --all

Application development using Groovy Beans DSL

spring framework 4.0 has support for DSL native. Bean definition may be used with the same format Grooby applications.

CLI configuration using settings.xml

Spring Boot CLI using Aether (Maven dependency parsing engine) to resolve dependencies. CLI using ~ / .m2 / settings.xml in maven configuration arranged Aether. CLI the following configurations:

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

Reference Documents

SpringBoot official document
public number: Yifei Come (focus on the Java domain knowledge in-depth study and orderly learning from the source to the principles of the system)

Yifei Come

Guess you like

Origin www.cnblogs.com/lw5946/p/11796354.html