[英]Pack library and all its dependencies to one folder, but other dependencies in separate folder
我有 Maven 項目。 我使用 maven-assembly-plugin 創建包含所有模塊依賴項的 zip 文件。 需要創建具有以下結構的 zip:
/my-libs
/other-libs
到my-libs
需要打包來自my-lib
依賴項的依賴項 + 它的所有傳遞依賴項。 TO other-libs
需要打包當前 maven 模塊中的所有其他依賴項。
基本上我需要有條件地選擇目標文件夾:
if (dependency in transitive-dependencies(my-lib))
copy to /my-libs
else
copy to /other-libs
是否可以使用maven-assembly-plugin
? 有沒有其他 Maven 插件可以這樣做?
需要在程序集XML文件中定義兩個dependencySet
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
...
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveFiltering>false</useTransitiveFiltering>
<includes>
<include>com.example:dependency1:jar</include>
<include>com.example:dependency2:jar</include>
...
</includes>
<outputDirectory>/my-libs</outputDirectory>
</dependencySet>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveFiltering>false</useTransitiveFiltering>
<includes>
<include>com.example:other1:jar</include>
<include>com.example:other2:jar</include>
...
</includes>
<outputDirectory>/other-libs</outputDirectory>
</dependencySet>
</dependencySets>
...
</assembly>
文檔:
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
你可以嘗試maven依賴插件 。
執行1 - 復制pom中提到的所有依賴項。
執行2 - 復制所有依賴項以及傳遞依賴項。
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-project-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/project-dependency</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
<execution>
<id>copy-project-dependencies-all</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/project-dependency-all</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
我的印象是你開始的方式不對。 您說只有某些程序需要類路徑上的other-libs
,而other-libs
程序則不需要。 因此,這些似乎不是您項目的“真正”依賴項,最多是可選的。
我建議使用my-lib
項目創建一個包含所有依賴項的my-lib
程序集 zip。 這可以在otherlibs
所有上下文中使用。
另一方面,您可以從原始項目創建第二個 zip,其中包含所有庫。 這樣,您有兩個不同的 zip 用於兩個不同的目的,但您不需要在 zip 中創建目錄結構。
您可以將依賴項拆分為兩個模塊:
- ModuleX
pom.xml (contain my-libs)
- ModuleY
pom.xml (contain my-libs + other-libs)
然后使用maven-dependency-plugin
將傳遞依賴項復制到相應的文件夾中。
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.parent.basedir}/build/other-lib</outputDirectory>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
現在你的每個模塊構建將只包含你需要的 jars,沒有重復的INSIDE
。 (我想我們可以忽略的重復my-libs
BETWEEN
兩個構建,因為你不打算在同一個平台反正同時使用)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.