maven-assembly-plugin maven自定义打包

梁华皓
2023-12-01

当你使用 Maven 对项目打包时,你需要了解以下 3 个打包 plugin,它们分别是

pluginfunction
maven-jar-pluginmaven 默认打包插件,用来创建 project jar
maven-shade-plugin用来打可执行包,executable(fat) jar
maven-assembly-plugin支持定制化打包方式,例如 apache 项目的打包方式

使用maven-assembly-plugin解决引入SDKclass方法

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>plugin-ws-manage</artifactId>
        <groupId>com.wbs.climb.pluginwsmanage</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>plugin-ws-rcyy</artifactId>

    <dependencies>

        <dependency>
            <groupId>com.wbs.climb.pluginwsmanage</groupId>
            <artifactId>plugin-ws-parent</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.xikang</groupId>
            <artifactId>supervise-sdk-client</artifactId>
            <version>0.1.3-lite</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 支持定制化打包方式 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <!--描述文件路径-->
                    <descriptors>
                        <descriptor>src/main/assembly/release.xml</descriptor>
                    </descriptors>
                    <!-- maven打包指定名称并去除jar-with-dependencies后缀 -->
                    <appendAssemblyId>false</appendAssemblyId>
                 </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 打包后拷贝目录 -->
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <!-- 存放的文件夹 -->
                                <copy todir="\hip\test_release\lib" overwrite="true">
                                    <fileset dir="${project.build.directory}">
                                        <include name="${project.artifactId}.jar"/>
                                    </fileset>
                                </copy>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>
</project>

release.xml

<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <!--包含-->
            <includes>
                <include>com.wbs.climb.pluginwsmanage:plugin-ws-rcyy</include>
                <include>com.xikang:supervise-sdk-client</include>
            </includes>
            <!-- 待验证 排除-->
            <excludes>
                <exclude>commons-logging:commons-logging</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

参考资料

maven-assembly-plugin 入门指南 - 简书

maven-assembly-plugin插件打jar包时排出指定的依赖 - 远去的列车 - 博客园

 类似资料: