我知道mvn dependency:sources可以下载所有依赖项的源。有了mvn copy-dependencies一个可以下载所有的依赖到指定的本地目录。
如何将两者结合起来,以便我可以将所有依赖项的源复制到一个目录中?
小智 6
你可以简单地使用
mvn dependency:copy-dependencies -Dclassifier=sources
Run Code Online (Sandbox Code Playgroud)
它将下载链接到项目依赖项的所有源 jar 并将它们复制到yourProject/target/dependency文件夹中
maven-dependency-plugin 的可选参数“classifier”记录在此处 https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependency-mojo.html
我不会使用任何这些解决方案。
只需将 maven-dependeny-plugin 包含到您的 Maven 构建中并根据您的需要调整配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>none</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
您可以更改多个项目以满足您的需求,例如,在我给您的示例中,我认为它直接为您提供了解决方案,我已指定在任何阶段都不会将依赖项复制到 tmp 中的alternateLocation文件夹。然而我也说我的新目标是复制依赖性。所以在命令行中这将是这样的:
mvn dependency:copy-dependencies
Run Code Online (Sandbox Code Playgroud)
如果您注意到我现在已经配置了两次outputDirectory。在执行内部,这意味着只有当您运行指定的 Maven 构建阶段(如打包、清理、测试等)时才会考虑它。作为插件节点的第一个子节点,这意味着当命令行显式调用依赖插件时将考虑它,这正是您想要的。
您可以在此处找到有关 maven-dependency 插件的更多信息:
由于您同时需要依赖项和源,我能想到的最好方法是正常运行 Maven,而不隐式调用实际插件。如果您在清理后阶段(即 mvn 清理后)运行它,它将运行以下两个目标:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>post-clean</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
<execution>
<id>sources</id>
<phase>post-clean</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
它始终会复制到目标文件夹,但如果文件已存在,则不会覆盖。我必须选择一个不常用的阶段。post-clean似乎是这里的最佳选择。这只是我想隔离这种构建。post clean还会清理构建。如果您只想在每次构建时继续使用此插件,那么我建议将其置于干净或安装阶段。这样它总是在后台发生,你不用担心。
| 归档时间: |
|
| 查看次数: |
5015 次 |
| 最近记录: |