有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从带有多个子模块的maven项目创建单个库jar?

我有一个相当大的JavaMaven项目,包含200多个模块,现在还可以。我试图将所有这些模块合并到一个jar中

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

我尝试过使用maven汇编插件。我可以创建新的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>

还有我的集会。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>

共 (1) 个答案

  1. # 1 楼答案

    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>