我有一个相当大的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>
最佳答案
Maven 实践:
父模块是您定义所有子模块共同使用的依赖项和插件的地方。它不应该有自己的输出。
您应该使用“分发”子模块来聚合所有其他模块 Artifact ,而不是尝试在父模块中执行此操作。
例如 -
为所有项目创建一个简单的父 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>
关于java - 如何从具有多个子模块的 Maven 项目创建单个库 jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37253993/