473

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

MozenRath
  • 9,652
  • 13
  • 61
  • 104
Ioan Alexandru Cucu
  • 11,981
  • 6
  • 37
  • 39
  • In Idea right clicking on the dependency in the Maven tools window does the magic. http://czak.pl/images/posts/gwt-with-idea-ce/download-sources.png – Gayan Weerakutti Dec 16 '16 at 12:07

20 Answers20

842

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

hfhc2
  • 4,182
  • 2
  • 27
  • 56
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • 12
    Where does it put them once they're downloaded? – James Moore Jul 18 '12 at 05:08
  • 20
    It puts them in the same directory as the binary JARs under M2_HOME. – Alain O'Dea Jul 18 '12 at 18:52
  • 20
    If you're using eclipse, it may be helpful to also do "mvn eclipse:eclipse" afterwards and then refresh your project in eclipse--saves you from manually attaching sources to each file. – unigeek Apr 03 '14 at 15:06
  • 1
    I can't find the sources and javadocs after they are downloaded. I've checked every directory under M2_HOME – Patrick Kiernan Apr 08 '14 at 14:43
  • 1
    How about the sources and docs for `javaee-(web-)api`? You have to do something extra to get those, right? But what? – Lii Aug 13 '14 at 13:16
  • 1
    @Lii it appears that javaee-(web-)api dependencies exclude source by design. That is because they are container-provided interfaces. Practically speaking the source depends on the runtime container chosen. https://java.net/jira/browse/GLASSFISH-11389 – Alain O'Dea Mar 01 '15 at 20:42
  • if you have eclipse and the maven plugin you can accomplish this form the right click menu as well. – owen gerig Aug 03 '15 at 16:52
  • 2
    After `mvn dependency:resolve -Dclassifier=javadoc` IntelliJ IDEA automatically notices the javadocs, which is very nice. – Jonik Nov 05 '15 at 14:44
  • 1
    Where `mvn dependency:sources` is shorthand for `mvn dependency:resolve -Dclassifier=sources`. You equally may use `mvn dependency:sources -Dclassifier=javadoc`. – Ed Randall Oct 10 '18 at 07:19
96

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>
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
  • What do you mean? Do you mean that sources and docs are downloaded when you run `mvn eclipse:eclipse` if you add this to the POM file? – Lii Aug 13 '14 at 12:53
  • @RobAu ,where will the source be downloaded, will it be attached to the project it self or somewhere else? – JaskeyLam Oct 11 '14 at 16:45
  • 1
    It will be downloaded as jar in your `M2_REPO` as a xxxx-yy-sources.jar, at the same location as the normal jar. It will get attached as source code for the xxxx-yy jar in the libraries – Rob Audenaerde Oct 13 '14 at 07:20
  • If you a netbeans user, go to menu 'tools', 'options', 'java', so access the 'Execution' level and fill the input 'Global Execution Options' with 'eclipse:eclipse' quoted by @RobAu – deldev Apr 10 '15 at 18:51
  • @RobAu Excellent answer. No need to run any command. Just adding this plugin will solve everything. But, why this didn't download **Spring Docs** as that of **Hibernate Docs** ? – OO7 May 15 '15 at 08:47
  • @OO7 It depends on the release procedure of the libraries. They can decide not to package sources or package the source in a non-standard way. – Rob Audenaerde Mar 03 '16 at 07:52
  • After mvn eclipse:eclipse Will restart of eclipse will automatically point to these source code ..? – Kanagavelu Sugumar Nov 21 '17 at 10:04
  • 1
    @KanagaveluSugumar you need to add the M2_REPO to eclipse, and then just refresh the project. – Rob Audenaerde Nov 21 '17 at 10:17
37

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.

Yoon5oo
  • 496
  • 5
  • 11
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
19

To download any artifact use

mvn dependency:get -Dartifact=groupId:artifactId:version:packaging:classifier

For Groovy sources this would be

mvn dependency:get -Dartifact=org.codehaus.groovy:groovy-all:2.4.6:jar:sources

For Groovy's javadoc you would use

mvn dependency:get -Dartifact=org.codehaus.groovy:groovy-all:2.4.6:jar:javadoc

This puts the given artifact into your local Maven repository, i.e. usually $HOME/.m2/repository.

dependency:sources just downloads the project dependencies' sources, not the plugins sources nor the sources of dependencies defined inside plugins.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Maddin
  • 957
  • 1
  • 11
  • 21
19

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

mvn idea:idea -DdownloadSources=true -DdownloadJavadocs=true
dev_doctor
  • 722
  • 6
  • 10
  • Instead of downloading sources for all dependencies, is it possible to download just for some dependencies by specifying it in pom along with dependency itself, may b via some inner tag in dependency tag or something similar? – nanosoft Jul 06 '15 at 07:55
  • This Plugin is Retired – kiltek Mar 12 '20 at 09:10
  • But, @kiltek the functionality is now built into the Maven UI in Intellij... – cptully Jul 01 '21 at 21:21
18

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

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

mvn dependency:sources -DincludeGroupIds=com.jcraft,org.testng -Dclassifier=javadoc
Anver Sadhat
  • 3,074
  • 1
  • 25
  • 26
  • Instead of downloading sources for all dependencies, is it possible to download just for some dependencies by specifying it in pom along with dependency itself, may b via some inner tag in dependency tag or something similar? – nanosoft Jul 06 '15 at 07:55
12

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'

Community
  • 1
  • 1
Stefan De Boey
  • 2,344
  • 16
  • 14
  • This is the only solution that worked for me. I am using Eclipse Kepler. Since this makes proper references in the **.classpath**. Like the following: – aspdeepak Oct 23 '14 at 13:45
  • When I followed @ RobAu answer only *Hibernate Docs* are downloaded from repo, not *Spring*. Then I visit the link u have provided & followed @ mrembisz & make changes to *Maven preference* & works like a charm. I get *Spring Docs*. @Stefan De Boey Thanks for sharing this. +1. – OO7 May 15 '15 at 09:15
9

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

Gabriel Ramirez
  • 131
  • 2
  • 4
8

In Eclipse

  1. Right click on the pom.xml
  2. Select Run As -> Maven generate-sources
    it will generate the source by default in .m2 folder

Pre-Requisite:

Maven should be configured with Eclipse.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
bondkn
  • 420
  • 5
  • 11
5

In eclipse - click on the project then:

enter image description here.

Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67
  • I unchecked 'download artifact sources' at eclipse maven setting, so it only download the jar. But latter, I tried to use this solution to download source, it failed without any message. I removed the jar folder in maven local repository and download the jar again, then the source jar downloaded success. I think this is because maven local repository has have the jar, so I cannot use this method to download the source. – frank Dec 20 '17 at 13:59
  • Similarly when you right click a Maven dependency and select Maven\Download Sources (or Javadoc), you can download only for a single dependency. – Cagin Uludamar Dec 26 '22 at 08:03
4

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
4

NetBeans, Context-Click

In NetBeans 8 with a Maven-driven project, merely context-click on the jar file list item of the dependency in which you are interested. Choose Download Sources. Wait a moment and NetBeans will automatically download and install the source code, if available.

Similarly you can choose Download Javadoc to get the doc locally installed. Then you can context-click some code in the editor and choose to see the JavaDoc.

screen shot of context-menu item "Download Sources" being chosen in a NetBeans 8 project driven by Maven

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3

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

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

Community
  • 1
  • 1
eebbesen
  • 5,070
  • 8
  • 48
  • 70
2

If you know the groupId and aritifactId,you can generate download url like this.

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

http://central.maven.org/maven2/ch/qos/logback/logback-classic/

and you will get a page like this, chose the version you need,just enjoy it! enter image description here

enter image description here

senninha
  • 96
  • 3
1

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.

So, I recommend the approach of Gabriel Ramirez.

eeezyy
  • 2,109
  • 1
  • 15
  • 18
1

Maven repositories do provide simple way to download sources jar.

I will explain it using a demonstration for "spring-boot-actuator-autoconfigure".

  1. Go to maven repository - https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator-autoconfigure
  2. The page lists various versions. Click-on to desired one, let's say, 2.1.6.RELEASE - https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator-autoconfigure/2.1.6.RELEASE
  3. The page have link "View All" next to "Files". Click it - https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-actuator-autoconfigure/2.1.6.RELEASE/
  4. The page lists various files including the one for sources - https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-actuator-autoconfigure/2.1.6.RELEASE/spring-boot-actuator-autoconfigure-2.1.6.RELEASE-sources.jar

Otherwise, you can always "git clone" the repo from github, if its there and get the specific code.

As explained by others, you can use "mvn dependency:sources" command the get and generate sources jar for the dependency you are using.

Note: Some dependencies will not have sources.jar, as those contains no source code but a pom file. e.g. spring-boot-starter-actuator. As in this case:

Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors.

Reference: Intro to Spring Boot Starters

Manojkumar Khotele
  • 963
  • 11
  • 25
1

In IntelliJ IDEA you can download artifact sources automatically while importing by switching on Automatically download Sources option:

SettingsBuild, Execution, DeploymentBuild ToolsMavenImporting

enter image description here

Saikat
  • 14,222
  • 20
  • 104
  • 125
1

If you want find the source jar file for any of the artifact manually, go to maven repository location for the particular artifact and in Files click on 'view All'. You can find source jar file.

0

For debugging you can also use a "Java Decompiler" such as: JAD and decompile source on the fly (although the generated source is never the same as the original). Then install JAD as a plugin in Eclipse or your favorite IDE.

atom88
  • 1,449
  • 3
  • 22
  • 32
0

If you're using Eclipse, I would recommend downloading both the source and the Javadocs of third-party libraries.

Right click on project and download both as per the screenshot below.

Downloading Javadocs means that typically you can get contextual help for methods from third-party libraries, with useful description of parameters, etc. This is essential if you don't know the library well. In some cases I have found that Javadocs are available when the source isn't.

enter image description here

DAB
  • 1,631
  • 19
  • 25