Hello friends lets discuss another way to create spring boot project structure by using Spring Boot Initializr. Spring Boot Initializr is used to quick start new Spring Boot Maven/Gradle projects within no time. It generates initial project structure and builds scripts to reduce Development time.
As previous chapter we have seen following ways for Spring Boot Initialzr.
- Spring Boot Initilizr through Web Interface
- Spring Boot Initilizr through IDEs/IDE Plugins
- Spring Boot Initilizr through Spring Boot CLI
Before starting discussion for this tutorial we have to go through my previous chapter (Spring Boot CLI installation and Hello World Example) for Basic and Installation guide for Spring Boot CLI. As in previous chapter we saw, the Spring Boot CLI is a great way to develop Spring applications by just writing code. However, the Spring Boot CLI also has a few commands that can help us use the Initializr to kick-start development on a more traditional Java project.
Spring Boot CLI provides a “spring init” command to bootstrap Spring Applications.
Syntax for Init Command:
spring init [options] [location]
Here “options” are command options and “location” is specified our file system location to create new Spring Boot Project.
Examples for Spring Init Command:
Example 1: Use default setting
$ spring init
After contacting the Initializr web application, the init command will conclude by downloading a demo.zip file with following default setting.
- Default Build tool is “maven”.
- Default Spring Initilizr service target URL: https://start.spring.io
- Default project name: “demo”
- Default maven type: “jar”
- Default only baseline starter dependencies for Spring Boot and testing
Example 2: Using required dependencies on web project
Let’s say you want to start out by building a web application that uses JPA for data persistence and that’s secured with Spring Security. You can specify those initial dependencies with either –dependencies or -d:
$ spring init –dweb, jpa, security
This will give you a demo.zip containing the same project structure as before, but with Spring Boot’s web, JPA, and security starters expressed as dependencies in pom.xml.
Example 3: Using required dependencies on web project with change Build (Maven to Gradle)
$ spring init –dweb, jpa, security –build gradle
$ spring init –dweb, jpa, security –build maven (maven is default build type)
Example 4: Change packaging
By default, the build specification for both Maven and Gradle builds will produce an executable JAR file. If you’d rather produce a WAR file, you can specify so with the –packaging or -p parameter:
$ spring init –dweb, jpa, security –build gradle –p war
Example 5: Change application name
$ spring init –dweb, jpa, security –build gradle –p war myapp.zip
Example 6: Specifying the Java version to compile with, and selecting a version of Spring Boot
$ spring init –dweb, jpa, security –build gradle –java-version=1.7 –boot-version=1.3.0.RELEASE –p war myapp.zip
Other more commands
We can discover all of the parameters by using the help command:
$ spring help init
For lists several parameters that are supported by the Initializr.
spring init -l
Whether we use Initializr’s web-based interface, create our projects from Spring Tool Suite, or use the Spring Boot CLI to initialize a project, projects created using the Spring Boot Initializr have a familiar project layout, not unlike other Java projects we may have developed before.
Happy Spring Boot Learning 🙂