maven多模块项目搭建

文章目录

创建方式

创建父项目

powershell 复制代码
#创建文件夹后,进入目录,执行以下命令
PS D:\demo> mvn archetype:generate
#将输出很多模板,供你选择,输入前面的数字,回车,进行选择
2129: remote -> io.quarkus:quarkus-amazon-lambda-rest-archetype (-)
2130: remote -> io.github.techacademy-curricula:daily-report-system-archetype (An archetype which contains a sample daily_report_system Webapp project.)
2131: remote -> com.bitofcode.archetype:jakartaee-microprofile (Jakarta EE with Eclipse MicroProfile project.)
2132: remote -> org.apache.maven.archetypes:maven-archetype-profiles (-)
#此时输入模板名称maven-archetype-quickstart
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 17: maven-archetype-quickstart
#将会列出名为maven-archetype-quickstart的模板,此处一共有两个
Choose archetype:
1: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
2: remote -> com.github.ywchang:maven-archetype-quickstart (Provide up-to-date java quickstart archetype)
#选择要使用第几个
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1: 1
#选择使用的版本(该模板具有多个版本),我选择的是1.4版本,所以输入4回车
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0
2: 1.1
3: 1.3
4: 1.4
Choose a number: 4: 4
#开始下载模板
Downloading: http://rdmaven.test.com:8081/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.pom
Downloaded: http://rdmaven.test.com:8081/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.pom (1.6 kB at 14 kB/s)
Downloading: http://rdmaven.test.com:8081/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/1.4/maven-archetype-bundles-1.4.pom
Downloaded: http://rdmaven.test.com:8081/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/1.4/maven-archetype-bundles-1.4.pom (4.5 kB at 99 kB/s)
Downloading: http://rdmaven.test.com:8081/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar
Downloaded: http://rdmaven.test.com:8081/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar (7.1 kB at 154 kB/s)
#输入groupId
Define value for property 'groupId': com.demo
#输入artifactId
Define value for property 'artifactId': multiDemo
#输入version,直接回车将使用默认值1.0-SNAPSHOT
Define value for property 'version' 1.0-SNAPSHOT: :
#输入package,直接回车,将使用groupId
Define value for property 'package' com.demo: :
#确认项目配置,回车即可完成创建父项目
Confirm properties configuration:
groupId: com.demo
artifactId: multiDemo
version: 1.0-SNAPSHOT
package: com.demo
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.demo
[INFO] Parameter: artifactId, Value: multiDemo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.demo
[INFO] Parameter: packageInPathFormat, Value: com/demo
[INFO] Parameter: package, Value: com.demo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.demo
[INFO] Parameter: artifactId, Value: multiDemo
[INFO] Project created from Archetype in dir: D:\demo\multiModule\multiDemo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:21 min
[INFO] Finished at: 2024-01-22T09:43:36+08:00
[INFO] Final Memory: 16M/202M
[INFO] ------------------------------------------------------------------------
PS D:\demo>

创建子模块

在项目目录里,重复执行以上命令

目录结构

my-project
├── my-module1
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   ├── resources
│   │   ├── test
│   │   │   ├── java
│   │   │   ├── resources
│   ├── target
│   ├── pom.xml
├── my-module2
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   ├── resources
│   │   ├── test
│   │   │   ├── java
│   │   │   ├── resources
│   ├── target
│   ├── pom.xml
├── pom.xml

示例

父模块

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ynh</groupId>
    <artifactId>micro</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <modules>
        <module>micro-admin</module>
        <module>micro-auth</module>
        <module>micro-common</module>
        <module>micro-movie</module>
        <module>micro-star</module>
        <module>micro-user</module>
    </modules>
    <packaging>pom</packaging>
    <properties>
        <java.version>1.8</java.version>
        <springboot.version>2.2.6.RELEASE</springboot.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
        <lombok.version>1.18.10</lombok.version>
        <fastjson.version>1.2.60</fastjson.version>
        <gson.version>2.8.5</gson.version>
        <jackson.version>2.11.4</jackson.version>
        <hutool.version>5.1.1</hutool.version>
        <protostuff.version>1.6.0</protostuff.version>
        <spring.version>5.2.12.RELEASE</spring.version>
        <tomcat.version>9.0.43</tomcat.version>
        <xstream.version>1.4.15</xstream.version>
        <junit.version>4.12</junit.version>
        <commons-lang.version>2.6</commons-lang.version>
        <guava.version>28.1-jre</guava.version>
        <druid.version>1.1.17</druid.version>
        <commons-codec.version>1.13</commons-codec.version>
        <ehcache3.version>3.5.0</ehcache3.version>
        <caffeine.version>2.8.0</caffeine.version>
        <disruptor.version>3.4.2</disruptor.version>
        <antisamy.version>1.5.8</antisamy.version>
        <mybatis-plus.version>3.2.0</mybatis-plus.version>
        <dynamic-datasource.version>2.5.4</dynamic-datasource.version>
        <mybatis-spring-boot-starter.version>2.1.0</mybatis-spring-boot-starter.version>
        <mariadb.version>2.3.0</mariadb.version>
        <pagehelper.version>4.1.1</pagehelper.version>
        <redisson.version>3.11.4</redisson.version>
        <pinyin4j.version>2.5.1</pinyin4j.version>
        <jts.version>1.14</jts.version>
        <servlet.version>2.5</servlet.version>
        <easyexcel.version>2.1.4</easyexcel.version>
        <slf4j.version>1.7.28</slf4j.version>
        <log4j-over-slf4j.version>1.7.25</log4j-over-slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <cglib.version>3.3.0</cglib.version>
        <jasypt.version>1.16</jasypt.version>
        <mybatis.version>3.5.6</mybatis.version>
        <snakeyaml.version>1.27</snakeyaml.version>
        <poi.version>4.1.2</poi.version>
        <httpclient.version>4.5.13</httpclient.version>
        <redisson.version>3.11.1</redisson.version>
        <rocketmq-common.version>4.8.0</rocketmq-common.version>
        <rocketmq-spring-boot-starter.version>2.1.0</rocketmq-spring-boot-starter.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- 包含SpringCloud、openFeign等版本信息-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- 包含nacos相关等版本信息-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--  mybatis/数据库相关 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
                <version>${dynamic-datasource.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot-starter.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mariadb.jdbc</groupId>
                <artifactId>mariadb-java-client</artifactId>
                <version>${mariadb.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <dependency>
                <groupId>com.demo.micro</groupId>
                <artifactId>common</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson</artifactId>
                <version>${redisson.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.rocketmq</groupId>
                <artifactId>rocketmq-client</artifactId>
                <version>${rocketmq-common.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.rocketmq</groupId>
                <artifactId>rocketmq-common</artifactId>
                <version>${rocketmq-common.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.rocketmq</groupId>
                <artifactId>rocketmq-spring-boot-starter</artifactId>
                <version>${rocketmq-spring-boot-starter.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

模块A

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>
        <groupId>com.ynh</groupId>
        <artifactId>micro</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ynh</groupId>
    <artifactId>micro-common</artifactId>
    <version>${parent.version}</version>
    <name>micro-common</name>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <!--    此处不配置,继承父pom的build    -->
    </build>
</project>

模块B(并在模块B中引入模块A)

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>com.ynh</groupId>
        <artifactId>micro</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <groupId>com.ynh</groupId>
    <artifactId>micro-star</artifactId>
    <version>${parent.version}</version>
    <name>micro-star</name>
    <description>StarManger</description>
    <properties>
        <!--        <java.version>1.8</java.version>-->
        <!--        <mybatis-plus.version>3.2.0</mybatis-plus.version>-->
        <!--        <dynamic-datasource.version>2.5.4</dynamic-datasource.version>-->
        <!--        <mybatis-spring-boot-starter.version>2.1.0</mybatis-spring-boot-starter.version>-->
        <!--        <gson.version>2.8.5</gson.version>-->
        <!--        <jackson.version>2.10.0</jackson.version>-->
        <!--        <argLine>-Dfile.encoding=UTF-8</argLine>-->
        <!--        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>-->
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.ynh</groupId>
            <artifactId>micro-common</artifactId>
            <version>${parent.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-typehandlers-jsr310</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.2.1</version>
        </dependency>
        <!-- json相关  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
    </dependencies>
    <build>
        <!--    此处不配置,继承父pom的build    -->
    </build>
</project>

注意事项

1.子文件夹和module名要对应,否则无法正确识别,idea中无法解析为树状

2.子模块会继承父项目的build

3.插件的作用

4.引入其他模块

相关推荐
m0_571957582 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
魔道不误砍柴功4 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2344 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
闲晨4 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
测开小菜鸟5 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
P.H. Infinity6 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天7 小时前
java的threadlocal为何内存泄漏
java
caridle7 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^7 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋37 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx