springboot父子项目

springboot父子项目

父子项目即在父级项目中包含多个子级项目, 子级项目可以看模块划分, 例如web层, 服务层等

注意点:

  1. 子项目可以继承父项目的依赖, 前提是需要使用parent标签指定父项目
  2. 子项目可以单独引入依赖, 依赖只对当前子项目有效
  3. 子项目中没有页面打包方式可以为jar, 有页面打包使用war

搭建父子项目案例

项目结构如下:

使用idea搭建父子项目

1. 创建父级项目jt

使用idea创建一个springboot项目, 打包方式为pom方式

<packing>pom</packing>

切记把父项目中的build标签删除, 因为父级项目不需要打包

父级项目导入的maven依赖可以被子类继承

2. 创建子maven项目jt-common

在父项目jt上右键 new model, 选择maven项目即可

common一般可以被公共模块调用, 即通用模块, 打包方式为jar包

<packing>jar</packing>

公共API项目也不需要build标签

但是, 如果子项目要继承父项目的jar包, 需要使用parent标签, 指定父级项目

<parent>
    <groupId>com.jt</groupId>
    <artifactId>jt</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

创建完毕有查看父级项目中有没有自动生成modules标签

3. 创建子模块jt-manager

在父项目jt上右键 new model, 选择maven项目即可

也是创建maven项目, 打包方式为war

<packing>war</packing>

此项目需要使用build标签, 否则项目可能无法运行

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

此项目也需要继承父级项目的jar包, 需要使用parent标签

<parent>
    <groupId>com.jt</groupId>
    <artifactId>jt</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

导入公共API, (jt-common)

<dependencies>
    <dependency>
        <groupId>com.jt</groupId>
        <artifactId>jt-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- 等其他依赖 -->
</dependencies>

然后创建springboot的启动类即可

创建完毕有查看父级项目中有没有自动生成modules标签

4. 创建web模块jt-web等...

在父项目jt上右键 new model, 选择maven项目即可

此项目依然为maven项目, 创建方式可参照第三步

如归父级项目中有数据源的依赖, 而此模块不需要直接访问数据库, 即不需要数据源的配置, 可以在主启动类@SpringBootApplication注解中加入排除配置类的属性, 例如

@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)

可以创建多个子模块的项目... 参照第三步即可

5. 检查父级项目

父级项目中如果生成如下内容表示成功, 没有则手动敲上去

<modules>
    <module>jt-common</module>
    <module>jt-manager</module>
    <module>jt-web</module>
</modules>

完整文件内容如下

jt/pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jt</groupId>
	<artifactId>jt</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

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

	<properties>
		<java.version>8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
		<skipTests>true</skipTests>
	</properties>

	<dependencies>
		<!-- springboot中导入的maven依赖, 此处略 -->
	</dependencies>
	
	<!-- 切记不要加build标签 -->

	<modules>
		<module>jt-common</module>
		<module>jt-manager</module>
        <module>jt-web</module>
    </modules>
</project>

jt-common/pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.jt</groupId>
    <artifactId>jt</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>jt-common</artifactId>
</project>

jt-manager/pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>jt-manager</artifactId>
	<packaging>war</packaging>

	<parent>
		<groupId>com.jt</groupId>
		<artifactId>jt</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
        <!-- 导入公共API- jt-common -->
        <dependency>
            <groupId>com.jt</groupId>
            <artifactId>jt-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
		<!-- 可以导入此项目专有的依赖, 在其他项目不生效 -->
	</dependencies>

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

jt-web/pom.xml

<?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>jt</artifactId>
        <groupId>com.jt</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jt-web</artifactId>
    <!--指定打包类型-->
    <packaging>war</packaging>

    <!--添加依赖信息-->
    <dependencies>
         <!-- 导入公共API- jt-common -->
        <dependency>
            <groupId>com.jt</groupId>
            <artifactId>jt-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <!-- 可以导入此项目专有的依赖, 在其他项目不生效 -->
    </dependencies>

    <!--添加插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
原文地址:https://www.cnblogs.com/zpKang/p/13519825.html