Get source JARs from Maven repository

Solution 1:

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

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

Solution 2:

Configuring and running the maven-eclipse plugin, (for example from the command line 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>

Solution 3:

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

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:

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

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

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

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

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 search for example.