【问题标题】:How to build a Uber jar with dependencies from multiple modules如何使用来自多个模块的依赖项构建 Uber jar
【发布时间】:2016-08-01 15:09:00
【问题描述】:

我在 IntelliJ 中建立了一个多模块项目。我的模块结构如下所示:

Project (no pom.xml file)
|
|--moduleA
|    |
|
|--moduleB
|    |-Log4J
|    |-Jackson
|
|--moduleC
|    |-moduleA
|    |-moduleB
|    |-Log4J
|
|--moduleD
|    |-moduleC
|    |-moduleA
|    |-moduleB
|    |-Log4J
|
|--moduleE
|    |-moduleA
|    |-moduleB
|    |-moduleC
|    |-Log4J

moduleC 依赖于moduleAmoduleB,而moduleD 依赖于moduleAmoduleBmoduleCmoduleE 也是如此。

我想创建两个 Uber 罐子。一个用于moduleD,一个用于moduleE,每个都包含模块的依赖关系,包括模块A、B和C。

我正在使用 Maven 3 来管理我的依赖项,并使用 maven-shade-plugin 来创建 Uber jar。

我在pom.xml 文件中添加了必要的依赖项,如下所示:

<dependency>
  <groupId>com.modules</groupId>
  <artifactId>moduleA</artifactId>
  <version>1.0</version>
</dependency>

我已将maven-shade-plugin 添加到每个模块的pom.xml 文件中。到目前为止,我能够为moduleAmoduleB 构建一个超级jar,但是当我尝试moduleC 时,我收到以下错误:

[WARNING] The POM for com.modules:moduleA:jar:1.0 is missing, no dependency information available
[WARNING] The POM for com.modules:moduleB:jar:1.0 is missing, no dependency information available

我该如何解决这个问题?

如果我通过 IntelliJ 构建和运行模块,一切正常。我还能够通过配置 IntellJ 工件来构建一个 uber jar。

如何配置 Maven 来执行此操作?我需要它,因为我想使用 uber jar 进行生产,并且我正在尝试设置 Jenkins 以在提交后构建解决方案。

我将以下帖子设为红色:How to create self-containing (standalone) jar of each modules of a multi-module maven project 但我无法使其正常工作。


编辑:

这是moduleAmoduleB 中的maven-shade-plugin 设置

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

这是moduleC中的maven-shade-plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
            </execution>
        </executions>
    </plugin>

如您所见,我使用的是默认配置。

【问题讨论】:

  • 你是如何定义 maven-shade-plugin 设置的?没有看到你的设置很难诊断。
  • @MichaelDibbets 请参阅配置的编辑部分。
  • maven.apache.org/plugins/maven-shade-plugin/examples/… 你没有定义 有什么原因吗?
  • 目前还不清楚你想做什么,但我猜你想在moduleDmoduleE 下创建一个专用模块,负责创建 uberjar。所以你将在moduleD 下拥有一个模块moduleD-uber,它依赖于moduleAmoduleBmoduleC
  • @IvanStoyanov,参见 logback-android 的 pom-uber.xml,它创建了一个包含其子模块项目的 uber jar。

标签: java maven intellij-idea jenkins jar


【解决方案1】:

尝试添加

<includes>
  <include>**</include>
</includes>

在您定义排除项的同一位置。

我能找到的&lt;filter&gt; 的唯一出现是与包含和排除过滤器结合使用。

别管我,但这就是我猜你的构建失败的原因。

【讨论】:

  • 它仍然失败。我的猜测是它找不到其他模块的 .jar 文件。
  • 您是否尝试过制作一个单独的项目,包括所有这些项目,并将其编译成一个 uberjar?为了防止mythologian.net/wp-content/uploads/2013/10/… 以某种方式发生,它一直在循环。
  • 不,我没有。到目前为止,我已经尝试为moduleC 创建一个 uber jar,但它失败了。我没有尝试过moduleD,因为我猜如果muduleCmoduleD 的依赖项之一失败,moduleD 也会失败。您是否建议我应该创建一个仅依赖于 moduleD 的新模块并从中创建一个 uber jar?这将如何运作?
  • 将新模块视为依赖于所有其他包的完整包,它使用它们并将它们一起编译成一个定义明确的包。通常在这种情况下,我发现最好明确列出我需要包含和排除的内容。可能看起来很乏味,但至少你不会对意想不到的事情感到惊讶。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多