当前位置: 首页 > news >正文

创建多模块教程

一、创建一个父模块

不需要添加什么springweb,创建好之后是这样子的,有这些文件

在这里插入图片描述

删掉 .mvn,src,HELP.md,mvnw,mvnw.cmd

在这里插入图片描述

最原始的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>owntestdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>owntestdemo</name>
    <description>owntestdemo</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</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>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

打开pom.xml,需要删掉<build>标签下面的

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

删掉<parent>标签

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

删掉<name>和<description>标签

    <name>owntestdemo</name>
    <description>owntestdemo</description>

删除<dependencies>标签

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

标签<dependencyManagement>来管理依赖

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

在<properties>中添加<spring-boot.version>

<spring-boot.version>3.4.3</spring-boot.version>

添加<packaging>,因为外层只是管理依赖的一个文件

<packaging>pom</packaging>

添加<build>标签

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugine</artifactId>
                    <version>${spring.boot.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

在<properties>里面添加<maven.compiler.source>、<maven.compiler.source>、<maven.compiler.source>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.source>17</maven.compiler.target>
        <maven.compiler.source>UTF-8</project.build.sourceEncoding>
    </properties>

最后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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>owntestdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>21</java.version>
        <spring-boot.version>3.4.3</spring-boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugine</artifactId>
                    <version>${spring.boot.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

二、创建子模块

右击owntestdemo项目文件夹 --> New --> Module

在这里插入图片描述

选择Spring Boot,创建module-common

在这里插入图片描述

点next,spring不用管,也不需要勾选任何springweb等

在这里插入图片描述

删掉 .mvn,.gitignore,HELP.md,mvnw,mvnw.cmd

删除后

在这里插入图片描述

module-common原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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>org.example</groupId>
	<artifactId>module-common</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>module-common</name>
	<description>module-common</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>21</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>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

修改<parent>标签中的<groupId>标签

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

把子类pom.xml中<parent>标签里面的数值改成父类<project>标签里面的数值

    <groupId>org.example</groupId>
    <artifactId>owntestdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

子类module-common的pom.xml修改后

	<parent>
		<groupId>org.example</groupId>
		<artifactId>owntestdemo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

删除module-common中pom.xml里的<name>标签和<description>标签

原:

	<groupId>org.example</groupId>
	<artifactId>module-common</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>module-common</name>
	<description>module-common</description>

改后:

	<groupId>org.example</groupId>
	<artifactId>module-common</artifactId>
	<version>0.0.1-SNAPSHOT</version>

删除<build>标签

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

删除<dependencies>标签里面的<dependency>标签

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

在父模块pom.xml中<project>标签下,也就是和<build>等标签同级,添加<modules>标签内容,这个就是模块管理子模块。

    <modules>
        <module>module-common</module>
    </modules>

子模块<dependencies>标签添加<dependency>标签,这个是springweb依赖

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

修改module-common中pom.xml的<parent>标签中的<relativePath>标签,如下

原:

<relativePath/>

改后:

    <parent>
        <groupId>org.example</groupId>
        <artifactId>owntestdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
    </parent>

修改<groupId>标签

原:

    <groupId>org.example</groupId>

改后:

    <groupId>org.example.common</groupId>
    <artifactId>module-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>

需要把父模块中的pom.xml中的<dependencyManagement>标签,添加<type>和<scope>标签。指定类型不然就会有问题。

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

加了<type>和<scope>标签子模块的pom.xml就会出现这个向上的箭头

在这里插入图片描述

如果不加<type>和<scope>标签子模块的pom.xml就不会出现这个向上的箭头,就会有问题。

在子模块pom.xml中添加 spring-boot-starter-test 依赖

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

最后

owntestdemo父模块的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>owntestdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>21</java.version>
        <spring-boot.version>3.4.3</spring-boot.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugine</artifactId>
                    <version>${spring.boot.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <modules>
        <module>module-common</module>
    </modules>

</project>

module-common 子模块的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>owntestdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example.common</groupId>
    <artifactId>module-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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


    </dependencies>


</project>

相关文章:

  • ARMv8.x-M架构计算能力概览
  • react路由5和6新增及区别
  • 【论文阅读】FairCLIP - 医疗视觉语言学习中的公平性提升
  • 基于香橙派 KunpengPro学习CANN(2)——Ascend Extension for PyTorch 配置与安装
  • 深度学习框架PyTorch——从入门到精通(5)自动微分
  • Web3游戏行业报告
  • XML转义符详解:如何在XML中正确处理特殊字符
  • Vue3项目匹配PC端和移动端---两套组件
  • 生成式人工智能大模型备案政策深度解读
  • 请大家推荐一款免费的网站模版。
  • 【C++】多参数构造函数使用explict的情形
  • LSM-Tree(Log-Structured Merge-Tree)详解
  • Java 单例模式与线程安全
  • Electron使用WebAssembly实现CRC-16 MAXIM校验
  • jmeter环境搭建及使用
  • 【第9章】亿级电商平台订单系统-整体技术架构设计
  • 【华为OD-E卷 -123 判断一组不等式是否满足约束并输出最大差 100分(python、java、c++、js、c)】
  • AI技术学习笔记系列003:`liger_kernel`、`flashattn2` 和 `unsloth` 介绍
  • 第52届医疗器械博览会盛装启幕,开启AI 赋能驱动医疗装备“新视界”
  • 【k8s004】 Docker 打包 K8s镜像
  • 携程:今年第一季度营业收入约138亿元,入境旅游预订同比增长超100%
  • 上海普陀:原则同意将工业河更名为同济湾河
  • 外交部:将持续便利中外人员往来,让“中国游”金字招牌更加闪耀
  • 三件珍贵标本开箱!中国恐龙大展5月26日在沪开幕,明星标本汇聚一堂
  • 义乌至迪拜“铁海快线+中东快航”首发,物流成本降低18%
  • 习近平就乌拉圭前总统穆希卡逝世向乌拉圭总统奥尔西致唁电