1. We can use Spring Initializr to create SpringBoot projects.
Spring Initializr is essentially a web application that can generate Spring Boot project structure for you. Although application code cannot be generated, it can provide you with a basic project structure and a Maven or Gradle build documentation for building code. You just need to write the code of the application.
There are several uses of Spring Initializr.
1. Use through the web interface.
2. Use through Spring Tool Suite.
3. Used through IntelliJ IDEA.
4. Use Spring Boot CLI.
5. Supplement: Simply use gradle or maven to build.
1. Use Spring Initializr's web interface
To use Spring Initializr, the most direct way is to open http://start.spring.io with your browser. You should see a form similar to the picture below:
Here you can choose to use Maven or Gradle to build the project, whether it is based on Java or Groovy, and Spring Boot. Below we can set the project's group and Artifact. On the right, we can add the required dependencies. Enter the dependency name we need and match it. If the dependency we need is not found, you can click "Switch to the full version" below to see the complete list of available dependencies. This information is used to generate Maven's pom.xml file (or Gradle's build.gradle file).
After filling in the form, select the dependency, click the "Generate Project" button, and Spring Initializr will generate a project for you. The browser will download this project in the form of a ZIP file (the file name depends on the content of the Artifact field). Depending on your choice, the content of the ZIP file will also be slightly different. Either way, the ZIP file contains an extremely basic project that allows you to start developing applications using Spring Boot.
After downloading, we just need to decompress and import it into the IDE~
2. Create Spring Boot Project in Spring Tool Suite
Spring Tool Suite ① has long been the best choice for developing Spring applications. Since version 3.4.0, it has integrated Spring Initializr, which makes it a great way to get started with Spring Boot. To create a new Spring Boot application in Spring Tool Suite, select the New > Spring Starter Project menu item in the File menu, and then the Spring Tool Suite will display a dialog box similar to the image below.
As you can see, the information required to be filled in this dialog box is the same as in the Spring Initializr's web interface. In fact, the data you provide here will be sent to Spring Initializr for creating project ZIP files, which is the same as using web forms.
If you want to specify the location where the project is created on the file system, or add it to a specific working set in the IDE, click the Next button. You will see the second dialog box, as shown below:
Location specifies the storage location of the project on the file system. If you use Eclipse's work sets to organize your project, you can also check the Add Project to Working Sets check box and select a working set so that the project can be added to the specified working set.
The Site Info section briefly describes the URL you will use to access Initializr, which you can ignore in most cases. However, if you want to deploy your own Initializr server (copy the code from https://github.com/spring-io/initializr), you can set the Initializr base URL here.
After clicking the Finish button, the project generation and import process begins. You must realize that the Spring Starter Project dialog box of Spring Tool Suite actually delegates the project generation work to Spring Initializr on http://start.spring.io, so you must be connected to the Internet to use this feature.
Once the project is imported into the workspace, the application is ready to be developed. During the development process, you will find that Spring Tool Suite has some icing on the cake for Spring Boot. For example, you can select Run As > Spring Boot Application in the Run menu to run your application in an embedded server.
Note that Spring Tool Suite interacts with Initializr through the REST API, so it can work properly only if you connect to Initializr. If your development machine is offline, or Initializr is blocked by a firewall, the Spring Starter Project wizard of Spring Tool Suite is not available.
3. Create Spring Boot Project in IntelliJ IDEA
IntelliJ IDEA is a very popular IDE, and IntelliJ IDEA 14.1 has begun to support Spring Boot.
To create a new Spring Boot application in IntelliJ IDEA, select New > Project in the File menu. You will see several screens of content (as shown below), and the questions asked are similar to Initializr's web application and Spring Tool Suite.
The second screen of the Spring Boot Initialization Wizard requires you to provide some basic information about the project, such as the project name, Maven Group and Artifact, Java version, and whether you want to build a project with Maven or Gradle. After describing the project information, click the Next button to see the third screen, as shown in the figure below:
The second screen asks you about the basic information about the project, and the third screen starts asking you what dependencies you want to add to the project. As before, the checkboxes on the screen correspond to Spring Boot startup dependencies. After selecting, click Next and you will reach the last screen of the wizard:
The last screen asks you what the project is called and where to create it. After everything is ready, click the Finish button and you will get an empty Spring Boot project in the IDE.
4. Use Initializr in Spring Boot CLI
If you want to complete Spring application development just by writing code, Spring Boot CLI is a good choice. However, the functionality of Spring Boot CLI is not limited to this. It has some commands to help you use Initializr and use it to develop more traditional Java projects.
The Spring Boot CLI includes an init command that can be used as the client interface of Initializr.
The easiest way to use the init command is to create a baseline for Spring Boot project:
$ spring init
After communicating with Initializr's web application, the init command will download a demo.zip file. After decompression, you will see a typical project structure containing a Maven pom.xml build description file. Maven's build instructions only include the most basic content, i.e. only Spring Boot baseline and test start dependencies. You may want something more.
Suppose you want to build a web application where data persistence is used using JPA and Spring Security for security hardening. You can use -- dependencies or -d to specify those initial dependencies:
$ spring init -dweb,jpa,security
This command will download a demo.zip file, which contains the same project structure as before, but adds Spring Boot's web, jpa and security startup dependencies in pom.xml. Please note that spaces cannot be added between -d and dependencies, otherwise it will become a ZIP file with the file name web, jpa, security.
Now, let's say you want to build your project with Gradle. No problem, use the --build parameter to specify Gradle as the build type:
$ spring init -dweb,jpa,security --build gradle
By default, build instructions for both Maven and Gradle will produce an executable JAR file. But if you want a WAR file, you can use the --packaging or -p parameter to explain:
$ spring init -dweb,jpa,security --build gradle -p war
So far, the init command is only used to download ZIP files. If you want the CLI to help you decompress that ZIP file, you can specify a directory for decompression:
$ spring init -dweb,jpa,security --build gradle -p war myapp
The last parameter here shows that you want to unzip the project into the myapp directory.
In addition, if you want the CLI to extract the generated project to the current directory, you can use the --extract or -x parameter:
$ spring init -dweb,jpa,security --build gradle -p jar -x
There are many other parameters init commands, including parameters for building projects based on Groovy, parameters for specifying compiled with Java version, and parameters for selecting Spring Boot versions that are dependent on. You can understand all parameters through the help command:
$ spring help init
You can also check which options are available for those parameters, and just bring --list or -l parameters to the init command:
$ spring init -l
You must have noticed that although spring init l lists some parameters supported by Initializr, not all parameters can be directly supported by the Spring Boot CLI init command. For example, when initializing a project with the CLI, you cannot specify the name of the root package, it defaults to demo. spring help init will tell you which parameters the CLI init command supports.
5. Supplement: Simply use gradle (or maven) to build Spring Boot projects
This method is suitable for the case where an abnormal error is reported when connecting to https://start.spring.io (changed to http protocol) when building a Spring Boot project using Spring Initializr.
For example:
Here is an example of using IDEA to create a Gradle project. First we create a Gradle project: File->New->Project->Gradle
We click Next, then fill in GroupId, ArtifactId and Version number, click Next,
Note here:
1. It is not recommended to check Use auto-import;
2. Because the default download of gradle from the Internet is very slow, it is recommended to check it here: Use local gradle distribution, fill in the local gradle home
After that, fill in the project name and path in NewProject and click Finish.
Here we have generated a Gradle project, which is not a Spring Boot project, so we need to write down some basic configurations of Spring Boot. Here I will post my gradle.build file to you:
buildscript { repositories { maven { url = 'http://maven.aliyun.com/nexus/content/groups/public/' } } dependencies { classpath('org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE') }}apply plugin: 'java'apply plugin: 'idea'apply plugin: 'org.springframework.boot'apply plugin: 'io.spring.dependency-management' group = 'com.springboot' version = '1.0.0.0'sourceCompatibility = 1.8targetCompatibility = 1.8repositories { maven { url = 'http://maven.aliyun.com/nexus/content/groups/public/' }}dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test')}(The Alibaba Cloud image used here is recommended to use. The download dependency speed is very fast, and the original configuration speed is too slow)
Then select Gradle in the Tool Buttons column on the right:
(If not, you can select Tool Buttons in the View to expand)
Then click the refresh refresh button.
After that, we will generate the directory structure of the Spring Boot project:
src |-- main |-- java |--com.springboot |---- SpringBootDemoApplication |----resources |----test
Finish.
2. Summary
Whether you use the Initializr's web interface, create a project in Spring Tool Suite, or use the Spring Boot CLI to initialize a project, the projects created by Spring Boot Initializr have similar project layouts, which is no different from the Java projects you have developed before.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.