【问题标题】:Jar from child pom with own dependencies来自具有自己依赖项的子 pom 的 Jar
【发布时间】:2017-01-05 11:21:06
【问题描述】:

有:

父 pom:

<groupId>practice</groupId>
<artifactId>app</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<modules>
    <module>h2db</module>
</modules>

<build>
    <pluginManagement>
    ...
    </pluginManagement>
</build>

<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.185</version>
    </dependency>
</dependencies>

儿童 pom:

<parent>
    <artifactId>app</artifactId>
    <groupId>practice</groupId>
    <version>1.0</version>
</parent>

<artifactId>h2db</artifactId>
<build>
    <finalName>h2db</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>h2_server.H2ConsoleServer</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.185</version>
    </dependency>
</dependencies>

在此示例中,这些是具有 2 个(或更多)依赖项的父 pom。子 pom 只有一个它需要的依赖项。我是否可以从这个子 pom 模块构建一个 jar 并包含 (scope:compile) 它的单一依赖项?它构建了一个 jar,但在 jar 文件中不包含 h2 依赖项。我尝试在子 pom 中使用 maven-assembly-pluginjar-with-dependencies,但结果它创建了一个包含来自父 pom 的所有依赖项的 jar。

【问题讨论】:

    标签: java maven build


    【解决方案1】:

    在父 pom 中列出依赖项时,是因为子项目需要它们。

    您应该考虑从父 pom 中删除所有子级不需要的依赖项。

    然而,一个最佳实践是使用dependencyManagement。您可以在父 pom 中指定将由子项目添加的任何 log4j 和 h2 库的版本。然后在您的子项目中,您只需导入不带版本的依赖项。

    父pom:

    <groupId>practice</groupId>
    <artifactId>app</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    
    <modules>
        <module>h2db</module>
    </modules>
    
    <build>
        <pluginManagement>
        ...
        </pluginManagement>
    </build>
    
    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.185</version>
        </dependency>
    </dependencies>
    </dependencyManagement>
    

    儿童 pom:

    <parent>
        <artifactId>app</artifactId>
        <groupId>practice</groupId>
        <version>1.0</version>
    </parent>
    
    <artifactId>h2db</artifactId>
    <build>
        <finalName>h2db</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>h2_server.H2ConsoleServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>
    

    【讨论】:

      【解决方案2】:

      别想了,你可以试试maven-shade-plugin (http://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html)

      不确定它是否正是您需要的,但它确实允许您指定要包含/忽略的依赖项。

      【讨论】:

      • 如果我有 100 个依赖项,我应该忽略每个依赖项而不是只包含我需要的依赖项怎么办?
      • 嗯...我对插件的了解不是 100%,但我相信您至少可以在包上使用通配符?似乎您也可以创建一个白名单,并且大概会忽略其余的? maven.apache.org/plugins/maven-shade-plugin/examples/… 显示如何设置包含。还有一个minimizeJar 可以丢弃未使用的东西。 stackoverflow.com/questions/8698814/… 上的答案对此做了一些解释。
      • 感谢minimizeJar。我想我可以用它的过滤器来制作它。
      猜你喜欢
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 2012-04-29
      • 2016-01-19
      • 1970-01-01
      • 2020-01-25
      相关资源
      最近更新 更多