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

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?

This question is related to eclipse maven-2 javadoc

The answer is


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.


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)

Surajz


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.


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>

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.


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)

Surajz


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.


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!


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


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.


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>

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

Window -> Preferences -> Maven


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

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

Window -> Preferences -> Maven


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.


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>

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


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.


Right click on project -> maven -> download sources


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


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


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.


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!


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


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


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


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

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.


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


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.


Right click on project -> maven -> download sources


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.


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

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>

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to maven-2

Maven:Non-resolvable parent POM and 'parent.relativePath' points at wrong local POM Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project. Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved Maven Error: Could not find or load main class MAVEN_HOME, MVN_HOME or M2_HOME Where is my m2 folder on Mac OS X Mavericks Maven won't run my Project : Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 'mvn' is not recognized as an internal or external command, operable program or batch file SLF4J: Class path contains multiple SLF4J bindings Create local maven repository

Examples related to javadoc

/** and /* in Java Comments Autocompletion of @author in Intellij Javadoc link to method in other class How to generate javadoc comments in Android Studio What is the IntelliJ shortcut key to create a javadoc comment? Maven is not working in Java 8 when Javadoc tags are incomplete How to see JavaDoc in IntelliJ IDEA? How to attach source or JavaDoc in eclipse for any jar file e.g. JavaFX? Adding author name in Eclipse automatically to existing files How to download Javadoc to read offline?