使用多模块构建的父 POM 创建程序集是常见的做法。有时,您可能希望确保此程序集还包含此构建中一个或多个模块的源代码。
此示例演示如何在项目程序集中的源/<module-name>目录下包含来自模块的项目源。
首先,让我们编写一个程序集描述符来创建这个程序集。为了清楚起见,该描述符将尽可能简单,仅演示此示例描述的特征。
<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>src</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>org.test:child1</include>
</includes>
<sources>
<includeModuleDirectory>false</includeModuleDirectory>
<fileSets>
<fileSet>
<outputDirectory>sources/${module.artifactId}</outputDirectory>
<excludes>
<exclude>${project.build.directory}/**</exclude>
</excludes>
</fileSet>
</fileSets>
</sources>
</moduleSet>
</moduleSets>
</assembly>
此描述符声明程序集 id 应为src,输出格式为目录,并且程序集的内容不应包含在以顶级项目的 finalName 命名的目录中。
此外,它声明我们希望将模块的源文件包含在 groupId 为org.test和 artifactId 为child1的模块中。这些源应包含在此模块的目录结构sources/ child1 中,因为 outputDirectory 表达式将在逐个模块的基础上进行插值。
默认情况下,Assembly Plugin 会将源代码添加到以每个模块的 artifactId 命名的文件夹下;这可以通过将includeModuleDirectory设置为false来禁用。
请注意,构建目录(默认为target)将被包括在内,因此它被明确排除,因为这是构建期间生成的文件的临时存储,并且不应包含任何项目源。
现在,让我们回顾一下通过assembly:single目标启用该程序集构建所需的 POM 配置:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Parent</name>
<modules>
<module>child1</module>
<module>child2</module>
<module>child3</module>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/src.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
此 POM 仅指示 Assembly Plugin 在执行时使用src.xml程序集描述符。