score:229

Accepted answer

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

score:0

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.

score:0

after setting the properties either at project level or user properties level, please do a maven -> update project (force update). it downloads the sources

score:0

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

score:1

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.

score:1

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!

score:1

for indigo (and probably helios) the checkboxes mentioned above are located here:

window -> preferences -> maven

score:2

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.

score:2

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.

score:3

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

score:3

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.

score:3

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

score:4

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)

score:8

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.

score:9

right click on project -> maven -> download sources

score:16

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>

score:72

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>

score:130

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

Related Query

More Query from same tag