Comments on the Approach

The Spring team has created two ways to create a new project and one way to modify an existing project. We find that the optimum approach is to generate a project with Spring Initializr and then add to it with the boot add command. The following sections explain why that is the best approach:

Initializr

The common way to create a new Spring Boot project has been to use Initializr, at start.spring.io. The Spring CLI provides you with a command to interact with Initializr. See Initializr but also offers other approaches to creating a new project and addresses the day-2 use case of adding code or configuration to an existing project.

Using Boot New

The boot new command lets you create a new project from an existing one that lives in a source code repository. The boot new command lets you specify a small set of options to customize it to your needs (such as changing the package name), but it does not offer the selection of dependencies that you get with Initializr.

The approach of boot new is to clone an existing repository that contains fully working code and configuration in addition to your project’s dependencies. This approach makes it easy for your or members of your team to create several project archetypes, as they are regular Spring projects in a source code repository that can be verified to work with a CI process. See Creating New Projects for more information.

Using Boot Add

Then, after initial project creation, you can add additional code and dependencies as needed by using the boot add command. This command intelligently merges an existing project into your current project. As with the boot new command, the existing project is a regular Spring project in a source code repository that can be compiled and verified to work with a CI process. See Guide to "boot add" for more information.

Templating Code and Configuration

Alternatively, you can add additional code and dependencies by using user defined commands, which rely on using a template engine to create code and configuration. See Guide to user defined commands for more information. The approach to using a template engine and making it easy for users to contribute commands that invoke those template was inspired by the hygen project.

Conclusion

While both approaches to adding additional code and dependencies have their use cases, when creating a new project from scratch, we have found it easier to start from working projects. Not only do you get the approved dependencies that your company requires, but you also get code, configuration, and tests. It is a starting point that removes the busy work and uncertainty of selecting dependencies and copying code from a tutorial. Often, those tutorials are so basic that they do not follow common best practice conventions and have no tests.

Creating a new project by using templating can be a challenge. In our opinion, templating Java code for a new project is more difficult than cloning a new project from an existing working project. The cycle of creating a template, running the template, and then compiling the resulting generated project is brittle. However, we have found that creating smaller blocks of code and configuration are more amenable to the templating approach — for example, using spring controller new to add a controller to a current project or using spring feature new that adds a few files, such as classes annotated with @Controller, @Service, and @Repository.

Your experience may differ, but both types of code generation are available in the Spring CLI.