【问题标题】:How to download the souce of jars to custom location in maven?如何将jar的源下载到maven中的自定义位置?
【发布时间】:2016-04-14 14:28:18
【问题描述】:

这是How to get all the specified jars mentioned in the pom.xml and transitively dependent jars?的后续帖子 除了我希望将依赖和传递依赖 jar 的源下载到自定义提到的位置。

我尝试了以下命令,但没有成功。

mvn dependency:sources -DoutputDirectory=.../

没用。

mvn dependency:sources dependency:copy-dependencies -DoutputDirectory=.../

没用。

【问题讨论】:

  • 什么样的自定义位置?在 Maven 中,您通常将存储库用于此类目的...如果您确实需要这些工件,请将它们部署到存储库管理器并从那里使用它们...
  • 您是否按照我的回答中的建议尝试了 mvn dependency:copy-dependencies -DoutputDirectory=somewhere -Dclassifier=sources?
  • @A.DiMatteo:是的。它的工作正常。谢谢。

标签: maven maven-2 pom.xml maven-dependency-plugin maven-jar-plugin


【解决方案1】:

源 jar 通常可以通过 Maven 使用classifier 获得,因此对于相同的 Maven 坐标(GAV、groupId、artifactId、版本),您可以拥有多个与相同构建相关的工件(即默认应用程序/库 jar、源 jar、测试源 jar、javadoc jar 等),如另一个 SO answer 中所述。来源的标准分类器是 sources,由 Maven Source Plugin 创建。

copy-dependencies 可以配置为通过classifier 选项获取某个分类器。

因此,在您的情况下,要将依赖项的来源获取到外部文件夹,您可以调用以下命令:

mvn dependency:copy-dependencies -DoutputDirectory=somewhere -Dclassifier=sources

注意额外的-Dclassifier=sources 选项。

在依赖插件的official documentation中也说明了实现相同的pom配置示例,使用以下sn-p:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>src-dependencies</id>
            <phase>package</phase>
            <goals>
              <!-- use copy-dependencies instead if you don't want to explode the sources -->
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <classifier>sources</classifier>
              <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
              <outputDirectory>${project.build.directory}/sources</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

请注意,尽管 Maven 不知道源,它只知道人工制品。因此,如果无法通过其 GAV+C 获得源(机密)工件,Maven 会找到它,因此不会下载任何源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2023-03-19
    相关资源
    最近更新 更多