尝试从多个 Maven 模块构建 uberjar

Trying to build uberjar from multiple maven modules

我正在尝试从一组大部分独立的模块构建一个 uber-jar,但它并没有像我想的那样工作。

我最初被引导到这里:https://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/module-binary-inclusion-simple.html 虽然现在我不太确定这就是我应该开始的...

父 pom 文件如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>BigProject</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>
    <name>BigProject</name>

    <modules>
        <module>../mod1</module>
        <module>../mod2</module>
        <!-- ...a bunch more... -->
        <module>../distribution</module>
    </modules>
<build>
    <pluginManagement>
        <plugins>
            <!-- not even sure why this needs to be specified here, but the documentation seems to think it's important... -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
</project>

"distribution" 模块的 pom 如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <artifactId>distribution</artifactId>
    <name>distribution</name>
    <packaging>pom</packaging>

    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>BigProject</artifactId>
        <version>0.0.1</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>distro-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

最后是汇编文件:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>bin</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <moduleSets>
        <moduleSet>
            <includeSubModules>true</includeSubModules>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.myCompany:mod1</include>
                <include>com.myCompany:mod2</include>
                <!-- and others... -->
            </includes>
            <binaries>
                <includeDependencies>true</includeDependencies>
                <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

(我意识到在某些时候我可能需要将 <format>dir</format> 更改为 <format>jar</format> 但现在,我只想让一些东西工作)

当我从主父模块的目录 运行 mvn clean package 时,我收到这样的警告:

[INFO] Reading assembly descriptor: src/assembly/assembly.xml
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o  'com.myCompany:mod1'
o  'com.myCompany:mod2'
...

后面出现错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:3.1.0:single (distro-assembly) on project distribution: Failed to create assembly: Error creating assembly archive bin: archive cannot be empty -> [Help 1]

实际上想要的:

一个 jar 文件,包含我的所有模块及其所有依赖项(即 *-with-dependencies.jars),作为一个包含 所有内容[=44 的 jar 文件=] 包含在里面。

我真的不确定如何在多模块上下文中实现这一点。

我想我可能已经解决了:子模块 mod1、mod2...缺少父模块依赖项。我添加了

<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>BigProject</artifactId>
    <version>0.0.1</version>
</parent>

到相关的子模块,现在构建不会失败,但会填充分发项目下的目标目录。