LAB 3.
1: HELLO BOOT WORLD
In this lab, you will configure and run a simple
Spring Boot job
Lab 3.1: Hello Boot World
LAB
SYNOPSI Lab
S
Overview: In this lab, we will become familiar with Spring
Boot and run a simple application that uses it
– We'll provide all of the code and configuration
– You will just run it, and examine how Spring Boot works
– You will use maven and Eclipse to work with it
– You'll also become familiar with the maven-based project
structure
Builds on previous labs: None
Approximate Time: 20-30 minutes
Lab 3.1: Hello Boot World 2
IMPORT MAVEN Lab
PROJECT
Eclipse INTO
understands maven ECLIPSE
projects fairly well
– The starter files (e.g. pom.xml, and Java source) are supplied by us
– When importing, Eclipse examines pom.xml, and sets up the classpath
– It understands maven's project structure, and sets up the Eclipse project
– It will compile the source as normal when importing (or changing source)
• No need to use maven for a compile or program execution- Eclipse does it
Tasks to Perform
Import the Lab03.1 project into Eclipse as follows:
– File | Import … | Maven | Existing Maven Projects
– Click Next, Browse to the workspace\Lab03.1 folder, click Finish
You may have errors in the project
– You may (or may not) be able to reach the maven repositories
– It depends on your environment setup (e.g. firewalls)
– We'll check that, and resolve any problems - see next slide
Lab 3.1: Hello Boot World 3
CHECK MAVEN Lab
REPOSITORY ACCESS
Tasks to Perform
Check the console window for error messages
– If you see an error like that below - starting with "Project build error:
Non-resolvable parent POM …", you can't reach the maven repository
• Likely due to firewall/security restrictions
If you see this error, follow the instructions on the next slide
– See notes on this page if you can't get maven to work any way at all (1)
If you do NOT see such an error, then skip the next slide
– It indicates you can reach the maven repositories
Lab 3.1: Hello Boot World 4
SETUP MAVEN Lab
REPOSITORY
Tasks ACCESS
to Perform
Only perform these steps if you can't access the maven repository
To resolve the error, you will replace your maven repository
– With one that we supply, that is pre-populated with all needed libraries
Go to your home folder (1), and find a folder named .m2 ("dot"m2)
– If it exists, rename that folder to .m2-ORIG (2)
– If it doesn't exist, that's no problem, just continue below (but read the note)
Copy the .m2 folder in StudentWork\Spring\mavenRepository
– Paste it to your home folder
– That's it - you've got a local maven repo with all the jars you need
Update the Lab03.1 project - Right Click | maven | Update Project
– The error should now go away - your maven repo is set up
Remember to restore your original .m2 folder after the course
– Rename the .m2 lab repo to .m2-Spring, and rename .m2-ORIG to .m2
Lab 3.1: Hello Boot World 5
REVIEW THE Lab
PROJECT POM
Tasks to Perform
Within Eclipse, open pom.xml for review
– First view the straight source (pom.xml tab)
– The Dependency Hierarchy tab shows direct/indirect dependencies
Lab 3.1: Hello Boot World 6
RUN THE Lab
APPLICATION
Tasks to Perform
Right click on HelloBootWorld.java (in Eclipse Package Explorer)
– Select Run As | Java Application
– It should run cleanly, and produce output like that below
– Note output from main(), and from run() (CommandLineRunner)
– Open HelloBootWorld for review to see how these are produced
Lab 3.1: Hello Boot World 7
REVIEW
DEBUGGING Lab
OUTPUT Tasks to Perform
Spring Boot has copious debugging built in
– Open src/main/resources/application.properties, set boot logging to debug
logging.level.org.springframework.boot=debug
– Rerun - this prints out a lot of info - e.g. the auto-configuration (see below)
Lab 3.1: Hello Boot World 8
CREATE
EXECUTABLE Lab
JAR Tasks to Perform
We will package the project with maven from within Eclipse (1)
– Right click on the project, select Run As | Maven build …
• Name the Run configuration Lab03.1-package
• Enter a goal of package
• Select Apply, then Run - This runs the mvn package command
• See notes for possible errors
– Creates a jar (the default maven packaging) in the target folder
– We've included the spring boot maven plugin in the POM
• Review it in pom.xml - it has an artifactId of spring-boot-maven-plugin
• It repackages the jar as an executable jar with all needed dependencies
• The original (.jar.original extension) and repackaged jar (.jar) are both
produced
– Go to the Navigator view (2), right click on Lab03.1, select
Refresh
– You should see the jars that were created (in the target folder)
Lab 3.1: Hello Boot World 9
NOTE ON MAVEN Lab
REPOSITORIES
We've occasionally seen odd errors in maven projects with
Eclipse
– Either compile or runtime
– Hard to track, and hard to figure out where they come from
Sometimes simply updating the project solves this
– Right Click | maven | Update project…
Sometimes there is something odd in your repository
– So you may need to rename the existing repo to something
else
• e.g. .m2 to .m2.ORIG
– Then update the project
– This only works if you have access to the maven repo
STOP
Keep this in mind if you get odd errors occurring
Lab 3.1: Hello Boot World 10