maven-assembly-plugin 插件多模块打包实例

参考了较多资料,踩了不少坑,总算是打包成功了,记录如下:

1,项目结构:

父工程:hdpetl

子模块:assembly,genhttp,telecomdata

文件(夹):logs文件夹,README.txt文件

如下:

其中assembly 是用来打包的模块,没有任何代码,就两个文件。其他模块为正常业务数据模块

2,pom文件

hdpetl.pom

    <modules>
        <module>genhttp</module>
        <module>telecomdata</module>
        <module>assembly</module> <!--打包模块排在最后-->
    </modules>

。。。正常dependency依赖略。。。

    <!--assembly-->

                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <finalName>hdpetl-${project.version}</finalName>
                        <descriptors>
                            <descriptor>assembly/assembly.xml</descriptor>
                        </descriptors>
                        <outputDirectory>d://</outputDirectory>
                        <appendAssemblyId>true</appendAssemblyId>

                    </configuration>
                </plugin>

assembly.pom,注意依赖其他两个业务模块


    <dependencies>
        <dependency>
            <groupId>www.abc.net.cn</groupId>
            <artifactId>genhttp</artifactId>
            <version>5.0</version>
        </dependency>
        <dependency>
            <groupId>www.abc.net.cn</groupId>
            <artifactId>telecomdata</artifactId>
            <version>5.0</version>
        </dependency>
    </dependencies>

 <!--assembly-->
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>

                        </execution>
                    </executions>
                    <configuration>
                        <descriptors>
                            <descriptor>assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </plugin>

其他两个业务模块telecomdata和genhttp pom文件正常有自己特有的dependency,其他无特殊标签依赖等

assembly.xml

<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/1.1.3
          http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>assembly</id>
    <baseDirectory>hdpetl-${project.version}</baseDirectory>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>


    <fileSets>
        <fileSet>
            <directory>../genhttp/target</directory>
            <outputDirectory>jar</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>../telecomdata/target</directory>
            <outputDirectory>jar</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>../genhttp/src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>../genhttp/src/main/shell</directory>
            <outputDirectory>shell</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>../logs</directory>
            <outputDirectory>logs</outputDirectory>
        </fileSet>

        <fileSet>
            <includes>
                <include>../README.txt</include>
            </includes>
            <outputDirectory></outputDirectory>
        </fileSet>
    </fileSets>


    <!--依赖打包-->
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>provided</scope>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>system</scope>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>hdpetl:genhttp</include>
                <include>hdpetl:telecomdata</include>
            </includes>
<!--            <binaries>
                <outputDirectory>jar</outputDirectory>
                <unpack>false</unpack>
                <dependencySets>
                    <dependencySet>
                        <outputDirectory>lib</outputDirectory>
                    </dependencySet>
                </dependencySets>
            </binaries>-->
        </moduleSet>
    </moduleSets>
</assembly>

 

assembly 下 执行命令:

mvn -X clean package assembly:single

 

即在 d盘根目录下生成 hdpetl-5.0-assembly.tar.gz 文件

解压:

两个业务模块已经打包jar文件到jar中

父工程的公共依赖和子模块的依赖,上述两个子模块的jar包一起在lib文件夹中

其他文件正常按配置打包到相应的文件夹中

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Maven-assembly-plugin是一个用于打包插件,可以将项目中的多个模块合并成一个可执行的JAR包或者WAR包。然而,有时候我们可能需要对已经打包好的JAR或者WAR包进行进一步的处理或者外部打包。以下是关于maven-assembly-plugin外部打包的解答。 首先,我们需要确保已经正确配置了maven-assembly-plugin,并且在项目的pom.xml文件中已经定义好了自定义的assembly描述文件(assembly.xml)。 在进行外部打包之前,我们需要先将项目进行构建和打包。在项目根目录下通过命令行输入"mvn clean install"命令来构建项目,并且将项目打包成JAR或者WAR文件。 接下来,在项目根目录下创建一个新的文件夹(例如,external_package),用于存放外部打包的文件。 然后,可以通过以下命令来进行外部打包: mvn assembly:assembly -Ddescriptor=assembly.xml -DoutputDirectory=/path/to/external_package 在上面的命令中,-Ddescriptor参数用于指定assembly描述文件所在的路径以及文件名称,-DoutputDirectory参数用于指定外部打包文件存放的路径。 执行完上述命令后,maven-assembly-plugin会根据assembly.xml文件的定义,在指定的路径下生成外部打包文件。 通过以上步骤,我们就可以实现maven-assembly-plugin的外部打包功能。需要注意的是,我们在进行外部打包之前,需要先正确配置和打包项目,并且确保已经定义了正确的assembly描述文件。 外部打包可以用于将项目的可执行文件(如JAR或者WAR文件)与其它资源(如配置文件或者依赖库)合并打包成一个单独的压缩文件,方便部署和分发。它可以帮助我们将项目打包成一个易于使用和发布的形式,简化部署流程,提高开发效率。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值