gpt4 book ai didi

java - 如何将多模块maven项目组装成一个WAR?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:20:59 25 4
gpt4 key购买 nike

类似问题here .

我想从 3 个不同的 Maven 模块中部署一个生成的 WAR。 war 模块绝对不冲突:

  • 第一个有 Java 类和一些 WEB-INF/工件的

  • 第二个只是 API - 接口(interface) - 必须已经存在于容器中或由此产生的 war 的一部分(这就是我想要的)

  • 第三个带有实现类、WEB-INF/artifacts(spring infrastructure、web.xml 等)

第一个取决于接口(interface)和实现。第三个取决于接口(interface)。

我对可能的选择一团糟。

我是否为此使用叠加层?

或者我是否使用程序集插件来集成第二个类中的类?

我是否使用 Cargo plugin

或者如果我指定来自不同模块的 webResources,它是由 maven-war-plugin 完成的吗?因为this dude和我做的几乎一样,但是只有 2 个 war 模块,而且他不使用 assembly 插件,也不使用 Overlays....

请告诉我,这是如何正确完成的?

最佳答案

这只需要对 maven jar 和 war 插件进行一些高级使用。

First one that has Java classes and some WEB-INF/artifacts

假设这代表主要的 WAR。您只需使用 maven-war-plugin 的 Overlays 功能。最基本的方法是指定 war 依赖:

    <dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-service-impl</artifactId>
<version>${version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>

并告诉 maven war 插件将此依赖项的 Assets 合并到主 war 中(我们现在所在的位置)

<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<dependentWarExcludes>WEB-INF/web.xml,**/**.class</dependentWarExcludes>
<webResources>
<resource>
<!-- change if necessary -->
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

您还包括对第二个的 jar 类型依赖(它将是 WEB-INF/lib/ 中的 JAR)

    <dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-service</artifactId>
<version>${version}</version>
<type>jar</type>
</dependency>

并且您还需要指定对第三次 WAR 中的类的依赖:

    <dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-service-impl</artifactId>
<version>${version}</version>
<classifier>classes</classifier>
<type>jar</type>
</dependency>

分类器的通知,这是必要的,因为您正在指定同一工件的 2 个依赖项......为了使其工作,您必须在第三个工件( war 类型工件)中设置 jar 插件,关于分类器和事实上,您需要一个工件(war 和 jar)中的 2 个包:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<!-- jar goal must be attached to package phase because this artifact is WAR and we need additional package -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<!--
classifier must be specified because we specify 2 artifactId dependencies in Portlet module, they differ in type jar/war, but maven requires classifier in this case
-->
<classifier>classes</classifier>
<includes>
<include>**/**.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

关于java - 如何将多模块maven项目组装成一个WAR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7897114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com