Create springBoot project using maven command line

Create project

1. First use the mvn command to create a basic Java project structure

// 创建MyProject目录,用于存放java项目
~/Desktop$ mkdir MyProject 

// 使用mvn命令创建java项目结构
~/Desktop/MyProject$ mvn archetype:generate -DgroupId=com.wong -DartifactId=springboot -Dversion=1.0.0 -DinteractiveMode=false

// 创建好的java项目,因为我们指定了-DartifactId=springboot,所以我们的项目名就是springboot
~/Desktop/MyProject$ tree -L 1
.
└── springboot

Explain the command to create a java project structure:

  • mvn : maven command
  • archetype: generate : This is a Maven plugin. The prototype archetype plugin is a Maven project template toolkit. You can use it to create a basic java project structure.
  • -DgourpId : Organization name, company website URL + project name
  • -DartifactId : project name (module name)
  • -Dversion : Project version number
  • -DinteractiveMode : Whether to use interactive mode: false is not used, directly created; true to use, you need to enter relevant information according to the prompt.

Warm tips:

You can use the -DarchetypeArtifactIdspecified ArchetypeId to quickly create a Java application, such as:

(1) Create a Java application:
mvn archetype:generate -DgroupId=com.wong.company -DartifactId=shopingmall -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

(2) Create a Web Project:
mvn archetype:generate -DgroupId=com.wong.company -DartifactId=shopingmall -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2. Add in the pom.xml file

<project...>
  ...
  <!--spring-boot-starter-parent是spring boot 的父级依赖,它是一个特殊的starter>,用以提供相关的maven默认依赖-->
  <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.2.5.RELEASE</version>
  </parent>
  <dependencies>
    <!--Spring Boot WEB依赖-->
    <!--项目导入web的启动依赖,SpringBoot就会集成SpringMVC,就可以进行Controller的开发-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    ...
  </dependencies>
</project>

Tips

(1) spring-boot-starter-parent is the parent dependency of spring boot, it is a special starter to provide related Maven default dependencies. About which jar packages the spring boot provides, you can ~ / .m2 / repository / org / springframework / boot / spring-boot-dependencies / 2.2.5.RELEASE / spring-boot-dependencies-2.2.5.RELEASE.pom file. By the way, under linux, each user has a repository directory with a path name of .m2 / respository / in their own user directory.

If you do not want to use a version that depends on the default in spring-boot-dependencies-2.2.5.RELEASE.pom, you can re-specify the version in the pom.xml of this project to override the default, such as the following.
The default version of Spring Data in spring-boot-dependencies-2.2.5.RELEASE.pom is:

    <properties>
	  ...
      <spring-data-releasetrain.version>Moore-SR5</spring-data-releasetrain.version>
	  ...
	<properties>
    <dependencies>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-releasetrain</artifactId>
        <version>${spring-data-releasetrain.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
     </dependencies>

Then we can re-specify the version in pom.xml in this project:

    <properties>  
        <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>  
    </properties>  

(2) If you do not want to inherit the POM from spring-boot-starter-parent, that is, <parent> specifies that it is not spring-boot-starter-parent, but other. However, we can still maintain the dependency management with spring-boot-starter-parent by using scope = import dependency, just add in the pom.xml of our project:

    <dependencyManagement>  
         <dependencies>  
            <dependency>  
                <!-- Import dependency management from Spring Boot -->  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-dependencies</artifactId>  
                <version>2.2.5.RELEASE</version>  
                <type>pom</type>  
                <scope>import</scope>  
            </dependency>  
        </dependencies>  
    </dependencyManagement>  

Above, we modified the property spring-data-releasetrain.version to override the default version, but sometimes there is a problem with this property modification method. That is, there are many dependencies to use this property, and we will change it all when we change it. But we don't want other dependent versions, so what should we do? In fact, it is very simple, that is, do not modify the attributes and redefine the dependencies directly. But you need to add a configuration to the dependencyManagement of the project before the spring-boot-dependencies item, such as to upgrade to another Spring Data version:

    <dependencyManagement>  
        <dependencies>  
            <!-- Override Spring Data release train provided by Spring Boot -->  
            <dependency>  
                <groupId>org.springframework.data</groupId>  
                <artifactId>spring-data-releasetrain</artifactId>  
                <version>Fowler-SR2</version>  
                <scope>import</scope>  
                <type>pom</type>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-dependencies</artifactId>  
                <version>2.2.5.RELEASE</version>  
                <type>pom</type>  
                <scope>import</scope>  
            </dependency>  
        </dependencies>  
    </dependencyManagement>  

(3) Starting to rely on spring-boot-starter-xx
Spring Boot provides many "out -of-the- box" dependency modules, all named after spring-boot-starter-xx. The start dependency is the dependency that automatically encapsulates the function you want to achieve . For example, if we want to implement web functions before, we need to introduce spring-boot-starter-web as a starting dependency.
Spring Boot reduces the complexity of project dependencies by providing many starting dependencies. The starting dependency is essentially a Maven Project Object Model (POM), which defines the transitive dependencies on other libraries. These things add up to support a certain function. Many of the naming dependencies at the beginning implied certain functions or types of functions they provide.

SpringBoot Maven plugin

This is in spring-boot-dependencies-2.2.5.RELEASE.pom, the pom inherited from the project pom.xml:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>2.2.5.RELEASE</version>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
  </build>

This plugin is too important, it provides a lot of functions:

  • Package the project into an executable super JAR (uber-JAR), including typing all the dependencies of the application into the JAR file, and adding a description file to the JAR, the content of which allows you to use java -jar to run the application program.
  • "Important" search the public static void main () method to mark it as a runnable class .

For more SpringBoot Maven plugins, please refer to "SpringBoot Maven Plugin"

Preparation of application entry class

package com.wong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {
         SpringApplication.run(App.class,args);
    }
}

  • @SpringBootApplication: is the core annotation of the Sprnig Boot project, the main purpose is to enable automatic configuration. @SpringBootApplication = (default property) @Configuration + @EnableAutoConfiguration + @ComponentScan
  • Main method: This is the main method of a standard Java application. Its main function is to serve as an entry point for project startup.

Write Controller

It must be written in the same package as the App startup class to be scanned.

package com.wong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class First{
	@GetMapping("hello")
	public String index(){
		return "Hello World!";
	}
}

The combination of @ RestController = @ Controller + @ ResponseBody, the methods in the class using this annotation are output in json format.

Start the springboot project

Three ways to start the project

  • App main method
  • Use the command: mvn spring-boot: run to start
  • Use the command: mvn package to package, the generated JAR file, use the java -jar command to run
~/Desktop/MyProject/springboot$ mvn spring-boot:run

Insert picture description here
2020-03-22 02:23:31.204 INFO 10993 --- [ main] com.wong.App : Started App in 1.384 seconds (JVM running for 1.731)The appearance of this sentence indicates successful startup!

test

Insert picture description here
thanks for reading!

Published 381 original articles · praised 85 · 80,000 views +

Guess you like

Origin blog.csdn.net/weixin_40763897/article/details/105020990