获取附加到Eclipse的源jar文件以获取Maven管理的依赖项

时间:2023-02-04 16:22:54

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.

我正在使用Maven(以及Maven Eclipse Integration)来管理Eclipse中Java项目的依赖项。来自Maven存储库的JAR文件的自动下载功能可以节省大量时间。不幸的是,它不包括API文档和源代码。

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

如何设置Maven以自动获取源和javadoc附件并使用Eclipse正确注册它们?

17 个解决方案

#1


213  

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.

我相信Eclipse的m2eclipse Maven插件 - 反之亦然 - 可以做到这一点。您可以将其配置为自动下载源文件和javadoc。

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

这可以通过进入Window> Preferences> Maven并检查“Download Artifact Sources”和“Download Artifact JavaDoc”选项来实现。

获取附加到Eclipse的源jar文件以获取Maven管理的依赖项

#2


122  

mvn eclipse:eclipse -DdownloadSources=true

or

mvn eclipse:eclipse -DdownloadJavadocs=true

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

或者你可以添加两个标志,正如Spencer K指出的那样。

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

此外,= true部分不是必需的,因此您可以使用

mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs

#3


71  

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:

关于这项工作的其他答案,但如果你想避免记住命令行参数,你也可以添加到downloadSources并将downloadJavadocs配置下载到你的pom.xml的maven-eclipse-plugin部分:

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

#4


15  

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:

我不想将源/ Javadoc下载设置放入项目pom.xml文件中,因为我觉得这些是用户首选项,而不是项目属性。相反,我将它们放在我的settings.xml文件中的配置文件中:

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

#5


7  

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.

如果源jar位于本地存储库中并且您正在使用Eclipses maven支持,则源会自动附加。您可以运行mvn dependency:sources来下载给定项目的所有源jar。不知道如何对文档做同样的事情。

#6


7  

Right click on project -> maven -> download sources

右键单击项目 - > maven - >下载源代码

#7


3  

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

还有一个类似的问题可以解决这个问题并包含示例pom设置。

#8


3  

If you are using meclipse do

如果你正在使用meclipse吗

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

window - > maven - >下载工件源(选择检查)

(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

#9


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.

我试过windows-> pref ..-> Maven但它没有用完。因此,我使用命令mvn eclipse创建了一个新的类路径:eclipse -DdownloadSources = true并刷新工作区一次。瞧..来源是附加的。

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

源jar的条目在类路径中可用。因此,新版本解决了这个问题......

#10


2  

in my version of Eclipse helios with m2Eclipse there is no

在我的版本的Eclipse helios与m2Eclipse没有

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

如果右键单击您的项目,则选择maven - > download sources

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

什么都没发生。没有源下载,没有pom文件更新,没有窗口弹出询问哪些来源。

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.

在eclipse之外做mvn xxx是危险的 - 有些命令不适用于m2ecilpse - 我曾经做过一次并丢失了整个项目,不得不重新安装eclipse并从头开始。

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

我仍在寻找一种方法来获取ecilpse和maven来查找和使用像servlet-api这样的外部罐子的来源。

#11


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.

改变maven-eclipse-plugin的pom以包含source / javadoc只是申请添加到pom的新依赖项。如果我们需要申请现有的依赖项,我们必须运行mvn依赖项:sources。我查了一下。

#12


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.

在Eclipse-Maven偏好中检查下载源/ javadoc,有时是不够的。如果由于某种原因maven无法下载它们(网络中断?),maven会创建一些* .lastUpdated文件,然后再也不会下载。我的经验解决方案是从.m2 / repository中删除工件目录,并在检查下载源/ javadoc时重新启动eclipse工作区,并在启动时更新项目。重新启动工作空间后,可能会错误地标记某些项目,同时下载eclipse进度,然后将清除任何错误。也许这个程序不是那么“科学”,但对我来说确实成功了。

#13


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.

我已经将pom配置添加到maven-eclipse插件中以下载源代码和javadocs,但我想/希望新的依赖项会发生,而不是现有的依赖项。

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.

对于现有的依赖项,我在package explorer中浏览了“Maven Dependencies”并右键单击了commons-lang-2.5.jar,选择了Maven |下载源和...似乎没有任何事情发生(没有进度条或表明它正在做任何事情)。然而,它确实下载了,因为我现在可以跳到commons-lang来源。

#14


1  

overthink suggested using the setup in the pom:

过度建议使用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).

首先我认为这仍然不会附加javadoc和源代码(因为我之前尝试过使用-DdownloadSources选项失败)。

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

但令人惊讶的是 - .classpath文件在使用POM变体时获取其源和javadoc!

#15


1  

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

对于Indigo(可能还有Helios),上面提到的复选框位于:

Window -> Preferences -> Maven

窗口 - >首选项 - > Maven

#16


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.

我遇到了类似的问题,最适合我的解决方案是将源包含在与编译代码相同的jar中(因此jar中的给定目录将包括Foo.java和Foo.class)。 Eclipse自动将源与已编译的代码相关联,并自动从源提供JavaDoc。显然,只有控制工件才有用。

#17


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

在项目级别或用户属性级别设置属性后,请执行Maven - >更新项目(强制更新)。它下载源

#1


213  

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.

我相信Eclipse的m2eclipse Maven插件 - 反之亦然 - 可以做到这一点。您可以将其配置为自动下载源文件和javadoc。

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

这可以通过进入Window> Preferences> Maven并检查“Download Artifact Sources”和“Download Artifact JavaDoc”选项来实现。

获取附加到Eclipse的源jar文件以获取Maven管理的依赖项

#2


122  

mvn eclipse:eclipse -DdownloadSources=true

or

mvn eclipse:eclipse -DdownloadJavadocs=true

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

或者你可以添加两个标志,正如Spencer K指出的那样。

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

此外,= true部分不是必需的,因此您可以使用

mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs

#3


71  

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:

关于这项工作的其他答案,但如果你想避免记住命令行参数,你也可以添加到downloadSources并将downloadJavadocs配置下载到你的pom.xml的maven-eclipse-plugin部分:

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

#4


15  

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:

我不想将源/ Javadoc下载设置放入项目pom.xml文件中,因为我觉得这些是用户首选项,而不是项目属性。相反,我将它们放在我的settings.xml文件中的配置文件中:

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

#5


7  

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.

如果源jar位于本地存储库中并且您正在使用Eclipses maven支持,则源会自动附加。您可以运行mvn dependency:sources来下载给定项目的所有源jar。不知道如何对文档做同样的事情。

#6


7  

Right click on project -> maven -> download sources

右键单击项目 - > maven - >下载源代码

#7


3  

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

还有一个类似的问题可以解决这个问题并包含示例pom设置。

#8


3  

If you are using meclipse do

如果你正在使用meclipse吗

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

window - > maven - >下载工件源(选择检查)

(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

#9


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.

我试过windows-> pref ..-> Maven但它没有用完。因此,我使用命令mvn eclipse创建了一个新的类路径:eclipse -DdownloadSources = true并刷新工作区一次。瞧..来源是附加的。

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

源jar的条目在类路径中可用。因此,新版本解决了这个问题......

#10


2  

in my version of Eclipse helios with m2Eclipse there is no

在我的版本的Eclipse helios与m2Eclipse没有

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

如果右键单击您的项目,则选择maven - > download sources

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

什么都没发生。没有源下载,没有pom文件更新,没有窗口弹出询问哪些来源。

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.

在eclipse之外做mvn xxx是危险的 - 有些命令不适用于m2ecilpse - 我曾经做过一次并丢失了整个项目,不得不重新安装eclipse并从头开始。

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

我仍在寻找一种方法来获取ecilpse和maven来查找和使用像servlet-api这样的外部罐子的来源。

#11


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.

改变maven-eclipse-plugin的pom以包含source / javadoc只是申请添加到pom的新依赖项。如果我们需要申请现有的依赖项,我们必须运行mvn依赖项:sources。我查了一下。

#12


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.

在Eclipse-Maven偏好中检查下载源/ javadoc,有时是不够的。如果由于某种原因maven无法下载它们(网络中断?),maven会创建一些* .lastUpdated文件,然后再也不会下载。我的经验解决方案是从.m2 / repository中删除工件目录,并在检查下载源/ javadoc时重新启动eclipse工作区,并在启动时更新项目。重新启动工作空间后,可能会错误地标记某些项目,同时下载eclipse进度,然后将清除任何错误。也许这个程序不是那么“科学”,但对我来说确实成功了。

#13


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.

我已经将pom配置添加到maven-eclipse插件中以下载源代码和javadocs,但我想/希望新的依赖项会发生,而不是现有的依赖项。

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.

对于现有的依赖项,我在package explorer中浏览了“Maven Dependencies”并右键单击了commons-lang-2.5.jar,选择了Maven |下载源和...似乎没有任何事情发生(没有进度条或表明它正在做任何事情)。然而,它确实下载了,因为我现在可以跳到commons-lang来源。

#14


1  

overthink suggested using the setup in the pom:

过度建议使用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).

首先我认为这仍然不会附加javadoc和源代码(因为我之前尝试过使用-DdownloadSources选项失败)。

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

但令人惊讶的是 - .classpath文件在使用POM变体时获取其源和javadoc!

#15


1  

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

对于Indigo(可能还有Helios),上面提到的复选框位于:

Window -> Preferences -> Maven

窗口 - >首选项 - > Maven

#16


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.

我遇到了类似的问题,最适合我的解决方案是将源包含在与编译代码相同的jar中(因此jar中的给定目录将包括Foo.java和Foo.class)。 Eclipse自动将源与已编译的代码相关联,并自动从源提供JavaDoc。显然,只有控制工件才有用。

#17


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

在项目级别或用户属性级别设置属性后,请执行Maven - >更新项目(强制更新)。它下载源