Spicy Spring: Running Our Own Spring Initializr Server
To start a new project based on Spring or Spring Boot we can use the website start.spring.io. We can easily create a project templates based on Maven or Gradle and define all needed dependencies by clicking on checkboxes in the UI. In a previous post we also learned how to create a project using a URL using the same start.spring.io website. The start.spring.io website is actually a Spring Boot application and we can easily host our own server. With our own server we can for example limit the number of dependencies, force Gradle as the only build tool and set default values for project name, packages and much more.
To get started we must first clone the GitHub project.
We need to build the project with Maven to install the libraries in our local Maven repository.
After that is done we are reading to use it in our own Spring Boot application that is our customised Spring Initializr server.
The easiest way to run the server is to have the Spring CLI tool installed.
The easiest way to install it is using SDKMAN!.
We type on the command line $ sdk install springboot
.
Next we create a new directory and inside the directory we create a new Groovy file initializr.groovy
:
package app
@Grab('io.spring.initalizr:initializr-web:1.0.0.BUILD-SNAPSHOT')
@Grab('spring-boot-starter-web')
class InitializerService {}
Next we need a configuration file which all the options for the Spring Initializr server.
We can start by copying the file application.yml
from the initializr-service
project on GitHub to our directory with the file initializr.groovy
.
This file contains a lot of information.
The configuration format is explained on the GitHub wiki, but it is really straight forward.
If we open the file we can for example remove dependencies from the dependencies
or set default values for the groupId
.
If we only want to support Gradle we can remove the Maven references from the types
section.
And of course to have Groovy as the default language we can in the languages
section set the default
to true
for Groovy.
...
artifactId:
value: sample
groupId:
value: com.mrhaki
version:
value: 1.0.0.DEVELOPMENT
name:
value: Sample
description:
value: Sample Project
packageName:
value: com.mrhaki.demo
...
types:
- name: Gradle Project
id: gradle-project
description: Generate a Gradle based project archive
sts-id: gradle.zip
tags:
build: gradle
format: project
default: false
action: /starter.zip
- name: Gradle Config
id: gradle-build
description: Generate a Gradle build file
sts-id: build.gradle
tags:
build: gradle
format: build
default: true
action: /build.gradle
...
javaVersions:
- id: 1.8
default: true
languages:
- name: Java
id: java
default: false
- name: Groovy
id: groovy
default: true
...
And that is it! We are ready to start our own Spring Initializr server:
$ spring run initializr.groovy
...
For a finishing touch we can also override the static resources and templates of the server application.
For example we can add a new spring.css
file in the directory static/css
.
And place a file home.html
in the directory templates
.
We can take the existing files as sample and change what we want.
The following screenshot shows a customized template with some style changes:
We can even use our server to create a project from IntelliJ IDEA. When we create a new project and select Spring Initializr from the list box on the left we can type in the URL of our server:
Next we see our default values for the project details:
And finally we can select the dependencies we have defined in our application.yml
file: