Use idea to create a multi-module Maven project (inheritance and dependencies between modules)

_Kakashi_ 2022-08-06 16:25:59 阅读数:498

useideacreatemulti-modulemulti

今天记录一下maven的基本使用-创建多模块项目, 各位看到此博客的小伙伴,如有不对的地方请及时通过私信我或者评论此博客的方式指出,以免误人子弟.多谢!

直接上步骤:

Create an inheritance relationship module(Two modules have a parent-child relationship)

Create parent and child modules

第一步:创建父模块

依次点击 File -> New -> Project ->Maven ->Next

img

img

Fill in the following information according to your needs,最后点击FinishComplete the creation of the parent module.

img

创建后目录结构如下:

img

第二步:Modify the parent modulepom

在修改pom文件之前,Delete the parent module firstsrc目录.

img

然后在修改pom文件,If only simple to use,In fact, just modify the following two configurations:

1、Add a label and change it to pom.

2、Add tags to configure the management of all module dependencies.

修改后完整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.yjh.learn</groupId>
<artifactId>pro-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 当前工程作为父工程,它要去管理子工程,所以打包方式必须是 pom -->
<packaging>pom</packaging>
<!-- 子模块 -->
<modules>
<module>redis-learn</module>
<module>pro-common</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- 使用dependencyManagementTag configuration management of all module dependencies -->
<dependencyManagement>
<dependencies>
<!-- spring-boot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- spring redis 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

We have introduced three dependencies in the parent module,Next we create a studyredis的子模块.

创建子模块

第一步:创建子模块

选中父工程 pro-learn -> New -> Module -> Spring Initializr -> Module SDK(选择自己的jdk版本) -> Next

img

img

Fill in the following information according to your needs,点击Next进入下一步.

img

You can choose nothing here,点击Next进入下一步,然后,Fill in the information according to your needs,最后点击FinishComplete the creation of the submodule.

img

img

The directory structure after creating the submodule is as follows:

img

Here I put unused files and directories(.mvn、HELP.md、mvnw、mvnw.cmd)删掉,As for what these files do,自行百度,删除后如下,看着清爽多了:

img

第二步:父模块中添加modules管理子模块

在父pom中添加如下配置:

<modules>
<module>redis-learn</module>
</modules>

第三步:配置子模块pom

将redis-learn模块pom文件中parent中的坐标(groupId、artifactId、version)Change to the coordinates of the parent module,and add dependencies,具体看以下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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 使用parent标签指定当前工程的父工程 -->
<parent>
<!-- 父工程的坐标 -->
<groupId>com.yjh.learn</groupId>
<artifactId>pro-learn</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- 子工程的坐标 -->
<!-- 如果子工程坐标中的groupId和version与父工程一致,可以省略 -->
<groupId>com.yjh.learn</groupId>
<artifactId>redis-learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-learn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 子工程引用父工程中的依赖信息时,可以把版本号去掉. -->
<!-- 把版本号去掉就表示子工程中这个依赖的版本由父工程决定. -->
<!-- 具体来说是由父工程的dependencyManagement来决定. -->
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

At this point we look at the submodulesmaven的依赖包,The found version is the same as the parent module,如下:

img

Of course you can also create submodules just like you create parent modules,This way you don't have to do it yourself in the parent modulemodulesAdd the configuration yourself,会自动添加,但是响应的src目录下也没有SpringBootthe startup class andresources文件夹,Just follow your habit.

Create a dependency module(A module references another module)

在我们开发中,Is not often put some public methods,比如你的util工具类,Constant classes, etc. into a separate package,比如放到common包中,If divided into multiple modules,These public methods can be placed in a separate module.

创建子模块

第一步:创建子模块

Created like aboveredis-learn模块一样,创建一个子模块pro-common用来存放公共类.

如下,我们在pro-commonOne has been added to the moduleTaskIdUtil的工具来,This class is used in my project by clientidGenerates self-incrementing serial numbers on a daily basis,格式为 yyMMdd + 4digit account serial number + 8位自增,当达到最大值999999999时从00000001开始自增,TaskIdUtil类代码如下:

package com.yjh.learn.procommon.utils;
import com.yjh.learn.procommon.cache.CacheCurrentDate;
import java.text.NumberFormat;
import java.util.concurrent.atomic.AtomicInteger;
/** * @author yjh */
public class TaskIdUtil {

private static final Integer MAX_VALUE = 99999999;
private static AtomicInteger num = new AtomicInteger(0);
/** * yyMMdd + 4digit account serial number + 8位自增 * * @param accountId 账号id * @return */
public static String getTaskId(String accountId) {

StringBuffer buffer = new StringBuffer();
String nowDateStr = CacheCurrentDate.INS.nowDateStr();
String accSeqnceNo = accFormat(accountId);
String sequenceNo = getSequenceNo();
return buffer.append(nowDateStr).append(accSeqnceNo).append(sequenceNo).toString();
}
private static String getSequenceNo() {

int initNum = num.get();
if (initNum == MAX_VALUE) {

num = new AtomicInteger(1);
return numFormat(1);
}
int result = num.incrementAndGet();
return numFormat(result);
}
private static String numFormat(int number) {

NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
format.setMaximumIntegerDigits(8);
format.setMinimumIntegerDigits(8);
String result = format.format(number);
return result;
}
private static String accFormat(String accountId) {

NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
format.setMaximumIntegerDigits(4);
format.setMinimumIntegerDigits(4);
String result = format.format(Integer.parseInt(accountId));
return result;
}
}

第二步:在redis-learn引用pro-common

修改redis-learn模块中的pom文件,通过pro-commoncoordinates to referencepro-common模块,在redis-learn的pom中添加如下配置:

<dependency>
<groupId>com.yjh.learn</groupId>
<artifactId>pro-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

This is our refreshmaven依赖可以看到pro-commonhas been introduced to oursredis-learn模块中了,如下:

img

第三步:测试依赖是否可用

在redis-learnAdd the following test method to the test class in the module:

@Test
void contextLoads() {

for (int i = 0; i < 20; i++) {

System.out.println(TaskIdUtil.getTaskId("1"));
}
}

found to be properly citedpro-commonThe module's tools come and execute successfully,执行结果如下:

img

至此,一个基本的mavenThe managed multi-module project is completed.

如果我的博客对你有帮助,欢迎进行评论️️、点赞、收藏️️,满足一下我的虚荣心

copyright:author[_Kakashi_],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061616341459.html