Download Sources and Javadocs with Maven – 用Maven下载源代码和Javadocs

最后修改: 2020年 7月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Peeking at the source code and documentation of different libraries and frameworks is a good way to learn more about them.

偷看不同库和框架的源代码和文档是了解它们的一个好方法。

In this short tutorial, we’ll see how to configure Maven, or ask of Maven, to download the dependency sources and their Javadocs for us.

在这个简短的教程中,我们将看到如何配置Maven,或要求Maven为我们下载依赖源及其Javadocs。

2. Command Line

2.命令行

By default, Maven only downloads the actual JAR file of each dependency, not the sources and documentation files.

默认情况下,Maven只下载每个依赖项的实际JAR文件,而不是源代码和文档文件。

To download just the sources, first, we should navigate to the directory containing the pom.xml and then execute the command:

要只下载源代码,首先,我们应该导航到包含pom.xml的目录,然后执行命令:

mvn dependency:sources

It may take a while to download the sources. Similarly, to download just the Javadocs, we can issue the command:

下载源代码可能需要一些时间。同样,如果只下载Javadocs,我们可以发出命令

mvn dependency:resolve -Dclassifier=javadoc

Of course, we can download both of them in one command, too:

当然,我们也可以在一个命令中同时下载它们。

mvn dependency:sources dependency:resolve -Dclassifier=javadoc

Obviously, if we add a new dependency after issuing these commands, we have to re-issue the commands to download sources and Javadocs for the new dependency.

很明显,如果我们在发出这些命令后增加了一个新的依赖关系,我们必须重新发出命令,为新的依赖关系下载源代码和Javadocs。

3. Maven Settings

3.Maven设置

It’s also possible to download sources and documentation system-wide on all Maven projects. To do that, we should edit the ~/m2/settings.xml file or create one and add the following configuration to it:

也可以在所有Maven项目中全系统下载源代码和文档。要做到这一点,我们应该编辑~/m2/settings.xml文件或创建一个,并在其中添加以下配置。

<settings>
    <!-- ... other settings omitted ... -->
    <profiles>
        <profile>
            <id>downloadSources</id>
            <properties>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </properties>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>downloadSources</activeProfile>
    </activeProfiles>
</settings>

As shown above, we’re creating a profile and activating it by default. In this profile, we’re setting two properties that tell Maven to download sources and documentation. Moreover, Maven will apply these settings to all projects.

如上所示,我们创建了一个配置文件,并默认激活了它。在这个配置文件中,我们设置了两个属性,告诉Maven下载源代码和文档。此外,Maven会将这些设置应用于所有项目。

4. The pom.xml

4.pom.xml

It’s even possible to put this configuration into the pom.xml. This way, we force all project contributors to download sources and documentation as part of the dependency resolution:

甚至可以把这个配置放到pom.xml中。这样一来,我们强制所有项目贡献者下载源代码和文档,作为依赖性解析的一部分

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>sources</goal>
                        <goal>resolve</goal>
                    </goals>
                    <configuration>
                        <classifier>javadoc</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Here, we’re configuring the maven-dependency-plugin to download the sources and documentation.

在这里,我们要配置maven-dependency-plugin来下载源代码和文档。

5. IDE Setup

5.IDE设置

We can also set up our favorite IDEs to do this for us. For instance, in IntelliJ IDEA, we just have to go to Preference > Build, Execution, Deployment > Build Tools > Maven > importing and check the sources and documentation checkboxes:

我们也可以对自己喜欢的IDE进行设置,让其为我们做这件事。例如,在IntelliJ IDEA中,我们只需进入Preference > Build, Execution, Deployment > Build Tools > Maven > importing 并勾选sources和document复选框。

6. Conclusion

6.结语

In this quick tutorial, we saw how to download dependency sources and documentation in Maven in a variety of ways, ranging from command-line solutions to per-project or system-wide configurations.

在这个快速教程中,我们看到了如何以各种方式在Maven中下载依赖源和文档,从命令行解决方案到每个项目或全系统配置。