Spring Boot CLI使用总结

2023-01-08 16:05:12

一.下载

本机为Windows10系统,下载zip包(参考文档)

二.配置环境变量

配置spring方式和配置Java JDK一样,像这样:右键我的电脑->高级系统设置->环境变量

1.系统变量下新建一个SPRING_HOME,变量值为刚刚下载的文件解压后放置的目录.例如       

D:\spring_home\spring-2.5.3

 2.系统变量下双击path添加 如下路径

%SPRING_HOME%\bin

 3.打开cmd查看 

spring --version

出现 Spring CLI v2.5.3 说明安装成功.

三.简单使用

1.新建一个groovy文件,名称为app.groovy,内容如下

	@RestController
	class ThisWillActuallyRun {

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

	}

2.app.groovy同级目录下使用shell运行

spring run app.groovy

提示:应用程序的第一次运行很慢,因为下载了依赖项.后续运行要快得多. 

3.我们发现当前用户家目录下多了一个 .m2 文件夹,这是刚刚运行文件下载的依赖项

4.在您喜欢的网络浏览器中打开 localhost:8080 您应该看到以下输出:

Hello World!

四.创建maven项目

1.新建一个文件夹,名字为你的项目名.例如:myproject

2.进入myproject,默认为项目根目录,创建pom.xml,内容如下:

	<?xml version="1.0" encoding="UTF-8"?>
	<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<modelVersion>4.0.0</modelVersion>

		<groupId>com.example</groupId>
		<artifactId>myproject</artifactId>
		<version>0.0.1-SNAPSHOT</version>

		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>2.5.3</version>
		</parent>

		<!-- Additional lines to be added here... -->

	</project>

此时,可以用shell查看maven依赖结构

mvn dependency:tree

等待构建完毕,可以看到结构

[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

添加web依赖项

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

再次使用刚才的命令查看依赖结构

[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ myproject ---
[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.5.3:compile
[INFO]    +- org.springframework.boot:spring-boot-starter:jar:2.5.3:compile
[INFO]    |  +- org.springframework.boot:spring-boot:jar:2.5.3:compile
[INFO]    |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.5.3:compile
[INFO]    |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.5.3:compile
[INFO]    |  |  +- ch.qos.logback:logback-classic:jar:1.2.4:compile
[INFO]    |  |  |  +- ch.qos.logback:logback-core:jar:1.2.4:compile
[INFO]    |  |  |  \- org.slf4j:slf4j-api:jar:1.7.32:compile
[INFO]    |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.14.1:compile
[INFO]    |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.14.1:compile
[INFO]    |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.32:compile
[INFO]    |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO]    |  +- org.springframework:spring-core:jar:5.3.9:compile
[INFO]    |  |  \- org.springframework:spring-jcl:jar:5.3.9:compile
[INFO]    |  \- org.yaml:snakeyaml:jar:1.28:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-json:jar:2.5.3:compile
[INFO]    |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.12.4:compile
[INFO]    |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.12.4:compile
[INFO]    |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.12.4:compile
[INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.12.4:compile
[INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.12.4:compile
[INFO]    |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.12.4:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.5.3:compile
[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.50:compile
[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.50:compile
[INFO]    |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.50:compile
[INFO]    +- org.springframework:spring-web:jar:5.3.9:compile
[INFO]    |  \- org.springframework:spring-beans:jar:5.3.9:compile
[INFO]    \- org.springframework:spring-webmvc:jar:5.3.9:compile
[INFO]       +- org.springframework:spring-aop:jar:5.3.9:compile
[INFO]       +- org.springframework:spring-context:jar:5.3.9:compile
[INFO]       \- org.springframework:spring-expression:jar:5.3.9:compile
[INFO] ------------------------------------------------------------------------

可以看到好多熟悉的依赖项,包括tomcat和spring boot本身.

3.编写代码

默认情况下,Maven 从 src/main/java 编译源代码,因此需要创建该目录结构.src/main/java/下添加一个名为MyApplication.java包含以下代码的文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

4.运行代码

mvn spring-boot:run

你应该会看到类似如下的输出


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.5.3)
....... . . .
....... . . . (log output here)
....... . . .
........ Started MyApplication in 2.222 seconds (JVM running for 6.514)

如果您打开 Web 浏览器访问localhost:8080,您应该会看到以下输出:

Hello World!

要退出应用程序,请按ctrl-c 

5.创建一个可执行的Jar

要创建一个可执行的jar,我们需要添加spring-boot-maven-plugin到我们pom.xml.为此,请在该dependencies部分下方插入以下几行:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

保存pom.xml并从命令行运行 mvn package ,如下所示:

[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:myproject >------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\86188\Desktop\myproject\src\main\resources
[INFO] skip non existing resourceDirectory C:\Users\86188\Desktop\myproject\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ myproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\86188\Desktop\myproject\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ myproject ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ myproject ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ myproject ---
[INFO] Building jar: C:\Users\86188\Desktop\myproject\target\myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.5.3:repackage (repackage) @ myproject ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

查看包内文件

jar tvf target/myproject-0.0.1-SNAPSHOT.jar

6.运行jar

java -jar target/myproject-0.0.1-SNAPSHOT.jar

和前面一样,要退出应用程序,请按ctrl-c.

  • 作者:yaoyongcsdn
  • 原文链接:https://blog.csdn.net/weixin_42759726/article/details/119286242
    更新时间:2023-01-08 16:05:12