【问题标题】:How to create additional jar file with contents of all modules during maven build?如何在 Maven 构建期间创建包含所有模块内容的附加 jar 文件?
【发布时间】:2015-12-30 17:22:17
【问题描述】:

我有一个多模块 maven 项目(比如 project-xxx)。假设它由 5 个模块组成:

project-xxx (war)
--> module-1 (jar)
--> module-2 (jar)
--> module-3 (jar)
--> module-4 (jar)
--> module-5 (jar)

在构建maven项目时,会生成war文件,其中还包含了5个模块的jar文件。

现在,出于不同的目的(即部署到分布式缓存,以便我们可以从命令行运行查询),我还想生成一个包含所有模块的 java 类的单个“jar”文件。我知道生成多个工件是违反 maven 的理念的,我在 SO 上阅读了 blog postfew other questions

但是创建这个单一的 jar 文件将大大简化我项目中的其他一些事情。生成这个 jar 文件的最佳方法是什么?

【问题讨论】:

    标签: maven jar maven-3


    【解决方案1】:

    非常非常赞成每个 Maven 项目约定一个工件。话虽如此,如果您需要一个包含所有模块的所有类的单个工件,那么请创建一个专用的单个项目来这样做:

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>your-group-id</groupId>
        <artifactId>one-jar-to-rule-them-all</artifactId>
        <version>your-version</version>
        <dependencies>
            <dependency>
                <groupId>your-group-id</groupId>
                <artifactId>module-1</artifactId>
                <version>your-version</version>
            </dependency>
            .
            .
            .
            <dependency>
                <groupId>your-group-id</groupId>
                <artifactId>module-5</artifactId>
                <version>your-version</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4.2</version>
                    <configuration>
                        <!--
                            This restricts the jar to classes from your group;
                            you may or may not want to do this.
                        -->
                        <artifactSet>
                            <includes>
                                <include>your-group-id</include>
                            </includes>
                        </artifactSet>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    此示例项目依赖于每个模块,然后使用maven-shade-plugin 将所有这些模块组合到一个 jar 工件中。您也可以将其设为您的父级 project-xxx 的子模块,以便它由反应器构建。这样,您可以同时拥有 war 和 uber jar,但仍保持标准 Maven 构建的模块化。

    【讨论】:

    • 谢谢!我几乎在同一个方向,但不知道shade 插件。那是缺失的部分。
    【解决方案2】:

    我认为您应该考虑引入第一个单独的配置文件,以便 profile1 包含配置以产生正确的战争包装。 Profile2 可以包含使用 maven-shade-plugin 的配置,以便从现有模块中创建 UBER jar。配置文件是一种非常干净且专业的方式来拆分不同的关注点。

    有关 Maven 配置文件,请参阅 here。对于 maven-shade-plugin,请参阅 here

    希望对您有所帮助。

    【讨论】:

    • 对于我的要求,创建一个新模块,如@heenenee 在answer 中描述的那样似乎更合适。感谢shade 插件指针。
    猜你喜欢
    • 2017-10-11
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多