SpringCloud项目Maven打包策略

0. 父级POM公共内容

<properties>
    <project.name>leway-cloud</project.name>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <maven-jar-plugin.version>3.2.2</maven-jar-plugin.version>
    <maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
</properties>

<build>
    <plugins>
        <!-- 编译版本控制-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>

        <!--跳过测试-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

1. 将子模块打包到各子模块目录下成单独的jar包

  • 子模块POM
<properties>
    <!--主启动类路径-->
    <mainClass.name>cn.zondy.leway.model.rabbitmq.RabbitMQApplication</mainClass.name>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <layout>ZIP</layout>
                <!--指定主启动类-->
                <mainClass>${mainClass.name}</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在这里插入图片描述

2. 抽离依赖,各模块仅打包源码,所有jar包输出到指定位置,依赖输出到指定位置lib目录下

  • 父级POM
 <!--jar打包插件-->
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <version>${maven-jar-plugin.version}</version>
     <configuration>
         <!-- 打包输出指定路径 -->
         <outputDirectory>D:/${project.name}</outputDirectory>
         <archive>
             <addMavenDescriptor>false</addMavenDescriptor>
             <manifest>
                 <addClasspath>true</addClasspath>
                 <classpathPrefix>lib/</classpathPrefix>
                 <!-- 指定启动主类-->
                 <mainClass>${mainClass.name}</mainClass>
             </manifest>
         </archive>
     </configuration>
 </plugin>

 <!--依赖库统一打包后放到指定目录-->
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
         <execution>
             <id>copy-dependencies</id>
             <phase>package</phase>
             <goals>
                 <goal>copy-dependencies</goal>
             </goals>
             <!-- 依赖库指定输出目录-->
             <configuration>
                 <outputDirectory>D:/${project.name}/lib</outputDirectory>
             </configuration>
         </execution>
     </executions>
 </plugin>
  • 子模块POM
<properties>
    <!--主启动类路径-->
    <mainClass.name>cn.zondy.leway.module.mongo.MongoApplication</mainClass.name>
</properties>

在这里插入图片描述

3. 抽离依赖,子模块打包成jar包,输出jar包及依赖到子模块目录

  • 父级POM
<!--依赖单独打包-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven-jar-plugin.version}</version>
    <configuration>
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <!-- 指定启动主类-->
                <mainClass>${mainClass.name}</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

<!--拷贝依赖到target中的lib目录-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>target/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive>
                <stripVersion>false</stripVersion>
                <includeScope>runtime</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>
  • 子模块POM
<properties>
    <!--主启动类路径-->
    <mainClass.name>cn.zondy.leway.module.mongo.MongoApplication</mainClass.name>
</properties>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值