Get source jar files attached to Eclipse for Maven-managed dependencies

EclipseMaven 2Javadoc

Eclipse Problem Overview


I am using Maven (and the Maven Eclipse Integration) to manage the dependencies for my Java projects in Eclipse. The automatic download feature for JAR files from the Maven repositories is a real time saver. Unfortunately, it does not include API documentation and source code.

How can I set up Maven to automatically also get the source and javadoc attachments and register them properly with Eclipse?

Eclipse Solutions


Solution 1 - Eclipse

I am sure m2eclipse Maven plugin for Eclipse - the other way around - can do that. You can configure it to download both the source files and javadoc automatically for you.

This is achieved by going into Window > Preferences > Maven and checking the "Download Artifact Sources" and "Download Artifact JavaDoc" options.

![Screenshot of Maven Preferences][1]

[1]: http://i.stack.imgur.com/CNUBh.png "Maven Preferences"

Solution 2 - Eclipse

mvn eclipse:eclipse -DdownloadSources=true

or

mvn eclipse:eclipse -DdownloadJavadocs=true

or you can add both flags, as Spencer K points out.

Additionally, the =true portion is not required, so you can use

mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs

Solution 3 - Eclipse

The other answers on this work, but if you want to avoid having to remember command line arguments, you can also just add to the downloadSources and downloadJavadocs config to the maven-eclipse-plugin section of your pom.xml:

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                    ... other stuff ...
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>

Solution 4 - Eclipse

I prefer not to put source/Javadoc download settings into the project pom.xml file as I feel these are user preferences, not project properties. Instead, I place them in a profile in my settings.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <profiles>
    <profile>
      <id>sources-and-javadocs</id>
      <properties>
        <downloadSources>true</downloadSources>
        <downloadJavadocs>true</downloadJavadocs>
      </properties>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>sources-and-javadocs</activeProfile>
  </activeProfiles>
</settings>

Solution 5 - Eclipse

Right click on project -> maven -> download sources

Solution 6 - Eclipse

If the source jars are in the local repository and you are using Eclipses maven support the sources are getting automatically attached. You can run mvn dependency:sources to download all source jars for a given project. Not sure how to do the same with the documentation though.

Solution 7 - Eclipse

If you are using meclipse do

window --> maven --> Download Artifact Sources (select check)

(If you still get attach source window, then click on attach file button and close the attach source window. The next time you try to see the source it will open the correct source)

Solution 8 - Eclipse

There is also a similiar question that answers this and includes example pom settings.

Solution 9 - Eclipse

I tried windows->pref..->Maven But it was not working out. Hence I created a new class path with command mvn eclipse:eclipse -DdownloadSources=true and refreshed the workspace once. voila.. Sources were attached.

Source jar's entry is available in class path. Hence new build solved the problem...

Solution 10 - Eclipse

in my version of Eclipse helios with m2Eclipse there is no

window --> maven --> Download Artifact Sources (select check)

Under window is only "new window", "new editor" "open perspective" etc.

If you right click on your project, then chose maven--> download sources

Nothing happens. no sources get downloaded, no pom files get updated, no window pops up asking which sources.

Doing mvn xxx outside of eclipse is dangerous - some commands dont work with m2ecilpse - I did that once and lost the entire project, had to reinstall eclipse and start from scratch.

Im still looking for a way to get ecilpse and maven to find and use the source of external jars like servlet-api.

Solution 11 - Eclipse

Changing pom for maven-eclipse-plugin to include source/javadoc just apply for new dependencies being added to pom. If we need to apply for existing dependencies, we must run mvn dependency:sources. I checked this.

Solution 12 - Eclipse

Checking download source/javadoc in Eclipse-Maven preference, sometimes is not enough. In the event maven failed to download them for some reason (a network blackout?), maven creates some *.lastUpdated files, then will never download again. My empirical solution was to delete the artifact directory from .m2/repository, and restart the eclipse workspace with download source/javadoc checked and update projects at startup checked as well. After the workspace has been restarted, maybe some projects can be marked in error, while eclipse progress is downloading, then any error will be cleared. Maybe this procedure is not so "scientific", but for me did succeded.

Solution 13 - Eclipse

I've added the pom configuration to the maven-eclipse plugin to download source and javadocs, but I figure/hope that will happen for new dependencies, not existing ones.

For existing dependencies, I browsed in package explorer down to the "Maven Dependencies" and right-clicked on commons-lang-2.5.jar, selected Maven | Download Sources and... nothing appeared to happen (no progress bar or indication that it was doing anything). It did, however, download as I'm able to jump to source in commons-lang now.

Solution 14 - Eclipse

overthink suggested using the setup in the pom:

<project>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
                ... other stuff ...
            </configuration>
        </plugin>
    </plgins>
</build>
...

First i thought this still won't attach the javadoc and sources (as i tried unsuccessfully with that -DdownloadSources option before).

But surprise - the .classpath file IS getting its sources and javadoc attached when using the POM variant!

Solution 15 - Eclipse

For Indigo (and probably Helios) the checkboxes mentioned above are located here:

Window -> Preferences -> Maven

Solution 16 - Eclipse

I had a similar problem, and the solution that worked best for me was to include the source in the same jar as the compiled code (so a given directory in the jar would include both Foo.java and Foo.class). Eclipse automatically associates the source with the compiled code, and automatically provides the JavaDoc from the source. Obviously, that's only helpful if you control the artifact.

Solution 17 - Eclipse

After Setting the Properties either at Project Level or User Properties level, Please do a Maven -> Update Project (Force Update). It downloads the sources

Solution 18 - Eclipse

A Small addition to the answer, if your project is not a maven project still you can get the source code of the jars, by using this plugin provided in eclipse Java Source Attacher

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
QuestionThiloView Question on Stackoverflow
Solution 1 - EclipsemrembiszView Answer on Stackoverflow
Solution 2 - EclipseStephen DenneView Answer on Stackoverflow
Solution 3 - EclipseoverthinkView Answer on Stackoverflow
Solution 4 - EclipseDuncan JonesView Answer on Stackoverflow
Solution 5 - EclipseSumeetView Answer on Stackoverflow
Solution 6 - EclipseHardyView Answer on Stackoverflow
Solution 7 - EclipsesurajzView Answer on Stackoverflow
Solution 8 - EclipseandrelView Answer on Stackoverflow
Solution 9 - Eclipseom39aView Answer on Stackoverflow
Solution 10 - EclipsewingnutView Answer on Stackoverflow
Solution 11 - Eclipsebnguyen82View Answer on Stackoverflow
Solution 12 - Eclipseuser17417View Answer on Stackoverflow
Solution 13 - Eclipseunique72View Answer on Stackoverflow
Solution 14 - EclipsejhohlfeldView Answer on Stackoverflow
Solution 15 - EclipseChris RomineView Answer on Stackoverflow
Solution 16 - EclipseDan R.View Answer on Stackoverflow
Solution 17 - EclipsearkabhiView Answer on Stackoverflow
Solution 18 - EclipseBhaskara AraniView Answer on Stackoverflow