Skip to content

Commit 56b8e41

Browse files
committed
Update getting started with alternative gradle instructions
Closes gh-32779
1 parent 5b10714 commit 56b8e41

File tree

1 file changed

+196
-11
lines changed

1 file changed

+196
-11
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/getting-started/first-application.adoc

Lines changed: 196 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
[[getting-started.first-application]]
22
== Developing Your First Spring Boot Application
33
This section describes how to develop a small "`Hello World!`" web application that highlights some of Spring Boot's key features.
4-
We use Maven to build this project, since most IDEs support it.
54

65
[TIP]
76
====
8-
The https://spring.io[spring.io] web site contains many "`Getting Started`" https://spring.io/guides[guides] that use Spring Boot.
7+
The https://spring.io[spring.io] website contains many "`Getting Started`" https://spring.io/guides[guides] that use Spring Boot.
98
If you need to solve a specific problem, check there first.
109
1110
You can shortcut the steps below by going to https://start.spring.io and choosing the "Web" starter from the dependencies searcher.
1211
Doing so generates a new project structure so that you can <<getting-started#getting-started.first-application.code,start coding right away>>.
1312
Check the https://github.com/spring-io/start.spring.io/blob/main/USING.adoc[start.spring.io user guide] for more details.
1413
====
1514

16-
Before we begin, open a terminal and run the following commands to ensure that you have valid versions of Java and Maven installed:
15+
[[getting-started.first-application.prerequisites]]
16+
=== Prerequisites
17+
18+
Before we begin, open a terminal and run the following commands to ensure that you have a valid version of Java installed:
1719

1820
[source,shell,indent=0,subs="verbatim"]
1921
----
@@ -23,6 +25,15 @@ Before we begin, open a terminal and run the following commands to ensure that y
2325
OpenJDK 64-Bit Server VM (build 17.0.4.1+1-LTS, mixed mode, sharing)
2426
----
2527

28+
NOTE: This sample needs to be created in its own directory.
29+
Subsequent instructions assume that you have created a suitable directory and that it is your current directory.
30+
31+
32+
[[getting-started.first-application.prerequisites.maven]]
33+
==== Maven
34+
35+
If you want to use Maven, ensure that you have Maven installed:
36+
2637
[source,shell,indent=0,subs="verbatim"]
2738
----
2839
$ mvn -v
@@ -31,13 +42,32 @@ Before we begin, open a terminal and run the following commands to ensure that y
3142
Java version: 17.0.4.1, vendor: BellSoft, runtime: /Users/developer/sdkman/candidates/java/17.0.4.1-librca
3243
----
3344

34-
NOTE: This sample needs to be created in its own directory.
35-
Subsequent instructions assume that you have created a suitable directory and that it is your current directory.
45+
[[getting-started.first-application.prerequisites.gradle]]
46+
==== Gradle
3647

48+
If you want to use Gradle, ensure that you have Gradle installed:
49+
50+
[source,shell,indent=0,subs="verbatim"]
51+
----
52+
$ gradle --version
53+
54+
------------------------------------------------------------
55+
Gradle 8.1.1
56+
------------------------------------------------------------
57+
58+
Build time: 2023-04-21 12:31:26 UTC
59+
Revision: 1cf537a851c635c364a4214885f8b9798051175b
60+
61+
Kotlin: 1.8.10
62+
Groovy: 3.0.15
63+
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
64+
JVM: 17.0.7 (BellSoft 17.0.7+7-LTS)
65+
OS: Linux 6.2.12-200.fc37.aarch64 aarch64
66+
----
3767

3868

3969
[[getting-started.first-application.pom]]
40-
=== Creating the POM
70+
=== Maven
4171
We need to start by creating a Maven `pom.xml` file.
4272
The `pom.xml` is the recipe that is used to build your project.
4373
Open your favorite text editor and add the following:
@@ -94,16 +124,56 @@ You can test it by running `mvn package` (for now, you can ignore the "`jar will
94124
NOTE: At this point, you could import the project into an IDE (most modern Java IDEs include built-in support for Maven).
95125
For simplicity, we continue to use a plain text editor for this example.
96126

127+
[[getting-started.first-application.gradle]]
128+
=== Gradle
129+
130+
We need to start by creating a Gradle `build.gradle` file.
131+
The `build.gradle` is the build script that is used to build your project.
132+
Open your favorite text editor and add the following:
97133

134+
[source,gradle,indent=0,subs="verbatim,attributes"]
135+
----
136+
plugins {
137+
id 'java'
138+
id 'org.springframework.boot' version '{spring-boot-version}'
139+
id 'io.spring.dependency-management' version '1.1.0'
140+
}
141+
142+
group = 'com.example'
143+
version = '0.0.1-SNAPSHOT'
144+
sourceCompatibility = '17'
145+
146+
repositories {
147+
mavenCentral()
148+
ifeval::["{artifact-release-type}" != "release"]
149+
maven { url 'https://repo.spring.io/milestone' }
150+
maven { url 'https://repo.spring.io/snapshot' }
151+
endif::[]
152+
}
153+
154+
dependencies {
155+
}
156+
----
157+
158+
The preceding listing should give you a working build.
159+
You can test it by running `gradle classes`.
160+
161+
NOTE: At this point, you could import the project into an IDE (most modern Java IDEs include built-in support for Gradle).
162+
For simplicity, we continue to use a plain text editor for this example.
98163

99164
[[getting-started.first-application.dependencies]]
100165
=== Adding Classpath Dependencies
166+
101167
Spring Boot provides a number of "`Starters`" that let you add jars to your classpath.
102-
Our applications for smoke tests use the `spring-boot-starter-parent` in the `parent` section of the POM.
168+
"`Starters`" provide dependencies that you are likely to need when developing a specific type of application.
169+
170+
[[getting-started.first-application.dependencies.maven]]
171+
==== Maven
172+
173+
Most Spring Boot applications use the `spring-boot-starter-parent` in the `parent` section of the POM.
103174
The `spring-boot-starter-parent` is a special starter that provides useful Maven defaults.
104175
It also provides a <<using#using.build-systems.dependency-management,`dependency-management`>> section so that you can omit `version` tags for "`blessed`" dependencies.
105176

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

@@ -130,12 +200,45 @@ To add the necessary dependencies, edit your `pom.xml` and add the `spring-boot-
130200

131201
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.
132202

203+
[[getting-started.first-application.dependencies.gradle]]
204+
==== Gradle
205+
206+
Most Spring Boot applications use the `org.springframework.boot` Gradle plugin.
207+
This plugin provides useful defaults and Gradle tasks.
208+
The `io.spring.dependency-management` Gradle plugin provides <<using#using.build-systems.dependency-management, dependency management>> so that you can omit `version` tags for "`blessed`" dependencies.
209+
210+
Since we are developing a web application, we add a `spring-boot-starter-web` dependency.
211+
Before that, we can look at what we currently have by running the following command:
212+
213+
[source,shell,indent=0,subs="verbatim"]
214+
----
215+
$ gradle dependencies
216+
217+
> Task :dependencies
218+
219+
------------------------------------------------------------
220+
Root project 'myproject'
221+
------------------------------------------------------------
222+
----
223+
224+
The `gradle dependencies` command prints a tree representation of your project dependencies.
225+
Right now, the project has no dependencies.
226+
To add the necessary dependencies, edit your `build.gradle` and add the `spring-boot-starter-web` dependency in the `dependencies` section:
227+
228+
[source,gradle,indent=0,subs="verbatim"]
229+
----
230+
dependencies {
231+
implementation 'org.springframework.boot:spring-boot-starter-web'
232+
}
233+
----
234+
235+
If you run `gradle dependencies` again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.
133236

134237

135238
[[getting-started.first-application.code]]
136239
=== Writing the Code
137240
To finish our application, we need to create a single Java file.
138-
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:
241+
By default, Maven and Gradle 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:
139242

140243
include::code:MyApplication[]
141244

@@ -191,6 +294,9 @@ The `args` array is also passed through to expose any command-line arguments.
191294

192295
[[getting-started.first-application.run]]
193296
=== Running the Example
297+
298+
[[getting-started.first-application.run.maven]]
299+
==== Maven
194300
At this point, your application should work.
195301
Since you used the `spring-boot-starter-parent` POM, you have a useful `run` goal that you can use to start the application.
196302
Type `mvn spring-boot:run` from the root project directory to start the application.
@@ -222,6 +328,39 @@ If you open a web browser to `http://localhost:8080`, you should see the followi
222328

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

331+
[[getting-started.first-application.run.gradle]]
332+
==== Gradle
333+
334+
At this point, your application should work.
335+
Since you used the `org.springframework.boot` Gradle plugin, you have a useful `bootRun` goal that you can use to start the application.
336+
Type `gradle bootRun` from the root project directory to start the application.
337+
You should see output similar to the following:
338+
339+
[source,shell,indent=0,subs="verbatim,attributes"]
340+
----
341+
$ gradle bootRun
342+
343+
. ____ _ __ _ _
344+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
345+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
346+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
347+
' |____| .__|_| |_|_| |_\__, | / / / /
348+
=========|_|==============|___/=/_/_/_/
349+
:: Spring Boot :: (v{spring-boot-version})
350+
....... . . .
351+
....... . . . (log output here)
352+
....... . . .
353+
........ Started MyApplication in 0.906 seconds (process running for 6.514)
354+
----
355+
356+
If you open a web browser to `http://localhost:8080`, you should see the following output:
357+
358+
[indent=0]
359+
----
360+
Hello World!
361+
----
362+
363+
To gracefully exit the application, press `ctrl-c`.
225364

226365

227366
[[getting-started.first-application.executable-jar]]
@@ -242,6 +381,9 @@ It can also be problematic if the same filename is used (but with different cont
242381
Spring Boot takes a <<executable-jar#appendix.executable-jar, different approach>> and lets you actually nest jars directly.
243382
****
244383

384+
[[getting-started.first-application.executable-jar.maven]]
385+
==== Maven
386+
245387
To create an executable jar, we need to add the `spring-boot-maven-plugin` to our `pom.xml`.
246388
To do so, insert the following lines just below the `dependencies` section:
247389

@@ -283,7 +425,7 @@ Save your `pom.xml` and run `mvn package` from the command line, as follows:
283425
----
284426

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

289431
[source,shell,indent=0,subs="verbatim"]
@@ -310,7 +452,50 @@ To run that application, use the `java -jar` command, as follows:
310452
....... . . .
311453
....... . . . (log output here)
312454
....... . . .
313-
........ Started MyApplication in 2.536 seconds (process running for 2.864)
455+
........ Started MyApplication in 0.999 seconds (process running for 1.253)
456+
----
457+
458+
As before, to exit the application, press `ctrl-c`.
459+
460+
[[getting-started.first-application.executable-jar.gradle]]
461+
==== Gradle
462+
463+
To create an executable jar, we need to run `gradle bootJar` from the command line, as follows:
464+
465+
[source,shell,indent=0,subs="verbatim,attributes"]
466+
----
467+
$ gradle bootJar
468+
469+
BUILD SUCCESSFUL in 639ms
470+
3 actionable tasks: 3 executed
471+
----
472+
473+
If you look in the `build/libs` directory, you should see `myproject-0.0.1-SNAPSHOT.jar`.
474+
The file should be around 18 MB in size.
475+
If you want to peek inside, you can use `jar tvf`, as follows:
476+
477+
[source,shell,indent=0,subs="verbatim"]
478+
----
479+
$ jar tvf build/libs/myproject-0.0.1-SNAPSHOT.jar
480+
----
481+
482+
To run that application, use the `java -jar` command, as follows:
483+
484+
[source,shell,indent=0,subs="verbatim,attributes"]
485+
----
486+
$ java -jar build/libs/myproject-0.0.1-SNAPSHOT.jar
487+
488+
. ____ _ __ _ _
489+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
490+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
491+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
492+
' |____| .__|_| |_|_| |_\__, | / / / /
493+
=========|_|==============|___/=/_/_/_/
494+
:: Spring Boot :: (v{spring-boot-version})
495+
....... . . .
496+
....... . . . (log output here)
497+
....... . . .
498+
........ Started MyApplication in 0.999 seconds (process running for 1.253)
314499
----
315500

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

0 commit comments

Comments
 (0)