如何使用Maven下载jar的源代码?

Suz*_*ioc 60 java jar maven

在我的项目中,我使用的是通过Maven提供的JAR文件.但Maven给我的只是这个罐子 - 没有javadocs,也没有来源.按"下载源"无效:Eclipse仍然找不到jar的来源.

这取决于什么?存储库应该自动提供源吗?

可能我需要在POM中写一些东西来指示Maven下载源代码?

我目前的pom如下:

<repositories>
    <repository>
        <id>xuggle repo</id>
        <url>http://xuggle.googlecode.com/svn/trunk/repo/share/java/</url>
    </repository>
</repositories>

<dependencies>

    <dependency>
        <groupId>xuggle</groupId>
        <artifactId>xuggle-xuggler</artifactId>
        <version>5.3</version>
        <type>rar</type>
    </dependency>

</dependencies>
Run Code Online (Sandbox Code Playgroud)

为什么Maven没有对它的任何评论来源下载失败?

hov*_*yan 92

执行mvn dependency:sources将强制maven下载项目中所有jar的所有源,如果源可用(在托管工件的存储库中上载).如果你想下载javadoc命令是mvn dependency:resolve -Dclassifier=javadoc

也可以在settings.xml文件中创建配置文件,并包含以下属性:

<properties>
  <downloadSources>true</downloadSources>
  <downloadJavadocs>true</downloadJavadocs>
</properties>
Run Code Online (Sandbox Code Playgroud)

  • @hovanessyan - 我们可以添加一些设置,仅针对某些依赖项而不是全部依赖项提取 jar 吗? (2认同)

Kal*_*oni 45

mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc
Run Code Online (Sandbox Code Playgroud)

如果它没有来源,它应该说类似的东西

[INFO] The following files have NOT been resolved:
[INFO]    com.oracle:ojdbc6:java-source:sources:12.1.0.1
[INFO]    javax:javaee-api:java-source:sources:6.0
Run Code Online (Sandbox Code Playgroud)


小智 9

最好不要依赖Eclipse插件,因为它已被弃用.使用downloadSourcesdownloadJavadocs属性对我不起作用.关于使用依赖项插件字的上面发布的答案.但是,您可能希望自动下载源和javadoc.此外,您可能希望始终创建一个源jar和一个javadoc jar.把它放在项目的pom中.如果您使用模块,请放入您的父pom.

<build>
    <plugins>
        <!-- download sources and javadoc -->
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>download-sources</id>
                    <goals>
                        <goal>sources</goal>
                    </goals>
                </execution>
                <execution>
                    <id>download-javadoc</id>
                    <configuration>
                        <classifier>javadoc</classifier>
                    </configuration>
                    <goals>
                        <goal>resolve</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Always create javadoc jar. -->
        <plugin>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.10.4</version>
            <executions>
                <execution>
                    <id>attach-javadoc</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Always create source jar. -->
        <plugin>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)


Ali*_*ahi 6

在 Eclipse 中,Maven>Download Sources再次右键单击您的项目Maven>Download Javadoc


Mar*_* An 5

您还可以target/dependencies使用以下方式下载源代码:

mvn -Dclassifier=sources dependency:copy-dependencies
Run Code Online (Sandbox Code Playgroud)