如何下载一个罐与 Maven 源代码?

在我的项目中,我使用的是通过 Maven 提供的 JAR 文件。但 Maven 给我的只是这个 jar ——没有 javadocs,也没有来源。按下“下载源”没有任何效果: Eclipse 仍然没有找到 jar 的源。

这取决于什么? 存储库应该自动提供源代码吗?

也许我需要用 POM 编写一些东西来指示 Maven 下载源代码?

我现在的诗是这样的:

<repositories>
<repository>
<id>xuggle repo</id>
<url>http://xuggle.googlecode.com/svn/trunk/repo/share/java/</url>
</repository>
</repositories>


<dependencies>


<dependency>
<groupId>xuggle</groupId>
<artifactId>xuggle-xuggler</artifactId>
<version>5.3</version>
<type>rar</type>
</dependency>


</dependencies>

为什么 Maven 没有说任何关于源代码下载失败的评论?

92842 次浏览

The source/javadoc jars may not have been provided and are not in the repository -- there is nothing that requires a source/javadoc jar to be present.

2020 Update:

The maven dependency plugin should be used whit the dependency:sources goal:

  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>download-sources</id>
<goals>
<goal>sources</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>

This can also be run from the command line as:

mvn dependency:sources -Dsilent=true

Executing mvn dependency:sources will force maven to download all sources of all jars in the project, if the sources are available (are uploaded in the repository where the artifact is hosted). If you want to download javadoc the command is mvn dependency:resolve -Dclassifier=javadoc

Deprecated:

It's also possible to create a profile in your settings.xml file and include the following properties:
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc

if it does not have sources it should say something like

[INFO] The following files have NOT been resolved:
[INFO]    com.oracle:ojdbc6:java-source:sources:12.1.0.1
[INFO]    javax:javaee-api:java-source:sources:6.0

extending on @hovanessyan answer.

A basic profile for enabling the downloadSources and downloadJavadocs, in Maven's settings.xml will look like this. e.g. profile id is downloadSources

<!-- add the profile under profiles section -->


<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>


<!-- activate the profile under activeProfiles section -->


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

In eclipse, rigth click on your project then Maven>Download Sources again Maven>Download Javadoc

It is best not to rely on the Eclipse plugin as it is deprecated. Using the downloadSources and downloadJavadocs properties did not work for me. The answer posted above regarding the use of the dependencies plugin word. However, you may wish to automatically download sources and javadocs. Furthermore you may wish to always create a source jar and a javadoc jar. Put this in the pom of your project. If you use modules, put in your parent pom.

<build>
<plugins>
<!-- download sources and javadoc -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>download-sources</id>
<goals>
<goal>sources</goal>
</goals>
</execution>
<execution>
<id>download-javadoc</id>
<configuration>
<classifier>javadoc</classifier>
</configuration>
<goals>
<goal>resolve</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Always create javadoc jar. -->
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadoc</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Always create source jar. -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

You can also download sources under target/dependencies using:

mvn -Dclassifier=sources dependency:copy-dependencies