maven插件--assembly

之前maven项目中使用assembly插件单独打包项目依赖包,项目只有一个模块也就一个pom,配置这个插件,一切很顺利。但是现在的项目复杂了,有parent有child,多模块。按照之前的做法怎么也不能成功。上网搜了下,找到一个打包多模块的,说每个模块单独打包,想了下感觉不对,在每个模块下面是把依赖的jar打包了,但是每个模块是单独的呀。最后求助官网:

http://maven.apache.org/plugins/maven-assembly-plugin/examples/index.html

上面有个多模块的说明,结合自己的项目,最终大功告成

项目名mls,根目录下的pom.xml是这个项目的parent,mls-web中包括了各种config.properties,没实际东西,用来打包的。parent中包含了各个子模块:

<modules>
    <module>mls-core</module>
    <module>mls-domain</module>
    <module>mls-domainservice</module>
    <module>mls-integration</module>
    <module>mls-facade</module>
    <module>mls-extservice</module>
    <module>mls-test</module>
    <module>mls-web</module>
 </modules>

mls-web是最后一个打包的,因此插件配置加在web项目中

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>mlslib</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/assembly/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

文件descriptor描述文件内容如下:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>mlslib</id>
    <formats>
        <format>war</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.houbank.mls:mls-core-common</include>
                <include>com.houbank.mls:mls-core-dal</include>
                <include>com.houbank.mls:mls-domain</include>
                <include>com.houbank.mls:mls-domainservice</include>
                <include>com.houbank.mls:mls-integration</include>
                <include>com.houbank.mls:mls-facade</include>
                <include>com.houbank.mls:mls-extservice</include>
                <!--<include>com.houbank.mls:mls-web</include>-->
            </includes>
            <binaries>
                <outputDirectory>modules</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
    <!--可以不加<fileSets>
        <fileSet>
            <directory>srcassembly
esources</directory>
            <outputDirectory></outputDirectory>
        </fileSet>
    </fileSets>-->
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>/WEB-INF/lib</outputDirectory>
            <scope>provided</scope>
        </dependencySet>
    </dependencySets>
</assembly>

插件的打包绑定到maven的package阶段,因此运行mvn clean package -P dev的时候会分离开自动打包依赖包。

game over!

原文地址:https://www.cnblogs.com/yhzh/p/5972737.html