Get source JARs from Maven repository

JavaMavenMvn Repo

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

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

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.

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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.

Solution 7 - Java

you can find info in this related question: https://stackoverflow.com/questions/310720/get-source-jar-files-attached-to-eclipse-for-maven-managed-dependencies">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'

Solution 8 - Java

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

Solution 9 - Java

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.

Solution 10 - Java

In eclipse - click on the project then:

enter image description here.

Solution 11 - Java

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

Solution 12 - Java

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

Solution 13 - Java

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.

Solution 14 - Java

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

Solution 15 - Java

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.

Solution 16 - Java

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

Solution 17 - Java

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

Solution 18 - Java

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.

Solution 19 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIoan Alexandru CucuView Question on Stackoverflow
Solution 1 - JavaAlain O'DeaView Answer on Stackoverflow
Solution 2 - JavaRob AudenaerdeView Answer on Stackoverflow
Solution 3 - JavaPascal ThiventView Answer on Stackoverflow
Solution 4 - Javadev_doctorView Answer on Stackoverflow
Solution 5 - JavaAnver SadhatView Answer on Stackoverflow
Solution 6 - JavaMaddinView Answer on Stackoverflow
Solution 7 - JavaStefan De BoeyView Answer on Stackoverflow
Solution 8 - JavaGabriel RamirezView Answer on Stackoverflow
Solution 9 - JavabondknView Answer on Stackoverflow
Solution 10 - JavaWitold KaczurbaView Answer on Stackoverflow
Solution 11 - JavaBozhoView Answer on Stackoverflow
Solution 12 - JavaBasil BourqueView Answer on Stackoverflow
Solution 13 - JavaeebbesenView Answer on Stackoverflow
Solution 14 - JavasenninhaView Answer on Stackoverflow
Solution 15 - JavaeeezyyView Answer on Stackoverflow
Solution 16 - JavaManojkumar KhoteleView Answer on Stackoverflow
Solution 17 - JavaSaikatView Answer on Stackoverflow
Solution 18 - JavaNaresh MuthyalaView Answer on Stackoverflow
Solution 19 - Javaatom88View Answer on Stackoverflow