Spring Initializr

In this post, we are quickly covering various features of the Spring Initializr tool.

 

Introduction

Spring Initializr provides a simple and intuitive web UI to create and configure and generate Spring based application. This tool makes it easy for developers to generate an initial project structure without worrying much about the project structure and dependencies. On a high-level Spring Initializr tools take cares of the following points for any Spring based application

  • Project structure.
  • Dependencies required to start the work.
  • build script (Maven or Gradle) to build the application.
  • Language and version (Initializr will add correct dependencies based on the version)
  • packaging (war or jar)

Spring Initializr is available on the web, most of the IDE has a built-in integration with the Initializr.

 

1. Spring Initializr

Let’s look at the Initializr interface to start with. Go to start.spring.io, you will have a similar screen.

Spring Initializr

This is the simplest Initializr view with minimal options to build your project. Let’s quickly discuss these fields for a basic understanding.

  • Generate as – This gives us the option to select the project as Maven or gradle based. In this post, we choose Maven. This selection generates a pom.xml file for our project.
  • With – The programming language to use (We chose Java). You have the option to select Groovy or Kotlin.
  • Spring Boot Version – Spring Boot version.
  • Group – The groupId attribute in Apache Maven, also known as the id of the project’s group.
  • Artifact – The name of the project, also known by the artifactId attribute in Apache Maven.
  • Dependencies – List of the dependencies for your application. We will see the different options under the advanced section. I am selecting “web” as a dependency for this post.

Once you provided these details, click on the “Generate Project” button. Spring Initializr provides a zip file containing a Maven/Gradle project. Below is the structure for our generated Maven project.

mvnw
mvnw.cmd
pom.xml
src
├── main
│   ├── java
│   │   └── com
│   │       └── javadevjournal
│   │           └── springinitializrsample
|   |               └── SpringInitializrSampleApplication.java
│   └── resources
│       ├── application.properties
│       ├── static
│       └── templates
└── test
    └── java
        └── com
            └── javadevjournal
                └── springinitializrsample
                    └── SpringInitializrSampleApplicationTests.java

Above structure automatically created by the tool based on the chosen dependencies. Since we chose “web” as our dependencies, Spring Initializr has detected it is a web application, a static and templates directories created to hold your static resources and UI templates.

[pullquote align=”normal”]Spring Initializr is smart enough to include Maven wrapper in the project structure. This wrapper makes sure we don’t have to install Maven to run this project. [/pullquote]

To build and run this application without installing Maven use the following command ./mvnw install

 

2. Spring Initializr Advance Options

Above section describe the least view of the tool, It also offers an advance view of the advanced users. Click on the “Switch to the full version” link to get this view.

Spring Initializr Full View

 

Let’s look at a few of the advanced properties (We already covered a few in Section 1)

  • Description -Description of the project.
  • Package Name – Root package of the project. If not specified, the value of the Group attribute used.
  • Java Version – the Java version to use.

2.1 Dependencies

Apart from this, Spring Initializr provides a detail view of the dependencies section where we can choose different dependencies required for our application. Here is a partial view of this section.

Depdencies section

 

There are certain use cases when a certain dependency is not available in the given Spring Boot version. If you ever face such issue, click on the advanced section to see if the required dependency is available in the selected version of the Spring Boot.

2.2 Default Values

This service comes with certain default values to generate a new project with minimum fuss. In case you don’t give these values, the Initializr service will pick these default values while generating your project structure. Here are the default configurations for your reference

  • Default language is Java.
  • Default build tool is Maven.
  • Packaging default to jar

 

3. Initializr Using Command Line

If you don’t like the web interface or more comfortable using command-line tools, the Initializr service offer an option to use the command line for generating a project structure for your application. We can use 

  • cURL
  • HTTPie

If you run curl start.spring.io in the command line, you get a response from the service, at the end of the response, you may find a similar output

To create a default demo.zip:
	$ curl https://start.spring.io/starter.zip -o demo.zip

To create a web project using Java 10:
	$ curl https://start.spring.io/starter.zip -d dependencies=web \\
			-d javaVersion=10 -o demo.zip

To create a web/data-jpa gradle project unpacked:
	$ curl https://start.spring.io/starter.tgz -d dependencies=web,data-jpa \\
		   -d type=gradle-project -d baseDir=my-dir | tar -xzvf -

To generate a Maven POM with war packaging:
	$ curl https://start.spring.io/pom.xml -d packaging=war -o pom.xml

Either you can use one of the above options to generate a project for your application or you can pass information to the service to generate a project based on your requirements. Let’s assume, we like to generate a web project based on Spring Boot 2.0.5.RELEASE using web and devtools as dependencies.

curl https://start.spring.io/starter.zip -d dependencies=web,devtools \
           -d bootVersion=2.0.5.RELEASE -o javadevjournal-initializr-project.zip

 

4. IDE

All the major Java IDE provides a built-in support for the Spring Initializr. Following IDE provides built-in support for the Initializr service.

 

5. Spring Initializr Video

Here is a quick video to get you started with Spring Initializr:

[responsive_video type=’youtube’ hide_related=’1′ hide_logo=’1′ hide_controls=’0′ hide_title=’0′ hide_fullscreen=’0′ autoplay=’0′]https://www.youtube.com/watch?v=K2X_QLo4ugE[/responsive_video]

 

Summary

In this post, we discuss different features of the Spring Initializr service. We discuss the different options like a web, command line and IDE for using this service. Initializr is a powerful service which helps to create to speed up project bootstrapping by taking out the setup complexities. There is a minimal code involved, and the service has a rich configuration structure, allowing you to define not only the values of various project attributes but also the list of dependencies and the constraints to apply to them.