Springboot搭建多模块项目,拆分应用

源码:https://github.com/ATSJP/Study-Springboot-Demo.git

工具:

  • idea

首先,我们要搭建的一个项目有以下几个模块

  • demo-apply   应用模块
  • demo-common 公用工具类
  • demo-static    静态资源

好,既然知道了我们想要做的事情,那么对于这些模块,我们需要统一的parent项目,统一管理他们,下面就来新建一个demo-parent项目
在这里插入图片描述
在这里插入图片描述
下面一路next,直接默认就行,关于导入其他依赖包,我们手动导入,最后项目结构如下:
在这里插入图片描述
因为我们只需要demo-parent作为一个管理项目,所以不需要src,直接删除即可,同时修改对应的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>

	<groupId>com.demo</groupId>
	<artifactId>demo-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging><!-- 注意:修改jar为pom -->

	<name>demo-parent</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.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>
	</properties>

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

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

	<build>
		<plugins>
			<!-- 注意: 因为我们不需要demo-parent是一个可运行的项目,所以修改原来的打包方式为maven打包方式 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

好叻,demo-parent建立完毕,下面在此项目下新建我们需要的模块,
在这里插入图片描述这里注意一下,如果想要建立对应项目下的module项目,那么鼠标先选中该项目名称(对照上图)在选择File>>new>>Module,因为所有的new都是相对你选中的目录来的。

接下来一路操作,搞定三个模块,如下图:
在这里插入图片描述

接下来,首先建立相互关系,demo-parent中配置子模块:

<?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.demo</groupId>
	<artifactId>demo-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>demo-parent</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<!-- 注意:加入子模块 -->
	<modules>
		<module>demo-apply</module>
		<module>demo-common</module>
		<module>demo-static</module>
	</modules>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
	</properties>

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

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven.compiler.plugin.version}</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

分别修改3个子模块,共同依赖demo-parent:

demo-apply

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

    <!-- maven可以继承父项目中的参数,所以可以删除子模块中定义 -->
    <!--<groupId>com.demo</groupId>-->
    <artifactId>demo-apply</artifactId>
    <!-- maven可以继承父项目中的参数,所以可以删除子模块中定义 -->
    <!--<version>0.0.1-SNAPSHOT</version>-->
    <packaging>jar</packaging>

    <name>demo-apply</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.demo</groupId>
        <artifactId>demo-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <!-- maven可以继承父项目中的参数,所以可以删除子模块中定义 -->
    <!--<properties>-->
        <!--<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>-->
        <!--<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>-->
        <!--<java.version>1.8</java.version>-->
    <!--</properties>-->

    <dependencies>
        <!-- maven可以继承父项目中的依赖,所以可以删除子模块中定义 -->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter</artifactId>-->
        <!--</dependency>-->

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

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


</project>

demo-common:

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

	<artifactId>demo-common</artifactId>
	<packaging>jar</packaging>

	<name>demo-common</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.demo</groupId>
		<artifactId>demo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
	</parent>

	<dependencies>

	</dependencies>

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


</project>

demo-static:

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

	<artifactId>demo-static</artifactId>
	<packaging>jar</packaging>

	<name>demo-static</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.demo</groupId>
		<artifactId>demo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
	</parent>

	<dependencies>
		
	</dependencies>

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


</project>

到这里为止,demo-parent和3个子模块的相互关系已经建立成功。


接下来,我们需要搞定楚3个模块之间的关系

模块打包方式依赖模块是否可执行功能
demo-applyjardemo-common/demo-static核心应用
demo-staticwar静态文件,如页面
demo-commonjar共用工具包

好,既然知道了模块的关系,接下里分别修改3个子模块

demo-apply:

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

    <artifactId>demo-apply</artifactId>
    <packaging>jar</packaging>

    <name>demo-apply</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.demo</groupId>
        <artifactId>demo-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!-- 加入其余模块的依赖 -->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>demo-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>demo-static</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 因为static是war打包方式 所以必须声明,不然默认为jar包方式 -->
             <type>war</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 当所依赖的其他模块,有启动类的时候,需要以下配置,指定唯一启动类,
                        本次事例未涉及其他模块有启动类,可自行测试 -->
                <configuration>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>com.demo.demoapply.DemoApplyApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

demo-common

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

	<artifactId>demo-common</artifactId>
	<packaging>jar</packaging>

	<name>demo-common</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.demo</groupId>
		<artifactId>demo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
	</parent>

	<dependencies>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

同时由于不需要可运行,所以删除demo-common下的启动类和properties文件,对应test文件下默认生成的测试类,也要删除。

demo-static:

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

	<artifactId>demo-static</artifactId>
	<!-- 修改打包方式 -->
	<packaging>war</packaging>

	<name>demo-static</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.demo</groupId>
		<artifactId>demo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
	</parent>

	<dependencies>

	</dependencies>

	<build>
		<plugins>
		    <!-- 修改springboot的编译插件为maven编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

另外,由于打包成war包方式,所以必须在对应src/main/webapp/WEB-INF/下存在web.xml文件,同时由于不需要可运行,所以删除启动类和properties文件,对应test文件下默认生成的测试类,也要删除。
在这里插入图片描述

为了检验我们项目是否搭建正确,我们在demo-parent的pom中加入web依赖,这样子项目可以直接继承:

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

同时在demo-apply的启动类中加入以下代码:

package com.demo.demoapply;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplyApplication {

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

    @RequestMapping("hello")
    public String hello() {
        return "hello";
    }
}

然后直接启动该类,浏览器中访问http://localhost:8080/hello,页面如下,大功告成。
在这里插入图片描述

下面我们来验证demo-parent部署正确
在这里插入图片描述
大功告成。

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值