Maven 3:为定义的工件生成 Javadoc

发布于 2024-10-17 01:33:27 字数 399 浏览 6 评论 0原文

我只想从专用文档项目中为我的项目的某些工件生成 javadoc。

这意味着我想要一个名为“docs”的独立项目。在 docs/pom.xml 中,我想定义应包含在生成的 javadoc 中的工件。

到目前为止,我了解到我必须为我想要包含的项目生成一个单独的sources.jar。但我不知道如何从那里继续下去。

现在我只能想象两种方法:

  1. 获取我想要包含的工件(sources.jar),解压它们并以某种方式将Javadoc插件指向源目录。

  2. 将我感兴趣的工件定义为依赖项,并使用 javadoc-plugin 的“dependencySourceInclude”选项。但我不确定这是否是预期的用法。

有什么建议如何解决这个问题吗?

I want to generate javadocs only for certain artifacts of my project from within a dedicated docs-project.

That means that I would like to have an independent project called "docs" for example. In the docs/pom.xml I would like to define the artifacts that should be included in the generated javadocs.

So far I learned that I have to generate a separate sources.jar for the projects I want to include. But I can't figure out how to go on from there.

For now I can only imagine two approaches:

  1. Get the artifacts (sources.jar) I want to include, unpack them and somehow point the Javadoc plugin to the source directory.

  2. Define the artifacts I am interested as dependency and use the "dependencySourceInclude" option of the javadoc-plugin. But I am not sure if this is usage as intended.

Any suggestions how to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-10-24 01:33:27

我自己找到了解决方案。这有点麻烦,但对我来说确实有用。我选择遵循我的第一个想法:

获取我想要包含的工件 (sources.jar),解压它们并以某种方式将 javadoc 插件指向源目录。

该解决方案有四个不同的部分,稍后我将更详细地解释:

  1. 在我想要包含的所有工件中生成sources.jars
  2. 解压这些sources.jars
  3. 通过将 javadoc-plugin 指向解压的源代码来生成 Javadoc
  4. 将生成的 apidocs 打包在zip 文件

现在更详细:

1. 在我想要包含的所有工件中生成sources.jars

要生成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>

您必须在要包含的每个项目/模块/工件中执行此操作你的API文档。

2.解压这些sources.jars

在用于生成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 知道在哪里查找源文件。导入的定义是 定义。在本节中,我们定义在步骤 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 文件夹。 目标文件夹。在此站点文件夹中,您将找到您的 apidocs。但为了让这个构建变得光彩夺目,我们希望将 apidocs 组装成 zip 存档。

4.将生成的 apidocs 打包在 zip 文件中

要组装文档,您必须使用 maven-assemble-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>

assemble.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>

I have found a solution my self. It is a bit of a hack but it does work for me. I chose to go with my first idea:

Get the artifacts (sources.jar) I want to include, unpack them and somehow point the javadoc plugin to the source directory.

This solution has four differents parts which I'll explain in more detail later:

  1. Generate sources.jars in all artifacts I want to include
  2. Unpack those sources.jars
  3. Generate Javadoc by pointing the javadoc-plugin to the unpacked sources
  4. Package the generated apidocs in a zip file

Now in more detail:

1. Generate sources.jars in all artifacts I want to include

To generate sources.jars you have to use the maven-sources-plugin as follows:

<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>

You have to do this in every project/module/artifact you want to include in your apidocs.

2. Unpack those sources.jars

In you pom.xml you use to generate the javadocs you have to add the following plugins to unpack the sources.jar files.

<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>

You can add as many unpack-execution-blocks as you like.

3. Generate Javadoc by pointing the javadoc-plugin to the unpacked sources

Now the tricky part. Letting the javadoc-plugin know where to look for the source files. The imported definition is the <sourcepath> definition. In this section we define the folder where we have unpacked the sources in step 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>

When you call mvn clean install at this point you will end up with a site folder inside your target folder. In this site folder you'll find your apidocs. But to make this build all shiny and stuff we want to assemble the apidocs into a zip archive.

4. Package the generated apidocs in a zip file

To assemble the docs you have to use the maven-assembly-plugin and a extra assembly-file.
First the plugin-defintion inside your 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>

assemble.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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文