美文网首页SpringHomespringbootJ2EE学习--SpringBoot
Springboot多模块开发一:项目搭建

Springboot多模块开发一:项目搭建

作者: 雪飘千里 | 来源:发表于2018-09-19 21:30 被阅读67次

1、统一父pom.xml管理,建立boot-parent工程


image.png
image.png
image.png

2、修改pom.xml文件,引入父pom springboot-boot-parent,删除不需要的src文件夹

<?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.example.boot</groupId>
    <artifactId>boot-parent</artifactId>
    <packaging>pom</packaging>
    <version>${global.appVersion}</version>

    <!--子模块-->
    <modules>
        <module>boot-web</module>
        <module>boot-service</module>
    </modules>


    <properties>
        <!--统一所有的版本号-->
        <global.appVersion>1.0.1-SNAPSHOT</global.appVersion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!--引入父pom-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-parent</artifactId>
                <version>1.5.10.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  <build>
        <plugins>
            <plugin>
<--子模块继承所用来打包的-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、新增子模块 boot-web


image.png
image.png
<?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">
    <parent>
        <artifactId>boot-parent</artifactId>
        <groupId>com.example.boot</groupId>
        <version>${global.appVersion}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>boot-web</artifactId>
    <version>${global.appVersion}</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.example.boot</groupId>
            <artifactId>boot-service</artifactId>
            <version>${global.appVersion}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--springboot主类-->
                    <mainClass>
                        com.springboot.demo.SpringbootApplication
                    </mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
package com.springboot.demo.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;


@SpringBootApplication
public class SpringbootApplication {

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

application.properties文件

server.port=8082

3、再建字模块 boot-service

<?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">
    <parent>
        <artifactId>boot-parent</artifactId>
        <groupId>com.example.boot</groupId>
        <!--父模块的版本号-->
        <version>${global.appVersion}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <!--当前模块的版本号-->
    <version>${global.appVersion}</version>
    <artifactId>boot-service</artifactId>
    <packaging>jar</packaging>

    <dependencies>
       <dependency>
            <groupId>com.bangmart.monitoring</groupId>
            <artifactId>bangmart-monitoring-dao</artifactId>
        </dependency>
    </dependencies>

</project>
package com.springboot.demo.service;

import org.springframework.stereotype.Service;

/**
 * Created by zhangkai on 2018/9/19.
 */
@Service
public class HelloService {

    public String SayHello(){
        return "hello world!!!";
    }
}

包结构图如下:


注意事项:
1、JAR包中mainClass是在web pom.xml文件中确定的,父pom.xml文件中的build的是给其他子模块继承用的;
2、springboot默认扫描包的路径,是springbootApplication启动类的位置的上级目录,所以多模块时,所有模块的上级目录都要和srpingbootApplication所在模块的上级目录相同

相关文章

网友评论

  • 王小凡wxf:要是引入数据库呀什么的都是写在父pom里面吗?

本文标题:Springboot多模块开发一:项目搭建

本文链接:https://www.haomeiwen.com/subject/ggminftx.html