首页 文章

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

提问于
浏览
9

类似的问题here .

我想从3个不同的maven模块部署一个结果WAR . 战争模块绝对没有冲突:

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

  • Second one are just API - interfaces - that must be either already present in container or part of the resulting war (that's what I want)

  • Third one with Implementation classes, WEB-INF/artifacts ( spring infrastructure, web.xml, etc)

第一个取决于接口和实现 . 第三个取决于接口 .

我有可能的选择混乱 .

我是否使用Overlays?

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

我使用Cargo plugin吗?

或者,如果我从不同的模块指定webResources,它是否由maven-war-plugin完成?因为this dude几乎和我一样,但只有2个war模块,并且他不使用程序集插件,也不使用Overlays ....

请告诉我,这怎么做得好?

1 回答

  • 7

    这只需要一点点高级使用maven jar和war插件 .

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

    让我们说这代表了主要的战争 . 你只需使用maven-war-plugin的叠加功能 . 最基本的方法是指定战争依赖:

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

    告诉maven 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>
    

相关问题