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

时间:2012-05-31 13:44:44

标签: maven overlay 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”(全部作为一个单元)。在覆盖之后但战争完全打包之前,我无法使配置处理器发生。

互联网上的多个人在过去几年里曾询问是否有办法在“解压缩覆盖”和“重新包装战争文件”步骤之间注入一个插件,但没有人看似以任何方式明确地回答这个问题。有什么想法吗?

2 个答案:

答案 0 :(得分:6)

由于战争叠加和战争包装似乎都是单一目标的一部分,我认为没有办法进入中间。作为一种变通方法,您可以在早期阶段提取web.xml并进行处理。可以在项目B中使用maven-dependency-plugin将项目A中的web.xml提取到工作目录中,然后在web.xml上运行maven-config-processor-plugin并将结果放在其他位置,然后指示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>

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

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

答案 1 :(得分:4)

您可以使用unpack goal of maven-dependency-plugin获取项目A以在本地获取web.xml,然后对其进行转换并指向maven-war-plugin to transformed web.xml

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.4</version>
            <executions>
              <execution>
                <id>unpack</id>
                <phase>generate-resources</phase>
                <goals>
                  <goal>unpack</goal>
                </goals>
                <configuration>
                  <artifactItems>
                    <artifactItem>
                      <groupId>org.test</groupId>
                      <artifactId>test-war1</artifactId>
                      <version>0.0.1-SNAPSHOT</version>
                      <type>war</type>
                    </artifactItem>           
                  </artifactItems>
                  <outputDirectory>${project.build.directory}/wars</outputDirectory>
                </configuration>
              </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>com.google.code.maven-config-processor-plugin</groupId>
            <artifactId>maven-config-processor-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <!-- 
                  configure to transform file
                  ${project.build.directory}/wars/WEB-INF/web.xml
                  into
                  ${project.build.directory}/transformed/web.xml
                 -->
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <warSourceDirectory>src/main/webapp</warSourceDirectory>
              <webXml>${project.build.directory}/transformed/web.xml</webXml>
            </configuration>
        </plugin>
    </plugins>
  </build>
相关问题