getting-started.md 26.0 KB
Newer Older
Miykael_xxm's avatar
Miykael_xxm 已提交
1
# Getting Started
M
Mao 已提交
2 3 4 5 6 7

If you are getting started with Spring Boot, or “Spring” in general, start by reading this section.
It answers the basic “what?”, “how?” and “why?” questions.
It includes an introduction to Spring Boot, along with installation instructions.
We then walk you through building your first Spring Boot application, discussing some core principles as we go.

Miykael_xxm's avatar
Miykael_xxm 已提交
8
## 1. Introducing Spring Boot
M
Mao 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Spring Boot helps you to create stand-alone, production-grade Spring-based applications that you can run.
We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss.
Most Spring Boot applications need very little Spring configuration.

You can use Spring Boot to create Java applications that can be started by using `java -jar` or more traditional war deployments.
We also provide a command line tool that runs “spring scripts”.

Our primary goals are:

* Provide a radically faster and widely accessible getting-started experience for all Spring development.

* Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.

* Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).

* Absolutely no code generation and no requirement for XML configuration.

Miykael_xxm's avatar
Miykael_xxm 已提交
27
## 2. System Requirements
M
Mao 已提交
28 29 30 31 32 33 34 35 36 37

Spring Boot 2.6.4 requires [Java 8](https://www.java.com) and is compatible up to and including Java 17.[Spring Framework 5.3.16](https://docs.spring.io/spring-framework/docs/5.3.16/reference/html/) or above is also required.

Explicit build support is provided for the following build tools:

|Build Tool|       Version       |
|----------|---------------------|
|  Maven   |        3.5+         |
|  Gradle  |6.8.x, 6.9.x, and 7.x|

Miykael_xxm's avatar
Miykael_xxm 已提交
38
### 2.1. Servlet Containers
M
Mao 已提交
39 40 41 42 43 44 45 46 47 48 49 50

Spring Boot supports the following embedded servlet containers:

|    Name    |Servlet Version|
|------------|---------------|
| Tomcat 9.0 |      4.0      |
| Jetty 9.4  |      3.1      |
| Jetty 10.0 |      4.0      |
|Undertow 2.0|      4.0      |

You can also deploy Spring Boot applications to any servlet 3.1+ compatible container.

Miykael_xxm's avatar
Miykael_xxm 已提交
51
## 3. Installing Spring Boot
M
Mao 已提交
52 53 54 55 56 57 58 59 60 61 62 63

Spring Boot can be used with “classic” Java development tools or installed as a command line tool.
Either way, you need [Java SDK v1.8](https://www.java.com) or higher.
Before you begin, you should check your current Java installation by using the following command:

```
$ java -version
```

If you are new to Java development or if you want to experiment with Spring Boot, you might want to try the [Spring Boot CLI](#getting-started.installing.cli) (Command Line Interface) first.
Otherwise, read on for “classic” installation instructions.

Miykael_xxm's avatar
Miykael_xxm 已提交
64
### 3.1. Installation Instructions for the Java Developer
M
Mao 已提交
65 66 67 68 69 70 71 72

You can use Spring Boot in the same way as any standard Java library.
To do so, include the appropriate `spring-boot-*.jar` files on your classpath.
Spring Boot does not require any special tools integration, so you can use any IDE or text editor.
Also, there is nothing special about a Spring Boot application, so you can run and debug a Spring Boot application as you would any other Java program.

Although you *could* copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).

Miykael_xxm's avatar
Miykael_xxm 已提交
73
#### 3.1.1. Maven Installation
M
Mao 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86

Spring Boot is compatible with Apache Maven 3.3 or above.
If you do not already have Maven installed, you can follow the instructions at [maven.apache.org](https://maven.apache.org).

|   |On many operating systems, Maven can be installed with a package manager.<br/>If you use OSX Homebrew, try `brew install maven`.<br/>Ubuntu users can run `sudo apt-get install maven`.<br/>Windows users with [Chocolatey](https://chocolatey.org/) can run `choco install maven` from an elevated (administrator) prompt.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Spring Boot dependencies use the `org.springframework.boot` `groupId`.
Typically, your Maven POM file inherits from the `spring-boot-starter-parent` project and declares dependencies to one or more [“Starters”](using.html#using.build-systems.starters).
Spring Boot also provides an optional [Maven plugin](build-tool-plugins.html#build-tool-plugins.maven) to create executable jars.

More details on getting started with Spring Boot and Maven can be found in the [Getting Started section](https://docs.spring.io/spring-boot/docs/2.6.4/maven-plugin/reference/htmlsingle/#getting-started) of the Maven plugin’s reference guide.

Miykael_xxm's avatar
Miykael_xxm 已提交
87
#### 3.1.2. Gradle Installation
M
Mao 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

Spring Boot is compatible with Gradle 6.8, 6.9, and 7.x.
If you do not already have Gradle installed, you can follow the instructions at [gradle.org](https://gradle.org).

Spring Boot dependencies can be declared by using the `org.springframework.boot` `group`.
Typically, your project declares dependencies to one or more [“Starters”](using.html#using.build-systems.starters).
Spring Boot provides a useful [Gradle plugin](build-tool-plugins.html#build-tool-plugins.gradle) that can be used to simplify dependency declarations and to create executable jars.

Gradle Wrapper

The Gradle Wrapper provides a nice way of “obtaining” Gradle when you need to build a project.
It is a small script and library that you commit alongside your code to bootstrap the build process.
See [docs.gradle.org/current/userguide/gradle\_wrapper.html](https://docs.gradle.org/current/userguide/gradle_wrapper.html) for details.

More details on getting started with Spring Boot and Gradle can be found in the [Getting Started section](https://docs.spring.io/spring-boot/docs/2.6.4/gradle-plugin/reference/htmlsingle/#getting-started) of the Gradle plugin’s reference guide.

Miykael_xxm's avatar
Miykael_xxm 已提交
104
### 3.2. Installing the Spring Boot CLI
M
Mao 已提交
105 106 107 108 109 110

The Spring Boot CLI (Command Line Interface) is a command line tool that you can use to quickly prototype with Spring.
It lets you run [Groovy](https://groovy-lang.org/) scripts, which means that you have a familiar Java-like syntax without so much boilerplate code.

You do not need to use the CLI to work with Spring Boot, but it is a quick way to get a Spring application off the ground without an IDE.

Miykael_xxm's avatar
Miykael_xxm 已提交
111
#### 3.2.1. Manual Installation
M
Mao 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124

You can download the Spring CLI distribution from the Spring software repository:

* [spring-boot-cli-2.6.4-bin.zip](https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.6.4/spring-boot-cli-2.6.4-bin.zip)

* [spring-boot-cli-2.6.4-bin.tar.gz](https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.6.4/spring-boot-cli-2.6.4-bin.tar.gz)

Cutting edge[snapshot distributions](https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/) are also available.

Once downloaded, follow the [INSTALL.txt](https://raw.githubusercontent.com/spring-projects/spring-boot/v2.6.4/spring-boot-project/spring-boot-cli/src/main/content/INSTALL.txt) instructions from the unpacked archive.
In summary, there is a `spring` script (`spring.bat` for Windows) in a `bin/` directory in the `.zip` file.
Alternatively, you can use `java -jar` with the `.jar` file (the script helps you to be sure that the classpath is set correctly).

Miykael_xxm's avatar
Miykael_xxm 已提交
125
#### 3.2.2. Installation with SDKMAN!
M
Mao 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI.
Get SDKMAN! from [sdkman.io](https://sdkman.io) and install Spring Boot by using the following commands:

```
$ sdk install springboot
$ spring --version
Spring CLI v2.6.4
```

If you develop features for the CLI and want access to the version you built, use the following commands:

```
$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-2.6.4-bin/spring-2.6.4/
$ sdk default springboot dev
$ spring --version
Spring CLI v2.6.4
```

The preceding instructions install a local instance of `spring` called the `dev` instance.
It points at your target build location, so every time you rebuild Spring Boot, `spring` is up-to-date.

You can see it by running the following command:

```
$ sdk ls springboot

================================================================================
Available Springboot Versions
================================================================================
> + dev
* 2.6.4

================================================================================
+ - local version
* - installed
> - currently in use
================================================================================
```

Miykael_xxm's avatar
Miykael_xxm 已提交
166
#### 3.2.3. OSX Homebrew Installation
M
Mao 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179

If you are on a Mac and use [Homebrew](https://brew.sh/), you can install the Spring Boot CLI by using the following commands:

```
$ brew tap spring-io/tap
$ brew install spring-boot
```

Homebrew installs `spring` to `/usr/local/bin`.

|   |If you do not see the formula, your installation of brew might be out-of-date.<br/>In that case, run `brew update` and try again.|
|---|---------------------------------------------------------------------------------------------------------------------------------|

Miykael_xxm's avatar
Miykael_xxm 已提交
180
#### 3.2.4. MacPorts Installation
M
Mao 已提交
181 182 183 184 185 186 187

If you are on a Mac and use [MacPorts](https://www.macports.org/), you can install the Spring Boot CLI by using the following command:

```
$ sudo port install spring-boot-cli
```

Miykael_xxm's avatar
Miykael_xxm 已提交
188
#### 3.2.5. Command-line Completion
M
Mao 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

The Spring Boot CLI includes scripts that provide command completion for the [BASH](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) and [zsh](https://en.wikipedia.org/wiki/Z_shell) shells.
You can `source` the script (also named `spring`) in any shell or put it in your personal or system-wide bash completion initialization.
On a Debian system, the system-wide scripts are in `/shell-completion/bash` and all scripts in that directory are executed when a new shell starts.
For example, to run the script manually if you have installed by using SDKMAN!, use the following commands:

```
$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
  grab  help  jar  run  test  version
```

|   |If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-line completion scripts are automatically registered with your shell.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------|

Miykael_xxm's avatar
Miykael_xxm 已提交
204
#### 3.2.6. Windows Scoop Installation
M
Mao 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217

If you are on a Windows and use [Scoop](https://scoop.sh/), you can install the Spring Boot CLI by using the following commands:

```
> scoop bucket add extras
> scoop install springboot
```

Scoop installs `spring` to `~/scoop/apps/springboot/current/bin`.

|   |If you do not see the app manifest, your installation of scoop might be out-of-date.<br/>In that case, run `scoop update` and try again.|
|---|----------------------------------------------------------------------------------------------------------------------------------------|

Miykael_xxm's avatar
Miykael_xxm 已提交
218
#### 3.2.7. Quick-start Spring CLI Example
M
Mao 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

You can use the following web application to test your installation.
To start, create a file called `app.groovy`, as follows:

```
@RestController
class ThisWillActuallyRun {

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

}

```

Then run it from a shell, as follows:

```
$ spring run app.groovy
```

|   |The first run of your application is slow, as dependencies are downloaded.<br/>Subsequent runs are much quicker.|
|---|----------------------------------------------------------------------------------------------------------------|

Open `[localhost:8080](http://localhost:8080)` in your favorite web browser.
You should see the following output:

```
Hello World!
```

Miykael_xxm's avatar
Miykael_xxm 已提交
252
## 4. Developing Your First Spring Boot Application
M
Mao 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

This section describes how to develop a small “Hello World!” web application that highlights some of Spring Boot’s key features.
We use Maven to build this project, since most IDEs support it.

|   |The [spring.io](https://spring.io) web site contains many “Getting Started” [guides](https://spring.io/guides) that use Spring Boot.<br/>If you need to solve a specific problem, check there first.<br/><br/>You can shortcut the steps below by going to [start.spring.io](https://start.spring.io) and choosing the "Web" starter from the dependencies searcher.<br/>Doing so generates a new project structure so that you can [start coding right away](#getting-started.first-application.code).<br/>Check the [start.spring.io user guide](https://github.com/spring-io/start.spring.io/blob/main/USING.adoc) for more details.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Before we begin, open a terminal and run the following commands to ensure that you have valid versions of Java and Maven installed:

```
$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
```

```
$ mvn -v
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_102, vendor: Oracle Corporation
```

|   |This sample needs to be created in its own directory.<br/>Subsequent instructions assume that you have created a suitable directory and that it is your current directory.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Miykael_xxm's avatar
Miykael_xxm 已提交
279
### 4.1. Creating the POM
M
Mao 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

We need to start by creating a Maven `pom.xml` file.
The `pom.xml` is the recipe that is used to build your project.
Open your favorite text editor and add the following:

```
<?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.6.4</version>
    </parent>

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

</project>
```

The preceding listing should give you a working build.
You can test it by running `mvn package` (for now, you can ignore the “jar will be empty - no content was marked for inclusion!” warning).

|   |At this point, you could import the project into an IDE (most modern Java IDEs include built-in support for Maven).<br/>For simplicity, we continue to use a plain text editor for this example.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Miykael_xxm's avatar
Miykael_xxm 已提交
312
### 4.2. Adding Classpath Dependencies
M
Mao 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

Spring Boot provides a number of “Starters” that let you add jars to your classpath.
Our applications for smoke tests use the `spring-boot-starter-parent` in the `parent` section of the POM.
The `spring-boot-starter-parent` is a special starter that provides useful Maven defaults.
It also provides a [`dependency-management`](using.html#using.build-systems.dependency-management) section so that you can omit `version` tags for “blessed” dependencies.

Other “Starters” provide dependencies that you are likely to need when developing a specific type of application.
Since we are developing a web application, we add a `spring-boot-starter-web` dependency.
Before that, we can look at what we currently have by running the following command:

```
$ mvn dependency:tree

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

The `mvn dependency:tree` command prints a tree representation of your project dependencies.
You can see that `spring-boot-starter-parent` provides no dependencies by itself.
To add the necessary dependencies, edit your `pom.xml` and add the `spring-boot-starter-web` dependency immediately below the `parent` section:

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

If you run `mvn dependency:tree` again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.

Miykael_xxm's avatar
Miykael_xxm 已提交
344
### 4.3. Writing the Code
M
Mao 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

To finish our application, we need to create a single Java file.
By default, Maven compiles sources from `src/main/java`, so you need to create that directory structure and then add a file named `src/main/java/MyApplication.java` to contain the following code:

```
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);
    }

}

```

Although there is not much code here, quite a lot is going on.
We step through the important parts in the next few sections.

Miykael_xxm's avatar
Miykael_xxm 已提交
375
#### 4.3.1. The @RestController and @RequestMapping Annotations ####
M
Mao 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388

The first annotation on our `MyApplication` class is `@RestController`.
This is known as a *stereotype* annotation.
It provides hints for people reading the code and for Spring that the class plays a specific role.
In this case, our class is a web `@Controller`, so Spring considers it when handling incoming web requests.

The `@RequestMapping` annotation provides “routing” information.
It tells Spring that any HTTP request with the `/` path should be mapped to the `home` method.
The `@RestController` annotation tells Spring to render the resulting string directly back to the caller.

|   |The `@RestController` and `@RequestMapping` annotations are Spring MVC annotations (they are not specific to Spring Boot).<br/>See the [MVC section](https://docs.spring.io/spring-framework/docs/5.3.16/reference/html/web.html#mvc) in the Spring Reference Documentation for more details.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Miykael_xxm's avatar
Miykael_xxm 已提交
389
#### 4.3.2. The @EnableAutoConfiguration Annotation ####
M
Mao 已提交
390 391 392 393 394 395 396 397 398 399 400

The second class-level annotation is `@EnableAutoConfiguration`.
This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added.
Since `spring-boot-starter-web` added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

Starters and Auto-configuration

Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied.
You are free to pick and choose jar dependencies outside of the starters.
Spring Boot still does its best to auto-configure your application.

Miykael_xxm's avatar
Miykael_xxm 已提交
401
#### 4.3.3. The “main” Method
M
Mao 已提交
402 403 404 405 406 407 408

The final part of our application is the `main` method.
This is a standard method that follows the Java convention for an application entry point.
Our main method delegates to Spring Boot’s `SpringApplication` class by calling `run`.`SpringApplication` bootstraps our application, starting Spring, which, in turn, starts the auto-configured Tomcat web server.
We need to pass `MyApplication.class` as an argument to the `run` method to tell `SpringApplication` which is the primary Spring component.
The `args` array is also passed through to expose any command-line arguments.

Miykael_xxm's avatar
Miykael_xxm 已提交
409
### 4.4. Running the Example
M
Mao 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

At this point, your application should work.
Since you used the `spring-boot-starter-parent` POM, you have a useful `run` goal that you can use to start the application.
Type `mvn spring-boot:run` from the root project directory to start the application.
You should see output similar to the following:

```
$ mvn spring-boot:run

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

If you open a web browser to `[localhost:8080](http://localhost:8080)`, you should see the following output:

```
Hello World!
```

To gracefully exit the application, press `ctrl-c`.

Miykael_xxm's avatar
Miykael_xxm 已提交
440
### 4.5. Creating an Executable Jar
M
Mao 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

We finish our example by creating a completely self-contained executable jar file that we could run in production.
Executable jars (sometimes called “fat jars”) are archives containing your compiled classes along with all of the jar dependencies that your code needs to run.

Executable jars and Java

Java does not provide a standard way to load nested jar files (jar files that are themselves contained within a jar).
This can be problematic if you are looking to distribute a self-contained application.

To solve this problem, many developers use “uber” jars.
An uber jar packages all the classes from all the application’s dependencies into a single archive.
The problem with this approach is that it becomes hard to see which libraries are in your application.
It can also be problematic if the same filename is used (but with different content) in multiple jars.

Spring Boot takes a [different approach](executable-jar.html#appendix.executable-jar) and lets you actually nest jars directly.

To create an executable jar, we need to add the `spring-boot-maven-plugin` to our `pom.xml`.
To do so, insert the following lines just below the `dependencies` section:

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

|   |The `spring-boot-starter-parent` POM includes `<executions>` configuration to bind the `repackage` goal.<br/>If you do not use the parent POM, you need to declare this configuration yourself.<br/>See the [plugin documentation](https://docs.spring.io/spring-boot/docs/2.6.4/maven-plugin/reference/htmlsingle/#getting-started) for details.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Save your `pom.xml` and run `mvn package` from the command line, as follows:

```
$ mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.6.4:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
```

If you look in the `target` directory, you should see `myproject-0.0.1-SNAPSHOT.jar`.
The file should be around 10 MB in size.
If you want to peek inside, you can use `jar tvf`, as follows:

```
$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar
```

You should also see a much smaller file named `myproject-0.0.1-SNAPSHOT.jar.original` in the `target` directory.
This is the original jar file that Maven created before it was repackaged by Spring Boot.

To run that application, use the `java -jar` command, as follows:

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

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

As before, to exit the application, press `ctrl-c`.

Miykael_xxm's avatar
Miykael_xxm 已提交
525
## 5. What to Read Next
M
Mao 已提交
526 527 528 529 530 531 532

Hopefully, this section provided some of the Spring Boot basics and got you on your way to writing your own applications.
If you are a task-oriented type of developer, you might want to jump over to [spring.io](https://spring.io) and follow some of the [getting started](https://spring.io/guides/) guides that solve specific “How do I do that with Spring?” problems.
We also have Spring Boot-specific “[How-to](howto.html#howto)” reference documentation.

Otherwise, the next logical step is to read *[using.html](using.html#using)*.
If you are really impatient, you could also jump ahead and read about *[Spring Boot features](features.html#features)*.