How to assemble multimodule maven project into one WAR?

青春壹個敷衍的年華 提交于 2019-11-30 05:13:06

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

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

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>

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>

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>

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>

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!