How to create a single library jar from maven project with multiple submodules?

问题描述 投票:0回答:1

我有一个相当大的 java maven 项目,有 200 多个模块,按原样就可以了。我正在尝试将所有这些模块合并到单个 jar 中。

在某些情况下,只声明一个新的 Maven 依赖项会非常方便,例如。 my-library-5.2.4-SNAPSHOT-bundle.jar 或新项目中的类似内容。

我试过使用maven assembly-plugin。我可以创建新的 jar 文件,jar 包含所有模块 jar,如果我安装它,它会正确地进入本地 .m2 文件夹。 Jar 可以在其他项目中声明为依赖项。

但问题是我无法在我的 Java 类中的库中导入任何这些模块。导入将无法识别这些。

我已将此构建部分添加到我的根 pom.xml:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>make-bundle</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我的 assembly.xml 看起来像这样:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bundle</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <binaries>
                <outputDirectory>modules</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>
java maven jar maven-assembly-plugin maven-dependency
1个回答
0
投票

Maven 实践:

父模块是您定义所有子模块共同使用的依赖项和插件的地方。它不应该有自己的输出。

您应该使用聚合所有其他模块工件的“分发”子模块,而不是尝试在父模块中这样做。

例如-

一般为所有项目创建一个简单的父 pom 文件项目(包装为“pom”)

<?xml version="1.0" encoding="UTF-8"?>
<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>my.library</groupId>
    <artifactId>my-library-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <distributionManagement>
        <repository>
            <id>site</id>
            <url>http://url_id</url>
        </repository>
    </distributionManagement>

</project>

现在对于您希望使用它的所有项目,只需包含此部分:

<parent>
  <groupId>my.library</groupId>
  <artifactId>my-library-parent</artifactId>
  <version>1.0.0</version>
</parent>
© www.soinside.com 2019 - 2024. All rights reserved.