«

java基于maven多模块合并打包部署的方法是什么

时间:2024-3-10 21:56     作者:韩俊     分类: Java


这篇文章主要介绍“java基于maven多模块合并打包部署的方法是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“java基于maven多模块合并打包部署的方法是什么”文章能帮助大家解决问题。

项目环境

  • jdk 1.8

  • spring 2.7.x

  • maven 3.6

项目结构

模块功能

client – 对外service
common – 共用工具切面等
main – 启动类
goods – 子模块具体实现

模块间依赖关系

client – 无依赖
common – 无依赖
main – client、common、goods
goods – client、common

具体POM配置

顶级pom

引入需要被使用的所有项目子模块,并统一制定版本号

    <groupId>org.ct</groupId>
    <artifactId>gp-data-analysis</artifactId>
    <packaging>pom</packaging>
    <version>${gp-data.version}</version>
    <modules>
        <module>gp-data-client</module>
        <module>gp-data-common</module>
        <module>gp-futrue-goods</module>
        <module>gp-data-main</module>
    </modules>
    
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <gp-data.version>1.0.0</gp-data.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.ct</groupId>
                <artifactId>gp-data-analysis</artifactId>
                <version>${gp-data.version}</version>
            </dependency>
            <dependency>
                <groupId>org.ct</groupId>
                <artifactId>gp-data-main</artifactId>
                <version>${gp-data.version}</version>
            </dependency>
            <dependency>
                <groupId>org.ct</groupId>
                <artifactId>gp-data-common</artifactId>
                <version>${gp-data.version}</version>
            </dependency>
            <dependency>
                <groupId>org.ct</groupId>
                <artifactId>gp-data-client</artifactId>
                <version>${gp-data.version}</version>
            </dependency>
            <dependency>
                <groupId>org.ct</groupId>
                <artifactId>gp-data-futrue</artifactId>
                <version>${gp-data.version}</version>
            </dependency>
         </dependencies>
    </dependencyManagement>

    <build>
        <!-- 引入maven打包工具 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

client、common、goods类似

 <!-- 指定父pom版本 -->
    <parent>
        <artifactId>gp-data-analysis</artifactId>
        <groupId>org.ct</groupId>
        <version>${gp-data.version}</version>
    </parent>

main 设置打包入口,扫描的xml配置项等
&emsp; &emsp; main 中dependency中引入顺序需要注意,需要保证,未被依赖的在前,被依赖的子模块在后。因为此处的顺序决定了maven install的顺序

    <parent>
        <artifactId>gp-data-analysis</artifactId>
        <groupId>org.ct</groupId>
        <version>${gp-data.version}</version>
    </parent>
     <dependencies>
        <dependency>
            <groupId>org.ct</groupId>
            <artifactId>gp-data-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ct</groupId>
            <artifactId>gp-data-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ct</groupId>
            <artifactId>gp-data-futrue</artifactId>
        </dependency>
    </dependencies>

     <build>
        <plugins>
            <!--打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <goals>
                            <!-- 整合打jar包 -->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--支持静态文件热部署-->
                    <addResources>true</addResources>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>org.ct.gp.GpDataBootApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>

        <!--打包规则,打包时将resources文件夹打包进去-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
            </resource>

            <!--保证mapper文件夹中的.xml文件可以被编译-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

使用maven打包root目录

生成的main jar,及包含了所有模块

标签: java maven

热门推荐