ChatGPT解决这个技术问题 Extra ChatGPT

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?

Related question asking how to always download sources and javadoc when using maven: stackoverflow.com/questions/5780758/…

S
Sathiamoorthy

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.

https://i.stack.imgur.com/CNUBh.png


Yes, I just started using m2eclipse, and it does take car of this (and many other things maven).
Neither of these replies are answers. Why are they being voted up? I was able to download the sources by control-clicking the project in Navigator view and choosing "m2 Maven"-->"Download Sources". I was then able to command-click (mac) on the class to navigate to the linked source.
It seems that if the compiled JAR has already been downloaded, Eclipse won't try to get the source/Javadoc JARs immediately. It appears that modifying a POM containing a dependency on the POMs for which I want the source/Javadoc in Eclipse and saving the POM downloads the source/Javadoc for the dependencies, even if the compiled JAR is already in the Maven repository cache.
@Francis - After making the above change right click on your project and select "Maven > Update Project ..." This will cause your source jars to download.
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?
S
Stephen Denne
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

or both! mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true This only works if the source and javadocs were provided to the repo you're downloading from. Sometimes they aren't provided like Spring.
Spring does now provide them.
Note that this answer is more than 10 years old. Use the m2e plugin in eclipse instead of having maven create eclipse configuration files.
o
overthink

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>

would i be able to set this at a per-dependency level?
@marcorossi Sorry, I'm not sure if that is possible.
I'm not convinced this is appropriate. Surely source and Javadoc download settings are a user preference, not a project-level concern? See my answer below for how to place this in the settings.xml file.
I got this warning: [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-eclipse-plugin is missing. To fix that you can add something like this below : 2.9
D
Duncan Jones

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 copied this settings.xml in .m2 however sources and javadocs are not getting copied. The following command explicitly on the CLI brings over the sources and javadocs though: mvn dependency:sources dependency:resolve -Dclassifier=javadoc Any thoughts ?
Maybe this works, but please explain, how is the maven eclipse plugin supposed to find these configuration settings? You think it looks for global properties?
S
Sathiamoorthy

Right click on project -> maven -> download sources


Everyone said how to enable it not how to get maven to download the sources! after enabling the option
H
Hardy

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.


This worked for me. After running mvn dependency:sources, I was able to click through in Eclipse to read source. No additional mucking with m2e required. To download the javadocs you do mvn dependency:sources -Dclassifier=javadoc.
S
Sathiamoorthy

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)


C
Community

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


S
Sathiamoorthy

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


With your solution, I can pick and chose which module I want to get sources for. Simply cd into that module and build with "mvn eclipse:clean eclipse:eclipse -DdownloadSources=true -T4 -Dmaven.test.skip=true -Denforcer.skip=true". Thanks :)
S
Sathiamoorthy

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.


b
bnguyen82

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.


u
user17417

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.


u
unique72

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.


j
jhohlfeld

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!


S
Sathiamoorthy

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

Window -> Preferences -> Maven

D
Dan R.

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.


a
arkabhi

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


B
Bhaskara Arani

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