【问题标题】:Pack library and all its dependencies to one folder, but other dependencies in separate folder将库及其所有依赖项打包到一个文件夹中,但将其他依赖项打包在单独的文件夹中
【发布时间】:2019-06-30 14:50:55
【问题描述】:

我有 Maven 项目。我使用 maven-assembly-plugin 创建包含所有模块依赖项的 zip 文件。 需要创建具有以下结构的 zip:

/my-libs
/other-libs

到my-libs需要打包来自my-lib的依赖+它所有的传递依赖。 TO other-libs 需要从当前 maven 模块打包所有 other 依赖项。

基本上我需要有条件地选择目标文件夹:

if (dependency in transitive-dependencies(my-lib))
   copy to /my-libs
else
   copy to /other-libs

可以用 maven-assembly-plugin 吗?有没有其他的 maven 插件可以做到这一点?

【问题讨论】:

  • 'mylib' 是 maven 项目本身还是在您的 maven 项目中用作依赖项?
  • 所以我假设my-lib 表示多模块构建中的项目工件,other-libs 是您项目的传递依赖项?问题是为什么需要这样的打包设置?
  • 我需要用 2 包库构建单个工件。该工件将被交付到平台并解压缩。 my-libs 目录将被添加到一组应用程序的类路径中,而my-libs + other-libs 将用于添加到另一组应用程序的类路径中。
  • with assembly-plugin?:不是动态方式:您必须在 assembly.xml 中硬编码/通配您需要的所有内容。替代插件?:没有什么是众所周知的(对我来说),但我相信“自定义插件”是可能的。
  • ..但要实现你想要的,我建议更好地利用 "parent pom" , "modules" 和 "pom import" ...

标签: java maven dependencies zip maven-assembly-plugin


【解决方案1】:

你需要在程序集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

【讨论】:

  • 是否可以避免手动列出所有依赖项?我知道那个解决方案,但它太脆弱。任何新的传递依赖都会打破想法。
  • 哦,对不起,那你需要将 useTransitiveFiltering 设置为 false。默认情况下,过滤器不适用于传递 deps,插件只会拉出所有这些。我将编辑我的答案。
  • @turbanoff 使用一个dependencySet 和includes/include:my-lib 和一个dependencySet 和excludes/exclude:my-lib。
【解决方案2】:

我的印象是你开始的方式不对。您说只有某些程序在类路径上需要other-libs,而其他程序则不需要。因此,这些似乎不是您项目的“真正”依赖项,最多是可选的。

我建议使用my-lib 项目来创建包含所有依赖项的my-lib 的程序集压缩包。这可以在所有不需要otherlibs 的情况下使用。

另一方面,您可以从原始项目创建第二个 zip,其中包含所有库。这样,您就有两个不同的 zip 用于两种不同的目的,但您不需要在 zip 中创建目录结构。

【讨论】:

  • 它会导致 zip 中的库重复。这是我实际上试图避免的。
  • @turbanoff 为什么?您将对不同的用例有明确的区分,并且一点点磁盘空间不应该造成伤害。
  • @turbanoff 我仍然想了解您为什么要避免重复库。这是某种磁盘空间很小的嵌入式系统吗?
【解决方案3】:

您可以将依赖项拆分为两个模块:

- 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>

现在您的每个模块构建将只包含您需要的 jar,没有重复的 INSIDE。 (我认为我们可以忽略 my-libsBETWEENboth 构建的重复,因为无论如何你都不会在同一个平台上使用两者)

【讨论】:

  • 但是在这种方法中,我也会在其他库中拥有 common 依赖项。这是重点,我要避免的。
  • OK 然后修改模块 Y 以包含 my-lib 依赖项,现在你将有两个集合:X = my lib, Y = my lib + other lib 没有重复
  • 怎么样? Y 将包含所有“我的库”库。
  • my-libs directory will be added to classpath to one set of applications => 使用 X,while my-libs + other-libs will be used to be added to classpath to other set of applications. => 使用 Y
猜你喜欢
  • 2011-02-18
  • 2014-05-30
  • 1970-01-01
  • 2020-01-16
  • 2011-05-26
  • 2010-10-07
  • 2014-07-27
  • 1970-01-01
  • 2015-07-12
相关资源
最近更新 更多