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

lis*_*sak 9 java war maven-3 maven-assembly-plugin multi-module

类似的问题在这里.

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

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

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

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

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

我有可能的选择混乱.

我是否使用Overlays?

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

我使用Cargo插件吗?

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

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

lis*_*sak 8

这只需要一点点高级使用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>
Run Code Online (Sandbox Code Playgroud)

并告诉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>
Run Code Online (Sandbox Code Playgroud)

你还在第二个包含jar类型依赖(它将是一个JAR WEB-INF/lib/)

    <dependency>
        <groupId>${groupId}</groupId>
        <artifactId>${rootArtifactId}-service</artifactId>
        <version>${version}</version>
        <type>jar</type>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

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

    <dependency>
        <groupId>${groupId}</groupId>
        <artifactId>${rootArtifactId}-service-impl</artifactId>
        <version>${version}</version>
        <classifier>classes</classifier>
        <type>jar</type>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

分类的通知,因为您指定2只依赖同一工件,有必要...要使其工作,你必须建立在第三神器Jar插件(战型神器),关于分类和事实你需要一个工件的两个包(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>
Run Code Online (Sandbox Code Playgroud)