您的位置:首页 > 编程语言 > Java开发

使用maven构建一个Spring Boot项目

2018-03-14 13:37 906 查看

1、使用Spring Boot

通常,让你的项目中的Maven POM文件继承 spring-boot-starter-parent 模块即可,具体使用通过声明一个或多个 Starter POMs 依赖。Spring Boot也提供了一个可选的 Maven Plugin 来创建可执行的jars。具体代码如下:

<?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>

需要注意的是,继承 spring-boot-starter-parent 模块是一个非常好的使用方式,但并不适用于所有情况。这时你需要继承其他的POM,或者你不喜欢默认的设置时。

我在上面使用的是最新的2.0版本,它要求JDK版本最低为1.8(如果JDK是更低的版本,自行降低Spring Boot版本即可),Maven版本为3.2以上。

2、创建Spring Boot项目

创建工具:maven、jdk

2.1、创建步骤:

新建项目文件夹:infrastructure;

新建一个pom.xml文件。

pom文件内容如下:

<?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>

这里面没有添加任何依赖,只继承了 spring-boot-starter-parent 模块,并且没有其他的代码文件等,但是它已经是一个Spring Boot项目,可以通过maven命令build并查看结果。

打开dos窗口,进入到项目路径:



命令行:mvn package



到这里,基本上已经算是创建了一个Spring Boot项目了。接下来需要做的是在项目中添加依赖包,并根据默认路径创建项目文件架构,最后才是写代码。

2.2、添加依赖

由于我们已经继承了 spring-boot-starter-parent ,而 spring-boot-starter-parent 又提供了 dependency-management ,所以我们可以忽略被选中依赖的版本。

在添加依赖之前,我们先看一下现在已有什么:mvn dependency:tree。该命令会打印一个当前项目的依赖树。

结果表明,当前没有任何依赖。



现在我们给项目添加以下依赖:

<!-- 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 -->

再次查看一下依赖树,发现有了这么多:



可以看出,spring-boot-starter-web 包含了很多内容,spring-webmvc、spring-web、jackson、validation、tomcat、starter。

2.3、编码并启动、打包项目

Maven默认编译路径为 src/main/java 下面的源码,所以,默认设置下,需要创建这些文件夹。

然后,编写文件 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);
}
}

在这里,我们只需要关心 @EnableAutoConfiguration 这个注解,它提供了一系列的默认自动配置,遵循我们平常的习惯约定。如果你的习惯跟它默认的配置一致,那么你几乎不需要任何的额外配置,当然,手动配置的情况总是不可避免的。这个注解包含了以下注解,这里不作深入讨论:



运行maven命令:mvn spring-boot:run



一开始我出现了以上错误,猜测:我在依赖中添加了JDBC模块,它自动配置数据源时出现错误;解决方法:

1、应该把下图红框中那个依赖去掉:



2、通过声明,移除datasource:



启动之后就可以访问了,默认地址: http://127.0.0.1:8080

打包项目:使用maven命令打包。并且需要使用Spring Boot提供的 spring-boot-maven-plugin:

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

打包命令:mvn package
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: