如何将多模块maven项目组装成一个WAR? [英] How to assemble multimodule maven project into one WAR?

查看:390
本文介绍了如何将多模块maven项目组装成一个WAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似问题此处

我想要从3个不同的maven模块部署一个生成的WAR。战争模块完全没有冲突:

I would like to get ONE resulting WAR to be deployed from 3 different maven modules. The war modules are absolutely non-conflicting :


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

第二个只是API - 接口 - 必须已经存在于容器中或部分产生的战争中(这就是我想)

第三个包含实现类,WEB-INF / artifacts(spring infrastructure,web.xml等)

首先取决于接口和实现。第三个取决于接口。

First one depends on interfaces and implementation. Third one depends on the interfaces.

我可能的选项总是很糟糕。

I have a total mess in possible options.

我是否使用Overlays?

Do I use Overlays for this ?

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

Or do I use assembly plugin to be able to integrate Classes from the second one ?

我是否使用货物插件

或者,如果我从不同的模块指定webResources,它是否由maven-war-plugin完成?因为这个家伙是和我做的几乎一样,但只有2个war模块,他不使用汇编插件,也不使用Overlays ....

Or is it accomplished by maven-war-plugin if I specify webResources from different module ? Because this dude is doing almost the same as I do, but with 2 war modules only, and he doesn't use assembly plugin, nor Overlays....

请告诉我,如何这样做得好吗?

Please tell me, how is this done properly ?

推荐答案

这只需要稍微高级的使用maven jar&战争插件。

This just requires a little advanced use of maven jar & war plugin.


第一个有Java类和一些WEB-INF / artifacts

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

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

Let say this represents the main WAR. You just use maven-war-plugin's Overlays feature. The most basic way is specifying the war dependency :

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

并告诉maven war插件将此依赖关系的资产合并到主战争中(我们现在在哪里)

and tell maven war plugin to merge assets of this dependency into the main war (where we are now)

<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类型依赖项(它将是<$ c中的JAR $ c> WEB-INF / lib / )

You also include a jar type dependency on the second one (it will be a JAR in WEB-INF/lib/)

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

您还需要指定第三个WAR对类的依赖:

And you also need to specify dependency on the classes from the third WAR :

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

分类器的注意事项,这是必要的,因为你指定了同一个工件的2个依赖项......为了使它工作,您必须在第三个工件(战争类型工件)中设置jar插件,关于分类器以及您需要来自一个工件(war& jar)的2个包的事实:

Notice of the classifier, it is necessary because you are specifying 2 dependencies of the same artifact... To make it work, you have to set up jar plugin in the third artifact (war type artifact), regarding the classifier and the fact that you need 2 packages from one artifacts (war & jar):

<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>

这篇关于如何将多模块maven项目组装成一个WAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆