如何使SpringBoot作为Maven构建的项目的一个子模块

1.问题

一般使用springboot都会引用springboot作为parent,在实际项目中web只是系统模块的一个子集。当然你可以做两个项目来管理,一个项目用来做各种支持包,一个项目专门做web,但是这样不仅调试不方便而且将面临更多的隐患,比如发布前的版本控制问题等。
所以最优方案已定是将Springboot作为一个Maven项目的子模块来处理。

2.先改一下我们项目的pom.xml

也就是在我们项目的pom中,也就是其他子项目的parent中增加全部的sprintboot的引用。注意一下这段是整段新增的。

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.0.1.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

3.在web子模块中增加springboot引用

在我们子模块的pom中像使用其他包一样加入springboot,注意dependencies可能是原有的,你只要增加dependency部分即可。
如果是一个空项目可以像下面这样引用。

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

4.需要一个server来启动web

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class clientServer {
    public static void main(String[] args) {
        SpringApplication.run(clientServer.class, args);
    }
}

5.之后就正常愉快的使用内置的tomcat和地址路由吧

猜你喜欢

转载自www.cnblogs.com/pcode/p/9202036.html