Build a Spring Boot project with maven

1. Using Spring Boot

        Typically, the Maven POM file in your project extends the spring-boot-starter-parent module, by declaring one or more Starter POMs dependencies. Spring Boot also provides an optional Maven Plugin to create executable jars. The specific code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <groupId>com.springboot</groupId>
    <artifactId>infrastructure</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!-- spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- spring boot -->
        

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

        It should be noted that inheriting the spring-boot-starter-parent module is a very good way to use it, but it is not suitable for all cases. This is when you need to inherit from other POMs, or when you don't like the default settings.

        I am using the latest version 2.0 above, which requires a minimum JDK version of 1.8 (if the JDK is a lower version, you can lower the Spring Boot version yourself), and the Maven version is 3.2 or higher.

2. Create a Spring Boot project

        Creation tools: maven, jdk

2.1. Creation steps:

        New project folder: infrastructure;

        Create a new pom.xml file.

 The contents of the pom file are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <groupId>com.springboot</groupId>
    <artifactId>infrastructure</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

        There is no dependency added here, only the spring-boot-starter-parent module is inherited, and there are no other code files, etc., but it is already a Spring Boot project, you can build and view the results through the maven command.

        Open the dos window and enter the project path:

        

        Command line: mvn package

        

        At this point, basically a Spring Boot project has been created. The next thing to do is to add dependency packages to the project, create the project file structure according to the default path, and finally write the code.

2.2, add dependencies

        Since we have inherited spring-boot-starter-parent, and spring-boot-starter-parent provides dependency-management, we can ignore the version of the selected dependency.

        Before adding dependencies, let's take a look at what we have now: mvn dependency:tree. This command prints a dependency tree of the current project.

        The results show that there are currently no dependencies. 

        

        Now we add the following dependencies to the project:

        <!-- spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- spring boot -->

        Looking at the dependency tree again, there are so many:

        

        It can be seen that spring-boot-starter-web contains a lot of content, spring-webmvc, spring-web, jackson, validation, tomcat, starter.

2.3, code and start, package the project

        Maven's default compilation path is the source code under src/main/java, so these folders need to be created by default.

        Then, write the file src/main/java/SampleController.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!============";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

        Here, we only need to care about the @EnableAutoConfiguration annotation, which provides a series of default automatic configurations, following our usual conventions. If your habits are consistent with its default configuration, then you hardly need any additional configuration, of course, manual configuration is always unavoidable. This annotation contains the following annotations, which are not discussed in depth here:

        

        Run the maven command: mvn spring-boot:run

        

        I got the above error at the beginning, guess: I added the JDBC module to the dependency, and it automatically configures the data source with an error; the solution:

        1. The dependency in the red box in the following figure should be removed:

        

        2. By declaring, remove the datasource:

        

It can be accessed after startup, the default address:  http://127.0.0.1:8080

        Package the project: use the maven command to package. And you need to use the spring-boot-maven-plugin provided by Spring Boot:  

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

        Package command: mvn package

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325156202&siteId=291194637