试图从多个maven模块构建uberjar

时间:2018-04-06 20:45:05

标签: java maven uberjar maven-module

我正在尝试从一组大多数独立的模块构建一个超级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>

&#34;分发&#34;模块看起来像这样:

<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),作为包含内部所有的单个jar文件。

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

1 个答案:

答案 0 :(得分:0)

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

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

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