修改maven中war叠加层内的资源

M1E*_*1EK 8 overlay maven maven-war-plugin

我目前有这个设置:

项目A输出一个war文件 - 有一个配置文件(WEB-INF/web.xml).我们一直在提供一个注释掉的配置部分,当在特定环境中部署项目时,可以手动取消注释.

项目的需求已经改变 - 我需要在没有完整配置的情况下构建项目A; 我需要另一个项目(项目B)来构建该部分配置(启用,未注释掉).

我不希望文件存在于两个项目中(双重维护),我曾希望项目B可以依赖项目A(通过war覆盖),然后使用maven-config-processor-plugin将我的特殊配置添加到WEB -INF/web.xml,然后重新打包war文件.

这似乎不起作用 - 但是 - 如果目标已经存在(即在上次运行之后),配置修改可以工作,但是当我一起运行所有内容时,覆盖和重新打包到新战争中一起发生 - 我可以'找出使配置处理器插件在中间运行的任何方法.基本上,默认顺序最终是"config-processor"(由于覆盖尚未发生而失败),然后是"war"(全部作为一个单元).在覆盖之后但战争完全打包之前,我无法使配置处理器发生.

互联网上的多个人在过去几年里曾询问是否有办法在"解压缩覆盖"和"重新包装战争文件"步骤之间注入一个插件,但是无论如何都没有人能够明确地回答这个问题.有任何想法吗?

mat*_*tts 6

由于战争叠加和战争包装似乎都是单一目标的一部分,我认为没有办法进入中间.作为一种解决方法,您可以web.xml在早期阶段提取并处理它.可以在项目B中使用maven-dependency-plugin web.xml从项目A中提取到工作目录中,然后运行maven-config-processor-plugin web.xml并将结果放在其他位置,然后指示maven-war-plugin包含已处理的web.xml覆盖之前.在项目B的POM中:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.4</version>
        <executions>
            <!-- Extract web.xml from Project A -->
            <execution>
                <id>unpack-webxml</id>
                <phase>generate-resources</phase>
                <goals>
                    <goal>unpack</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>your.group</groupId>
                            <artifactId>project.a</artifactId>
                            <version>...</version>
                            <type>war</type>
                            <overWrite>true</overWrite>
                            <outputDirectory>${project.build.directory}/myconfig/work</outputDirectory>
                            <includes>WEB-INF/web.xml</includes>
                        </artifactItem>
                    </artifactItems>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>com.google.code.maven-config-processor-plugin</groupId>
        <artifactId>maven-config-processor-plugin</artifactId>
        <version>2.0</version>
        <executions>
            <!-- Process extracted web.xml and place in temp build directory -->
            <execution>
                <id>process-webxml</id>
                <goals>
                    <goal>process</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/myconfig/build</outputDirectory>
                    <transformations>
                        <transformation>
                            <input>${project.build.directory}/myconfig/work/WEB-INF/web.xml</input>
                            <output>WEB-INF/web.xml</output>
                            <!-- your transformation config -->
                        </transformation>
                    </transformations>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
            <webResources>
                <!-- Instruct war plugin to include temp build directory in webapp -->
                <resource>
                    <directory>${project.build.directory}/myconfig/build</directory>
                    <includes>
                        <include>**</include>
                    </includes>
                </resource>
            </webResources>
            <overlays>
                <!-- Overlay customization if needed -->
            </overlays>
        </configuration>
    </plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)

据我所知,war插件webResources首先包括src/main/webapp,然后是覆盖.

我不熟悉maven-config-processor-plugin,所以如果我的配置不正确,我会道歉.