我自己找到了解决方案。这有点麻烦,但对我来说确实有用。我选择遵循我的第一个想法:
获取我想要包含的工件 (sources.jar),解压它们并以某种方式将 javadoc 插件指向源目录。
该解决方案有四个不同的部分,我稍后将更详细地解释:
- 在我想要包含的所有工件中生成sources.jar
- 解压这些sources.jar
- 通过将 javadoc-plugin 指向解压的源代码来生成 Javadoc
- 将生成的 apidocs 打包在 zip 文件中
现在更详细:
1.在我想要包含的所有工件中生成sources.jar
要生成sources.jars,您必须使用maven-sources-plugin,如下所示:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>bundle-sources</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
您必须在要包含在 apidocs 中的每个项目/模块/工件中执行此操作。
2. 解压这些sources.jar
在用于生成 javadoc 的 pom.xml 中,您必须添加以下插件来解压sources.jar 文件。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-artifact-sources</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId><!-- your artifact here --></artifactId>
<version>${project.version}</version>
<classifier>sources</classifier>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/unpack_sources</outputDirectory>
</configuration>
</execution>
<!-- add more unpack-executions here -->
</executions>
</plugin>
您可以根据需要添加任意数量的解包执行块。
3. 通过将 javadoc-plugin 指向解压的源代码来生成 Javadoc
现在是棘手的部分。让 javadoc-plugin 知道在哪里寻找源文件。导入的定义是<sourcepath>
定义。在本节中,我们定义在步骤 2 中解压源代码的文件夹。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
<sourcepath>${project.build.directory}/unpack_sources</sourcepath>
</configuration>
<executions>
<execution>
<goals>
<goal>javadoc</goal>
</goals>
<phase>process-resources</phase>
</execution>
</executions>
</plugin>
你打电话时mvn clean install
此时你最终会得到一个site
你里面的文件夹target
文件夹。在此站点文件夹中,您将找到您的 apidocs。但为了让这个构建变得光彩夺目,我们希望将 apidocs 组装成 zip 存档。
4. 将生成的apidocs打包为zip文件
要组装文档,您必须使用maven-assembly-plugin
和一个额外的汇编文件。
首先是 pom 中的插件定义:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>docs-assembly</id>
<phase>package</phase>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assemble.xml</descriptor>
</descriptors>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
汇编.xml:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>${project.build.finalName}</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/site/apidocs</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>