Java 从 Maven 存储库获取源 JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2059431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:05:46  来源:igfitidea点击:

Get source JARs from Maven repository

javamavenmvn-repo

提问by Ioan Alexandru Cucu

Does anyone have any idea if you can find source JARs on Maven repositories?

有没有人知道您是否可以在 Maven 存储库上找到源 JAR?

采纳答案by Alain O'Dea

Maven Micro-Tip: Get sources and Javadocs

When you're using Maven in an IDE you often find the need for your IDE to resolve source code and Javadocs for your library dependencies. There's an easy way to accomplish that goal.

mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc

The first command will attempt to download source code for each of the dependencies in your pom file.

The second command will attempt to download the Javadocs.

Maven is at the mercy of the library packagers here. So some of them won't have source code packaged and many of them won't have Javadocs.

In case you have a lot of dependencies it might also be a good idea to use inclusions/exclusions to get specific artifacts, the following command will for example only download the sources for the dependency with a specific artifactId:

mvn dependency:sources -DincludeArtifactIds=guava

Maven Micro-Tip:获取源代码和 Javadocs

当您在 IDE 中使用 Maven 时,您经常会发现您的 IDE 需要为您的库依赖项解析源代码和 Javadoc。有一个简单的方法可以实现这个目标。

mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc

第一个命令将尝试下载 pom 文件中每个依赖项的源代码。

第二个命令将尝试下载 Javadoc。

Maven 在这里受库打包者的支配。所以他们中的一些人没有打包的源代码,他们中的许多人没有Javadocs。

如果您有很多依赖项,使用包含/排除项来获取特定工件也是一个好主意,例如,以下命令将仅下载具有特定工件 ID 的依赖项的源:

mvn dependency:sources -DincludeArtifactIds=guava

Source: http://tedwise.com/2010/01/27/maven-micro-tip-get-sources-and-javadocs/

资料来源:http: //tedwise.com/2010/01/27/maven-micro-tip-get-sources-and-javadocs/

Documentation: https://maven.apache.org/plugins/maven-dependency-plugin/sources-mojo.html

文档:https: //maven.apache.org/plugins/maven-dependency-plugin/sources-mojo.html

回答by Bozho

You can, if they are uploaded. Generally they are called "frameworkname-version-source(s)"

你可以,如果它们被上传。通常它们被称为“框架名称-版本-源(s)”

回答by Stefan De Boey

you can find info in this related question: Get source jar files attached to Eclipse for Maven-managed dependencies
if you use the eclipse maven plugin then use 'mvn eclipse:eclipse -DdownloadSources=true'

您可以在此相关问题中找到信息:Get source jar files attach to Eclipse for Maven-managed dependencies
if you use the eclipse maven plugin then use 'mvn eclipse:eclipse -DdownloadSources=true'

回答by Pascal Thivent

If a project creates a jar of the project sources and deploys it to a maven repository, then you'll find it :)

如果一个项目创建了一个项目源的 jar 并将其部署到 Maven 存储库,那么您会找到它:)

Just FYI, sources artifacts are generally created by the maven-source-plugin. This plugin can bundle the main or test sources of a project into a jar archive and, as explained in Configuring Source Plugin:

仅供参考,源工件通常由maven-source-plugin 创建。此插件可以将项目的主要或测试源捆绑到 jar 存档中,并且如配置源插件中所述

(...) The generated jar file will be named by the value of the finalNameplus "-sources" if it is the main sources. Otherwise, it would be finalNameplus "-test-sources" if it is the test sources.

(...) 生成的jar文件finalName如果是主源,会以加“-sources”的值命名。否则,finalName如果它是测试源,它将加上“-test-sources”。

The additional text was given to describe an artifact ("-sources" or "-test-sources" here) is called a classifier.

附加文本用于描述工件(此处为“-sources”或“-test-sources”)称为分类器。

To declare a dependency on an artifact that uses a classifier, simply add the <classifier>element. For example:

要声明对使用分类器的工件的依赖关系,只需添加<classifier>元素。例如:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate</artifactId>
  <version>3.2.7.ga</version>
  <classifier>sources</classifier>
</dependency>

Note that you generally don't do this, most IDEs provide support to download sources (and/or JavaDoc) from the main artifact without declaring explicitly a dependency on them.

请注意,您通常不会这样做,大多数 IDE 都支持从主工件下载源(和/或 JavaDoc),而无需明确声明对它们的依赖。

Finally, also note that some repository search engines allow searching for artifacts using the classifier (at least Nexus does with the advanced search). See this searchfor example.

最后,还要注意一些存储库搜索引擎允许使用分类器搜索工件(至少 Nexus 可以使用高级搜索)。例如,请参阅此搜索

回答by dev_doctor

The maven idea pluginfor IntelliJ Idea allows you to specify whether or not sources and java doc should be resolved and downloaded

IntelliJ Idea的maven idea 插件允许您指定是否应解析和下载源和 java 文档

mvn idea:idea -DdownloadSources=true -DdownloadJavadocs=true

回答by Gabriel Ramirez

if you're using eclipse you could also open Preferences > Maven and select Download Artifact Sources, this would let the pom.xml intact and keep your sources or java docs (if selected) just for development right at your machine location ~/.m2

如果您使用的是 eclipse,您还可以打开 Preferences > Maven 并选择 Download Artifact Sources,这将使 pom.xml 完好无损并保留您的源代码或 java 文档(如果选择),仅用于在您的机器位置进行开发 ~/.m2

回答by RobAu

Configuring and running the maven-eclipse plugin, (for example from the command line mvn eclipse:eclipse)

配置和运行 maven-eclipse 插件,(例如从命令行mvn eclipse:eclipse

   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
        </plugins>
    </build>

回答by eeezyy

I have also used the eclipse plugin to get the project into the eclipse workspace. Since I've worked on a different project I saw that it is possible to work with eclipse but without the maven-eclipse-plugin. That makes it easier to use with different environments and enables the easy use of maven over eclipse. And that without changing the pom.xml-file.

我还使用 eclipse 插件将项目放入 eclipse 工作区。由于我从事过一个不同的项目,我看到可以使用 eclipse 但没有 maven-eclipse-plugin。这使得它更容易在不同的环境中使用,并且可以在 Eclipse 上轻松使用 maven。并且无需更改 pom.xml 文件。

So, I recommend the approach of Gabriel Ramirez.

所以,我推荐 Gabriel Ramirez 的方法。

回答by eebbesen

Based on watching the Maven console in Eclipse (Kepler), sources will be automatically downloaded for a Maven dependency if you attempt to open a class from said Maven dependency in the editor for which you do not have the sources downloaded already. This is handy when you don't want to grab source for all of your dependencies, but you don't know which ones you want ahead of time (and you're using Eclipse).

基于在 Eclipse (Kepler) 中观察 Maven 控制台,如果您尝试在编辑器中打开来自所述 Maven 依赖项的类,而您尚未下载源代码,则将自动下载 Maven 依赖项的源代码。当您不想获取所有依赖项的源代码,但又不知道要提前获取哪些依赖项(并且您正在使用 Eclipse)时,这很方便。

I ended up using @GabrielRamierez's approach, but will employ @PascalThivent's approachgoing forward.

我最终使用了@GabrielRamierez 的方法,但将采用@PascalThivent 的方法

回答by Anver Sadhat

To download some specific source or javadocwe need to include the GroupIds - Its a comma separated value as shown below

要下载一些特定的源或 javadoc,我们需要包含 GroupIds - 它是一个逗号分隔的值,如下所示

mvn dependency:sources -DincludeGroupIds=com.jcraft,org.testng -Dclassifier=sources

Note that the classifier are not comma separated, to download the javadoc we need to run the above command one more time with the classifier as javadoc

请注意,分类器不是逗号分隔的,要下载 javadoc,我们需要将分类器作为 javadoc 再运行一次上述命令

mvn dependency:sources -DincludeGroupIds=com.jcraft,org.testng -Dclassifier=javadoc