如何将jar的源下载到maven中的自定义位置?

How to download the souce of jars to custom location in maven?

这是 的跟进 post 除了我希望将依赖和传递依赖的 jar 的源下载到自定义提到的位置。

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

mvn dependency:sources -DoutputDirectory=.../

没用。

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

没用。

通常可以使用 classifier, so that for the same Maven coordinates (GAV, groupId, artifactId, version) you can have more than one artefact related to the same build (i.e. default application/library jar, sources jar, test sources jar, javadoc jar, etc.), as also explained in another SO answer. The standard classifier for sources is sources, created by the Maven Source Plugin.

通过 Maven 获得源 jar

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

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

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

注意附加的 -Dclassifier=sources 选项。

在依赖插件的 official documentation 中也解释了实现相同目的的 pom 配置示例,使用以下代码段:

<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 将找到它,因此不会下载任何源。