如何使 Maven Javadoc 插件适用于任何 Java 版本 [英] How to make Maven Javadoc Plugin work with any Java version

查看:225
本文介绍了如何使 Maven Javadoc 插件适用于任何 Java 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这样的 Maven Javadoc 插件

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><version>3.1.1</version><执行><执行><id>attach-javadocs</id><目标><goal>jar</goal></目标></执行></执行></插件>

我安装了多个 JDK 版本,并希望能够使用其中任何一个版本构建项目.上述配置在 Java 8 上运行良好,但在 Java 11 上失败并出现以下错误

<块引用>

未能执行目标org.apache.maven.plugins:maven-javadoc-plugin:3.1.1:jar(attach-javadocs) on project ...: MavenReportException: Error while生成 Javadoc:无法找到 javadoc 命令:环境变量 JAVA_HOME 设置不正确.

显而易见的解决方案是设置 JAVA_HOME.但是正如我提到的,我有多个版本的 JDK,所以每次我想使用另一个版本时都重新配置 JAVA_HOME 一点也不方便.

另一个解决方案(来自 无法找到 javadoc 命令 - maven)是将以下配置添加到插件中:

<预><代码><配置><javadocExecutable>${java.home}/bin/javadoc</javadocExecutable></配置>

这使它适用于 Java 11,但会破坏 Java 8,因为在这种情况下 javadoc 位置是 ${java.home}/../bin/javadoc.

理想情况下,我总是想从 Maven 使用的 java 可执行文件所在的目录中使用 javadoc,但我还没有找到使用 Maven Javadoc 插件的方法.

解决方案

Maven 配置文件可能会有所帮助.

将这样的内容添加到您的 POM.

<个人资料><id>jdk-8-config</id><激活><jdk>1.8</jdk></激活><属性><javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable></属性></个人资料><个人资料><id>jdk-11-config</id><激活><jdk>11</jdk></激活><属性><javadocExecutable>${java.home}/bin/javadoc</javadocExecutable></属性></个人资料></个人资料>

JDK 范围也是可能的,请在链接的文档中阅读更多内容.

Maven 将选择用于运行自身的 Java 版本,激活正确的配置文件,并正确定义属性.

警告 - 通常我们使用 JAVA_HOME 集构建,或通过 Jenkins 构建,Jenkins 可以配置为为每个作业定义 JAVA_HOME.这种方法在这些情况下效果很好.

您还可以研究 Maven 工具链.我对这些没有任何经验,但阅读它们有助于更轻松地定义各种机器上的刀具位置.

I'm using Maven Javadoc Plugin like this

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <id>attach-javadocs</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I have multiple JDK versions installed and want to be able to build a project with any of them. The above configuration works fine with Java 8, but it fails with Java 11 with the following error

Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.1.1:jar (attach-javadocs) on project ...: MavenReportException: Error while generating Javadoc: Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set.

The obvious solution is to set JAVA_HOME. But as I mentioned, I have multiple versions of JDK, so reconfiguring JAVA_HOME every time I want to use another version wouldn't be convenient at all.

Another solution (from Unable to find javadoc command - maven) is to add the following configuration to the plugin:

<configuration>
    <javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>

This makes it work with Java 11, but breaks Java 8 because the javadoc location is ${java.home}/../bin/javadoc in that case.

Ideally I always want to use javadoc from the directory where the java executable that Maven uses is, but I haven't found a way to do it with Maven Javadoc Plugin.

解决方案

Maven profiles may be able to help.

Add something like this to your POM.

<profiles>
    <profile>
        <id>jdk-8-config</id>
        <activation>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
        </properties>
    </profile>
    <profile>
        <id>jdk-11-config</id>
        <activation>
            <jdk>11</jdk>
        </activation>
        <properties>
            <javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
        </properties>
    </profile>
</profiles>

JDK ranges are also possible, read more on those in the linked doc.

Maven will pick up the version of Java being used to run itself, activate the correct profile, and define the property correct.

A caveat - typically we build with JAVA_HOME set, or via Jenkins which can be configured to define JAVA_HOME per job. This approach works well in those cases.

You could also investigate Maven toolchains. I have no experience with those, other than reading they help make it easier to define tool locations on various machines.

这篇关于如何使 Maven Javadoc 插件适用于任何 Java 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆