89

In my project I am using a JAR file provided via Maven. But what Maven gives me is only this jar - no javadocs and no sources. Pressing "Download Sources" has no effect: Eclipse still does not find the sources of the jar.

What this depends on? Should repository provide sources automatically?

May be I need to write something in POM to instruct Maven to download sources?

My current pom follows:

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

Why Maven does not say any comments on it's sources download fail?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

7 Answers7

152

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>
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • But if there are no sources in repository Maven will just do nothing silently, right? No errors, no warnings? – Suzan Cioc Jul 06 '12 at 11:46
  • yes, I suppose so, at least if it's not running in some very verbose/debug mode. Maybe it's possible to run it in debug/verbose mode and grep for the specific messages. – hovanessyan Jul 06 '12 at 11:49
  • I added an answer to extend on the adding of these properties to the maven settings.xml – RaamEE Aug 10 '14 at 07:10
  • 2
    @hovanessyan - Can we add some setting which pulls jars only for some of the dependencies rather than all of them ? – MasterJoe Jul 26 '18 at 17:24
  • can these properties be part of project properties? – Raja Anbazhagan Aug 06 '18 at 12:12
  • 1
    These properties are for the deprecated Eclipse Maven Plugin (https://maven.apache.org/plugins/maven-eclipse-plugin/eclipse-mojo.html#downloadJavadocs). They won't do anything if you're not using that plugin. – AndrewBourgeois Aug 12 '18 at 18:41
  • the xml snippet etc. etc. put inside my settings.xml and against a profile has not worked for me - this might allude to what @AndrewBourgeois was trying to explain – user1561783 Feb 13 '20 at 13:27
  • Note: The first paragraph under **Deprecated** is still valid and should not be struckthrough, but the edit queue is currently full. – Leponzo Feb 09 '22 at 13:04
  • @Leponzo done - I've removed the strikethrough – hovanessyan Feb 09 '22 at 13:20
  • @hovanessyan, thanks, but the second paragraph is still deprecated and can be struck through, while the first paragraph can additionally be moved out of the Deprecated section. – Leponzo Feb 09 '22 at 13:33
  • 1
    @Leponzo got it, thanks. – hovanessyan Feb 09 '22 at 15:15
63
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
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
15

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>
Arjan
  • 823
  • 1
  • 7
  • 18
Douglass Parker
  • 181
  • 1
  • 3
7

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

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
6

You can also download sources under target/dependencies using:

mvn -Dclassifier=sources dependency:copy-dependencies
Marinos An
  • 9,481
  • 6
  • 63
  • 96
4

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.

kevinpeterson
  • 1,150
  • 9
  • 15
4

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>
RaamEE
  • 3,017
  • 4
  • 33
  • 53