好的,我已经通过以下选项:
- maven-assembly-plugin:构建组合二进制文件,也可能是源,但没有 javadocs。而且,非常复杂。
- maven-shade-plugin:非常容易构建组合的二进制文件和源代码,但没有 javadocs。此外,源文件名很难控制。
所以,我采取的简单方法如下:
确保所有模块都生成源 jar。这可以通过在父 pom 中启用源插件来完成,该插件由所有模块继承:
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
创建一个特殊的“构建”模块来组合所有其他模块的源。这是通过使用带有源分类器maven-dependency-plugin
的目标来完成的。unpack-dependencies
这意味着将所有其他模块源合并为该模块的源。
构建模块的 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>
<parent>
<artifactId>parent</artifactId>
<groupId>com.example.product</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>build</artifactId>
<packaging>jar</packaging>
<name>Build</name>
<properties>
<!--add comma separated artifact names from dependencies, to be included in unpacking-->
<project.build.unpack_includes>sub-product</project.build.unpack_includes>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sub-product</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${parent.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<classifier>sources</classifier>
<type>jar</type>
<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
<includeArtifactIds>${project.build.unpack_includes}</includeArtifactIds>
<includes>**\/*.java</includes>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
现在,构建模块像任何其他项目一样编译,生成所有需要的 jar。