idea中Springboot项目如何做成父子结构

什么是父子结构呢,父子结构就是maven里面的同时有一个父项目和多个子项目的项目结构,并且子项目可以使用父项目的pom配置以及依赖的版本号等等。

首先从父结构开始,

090721_6KtI_3768341.png

先新建一个项目,选择Maven,而不是Spring Initializr,这个很容易弄错。Create from archetype那里不要打勾。

next,next之后,在pom.xml中加入

<packaging>pom</packaging>

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

    <groupId>com.barsliver</groupId>
    <artifactId>nightsliver</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

</project>

然后在父项目的文件夹中新建一个子项目的文件夹,比如叫register-center,删除src文件夹

091814_tYX8_3768341.png

右键选中项目名称,New->Module

091945_Lwqa_3768341.png

这个时候再选择Spring Initializr

092151_9iMP_3768341.png

然后可以开始愉快的开始你的Springboot配置了,记得注意你的springboot版本号,选择适合你的版本号

092343_EUVg_3768341.png

next之后,Content root要选择你刚才新建的文件夹,此处不要弄错

092548_wTwG_3768341.png

在子项目的pom中把

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>

剪切粘贴到父项目的pom去,在子项目中加上

<parent>
   <groupId>com.barsliver</groupId>
   <artifactId>nightsliver</artifactId>
   <version>1.0-SNAPSHOT</version>
</parent>

把各种可能的各个子项目都会用到的依赖剪切到父项目中,如

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

保留只在子项目中会用到的依赖,如

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

把子项目中的依赖管理剪切到父项目中

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在父项目的pom中添加module模块,把子项目的项目名写进去

<modules>
   <module>register-center</module>
</modules>

这样一个父子结构的原型就差不多完成了,再建新的子项目于此类同。

转载于:https://my.oschina.net/u/3768341/blog/1819723

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值