1

我正在尝试从一组大部分独立的模块中构建一个 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>

“分发”模块的 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] 读取程序集描述符:src/assembly/assembly.xml
[警告] 在此工件包含过滤器中从未触发以下模式:
o'com.myCompany:mod1'
o'com.myCompany:mod2'
...

其次是错误:

[错误] 无法在项目分发中执行目标 org.apache.maven.plugins:maven-assembly-plugin:3.1.0:single (distro-assembly):创建程序集失败:创建程序集存档 bin 时出错:存档不能为空-> [帮助 1]

真正想要的:

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

我真的不确定如何在多模块环境中实现这一点。

4

1 回答 1

0

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

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

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

于 2018-04-06T21:24:00.387 回答