假设您有一个将以两种形式分发的项目:一种用于 appserver A,另一种用于 appserver B。作为对这两个服务器的自定义,您需要排除一些您将要分发的 appserver 未使用的依赖项.
注意: 虽然将 <excludes> 放在 <dependencySets> 中可能会提供我们想要的结果,但不建议这样做,因为如果新的应用服务器可用,您将必须维护其他发行版的排除项以排除对新应用服务器的依赖项。
这个例子演示了 <componentDescriptors> 的使用,更多信息可以在这里找到。
首先,让我们为 appserver A 发行版编写程序集描述符。它应该是这样的:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>appserverA</id> <formats> <format>zip</format> </formats> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> <includes> <include>application:logging</include> <include>application:core</include> <include>application:utils</include> <include>application:appserverA</include> </includes> </dependencySet> </dependencySets> </assembly>
appserver B 分发的程序集描述符将类似:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>appserverB</id> <formats> <format>zip</format> </formats> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> <includes> <include>application:logging</include> <include>application:core</include> <include>application:utils</include> <include>application:appserverB</include> </includes> </dependencySet> </dependencySets> </assembly>
从显示的两个描述符中,我们可以说两者共有三个工件,因此我们将它们分成一个公共组件描述符并将其保存为src/assembly/component.xml。它的内容是:
<component> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> <includes> <include>application:logging</include> <include>application:core</include> <include>application:utils</include> </includes> </dependencySet> </dependencySets> </component>
那么应用服务器 A 的最终程序集描述符将是:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>appserverA</id> <formats> <format>zip</format> </formats> <componentDescriptors> <componentDescriptor>src/assembly/component.xml</componentDescriptor> </componentDescriptors> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> <includes> <include>application:appserverA</include> </includes> </dependencySet> </dependencySets> </assembly>
应用服务器 B 的相应程序集描述符将是:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>appserverB</id> <formats> <format>zip</format> </formats> <componentDescriptors> <componentDescriptor>src/assembly/component.xml</componentDescriptor> </componentDescriptors> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> <includes> <include>application:appserverB</include> </includes> </dependencySet> </dependencySets> </assembly>
现在我们应该为 Assembly Plugin 更新项目的 POM 配置,如下所示:
<project> [...] <build> [...] <plugins> [...] <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptors> <descriptor>src/assembly/appserverA-assembly.xml</descriptor> <descriptor>src/assembly/appserverB-assembly.xml</descriptor> </descriptors> </configuration> </plugin> [...] </project>