maven-replacer-plugin 用于替换构建中的令牌而不是源代码

maven-replacer-plugin to replace tokens in build and not source

我正在尝试使用 maven-replacer-plugin 来替换我的 web.xml 中的标记,当它建立在 WAR 文件中而不是在源代码中时,它会删除标记后续构建并显示文件相对于版本控制存储库的更改。

目前我只能更改源中的文件,不符合我的要求:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <file>${project.basedir}/src/main/webapp/WEB-INF/web.xml</file>
        <replacements>
            <replacement>
                <token>@@sec.level@@</token>
                <value>local</value>
            </replacement>
        </replacements>
    </configuration>
</plugin>

问题:我如何 运行 替换器只更改 WAR 包中的文件,而在后续构建中保持源不变?

您可以使用 maven-war-pluginexploded 目标来访问一个临时文件夹(就像在 target 下创建的任何东西一样)稍后将成为一部分的分解版本最后的 war 文件,然后在此文件上执行 replacer 插件(安全副本,不与使用该文件的其他插件冲突)。

official replacer plugin doc

实际上也记录了这种方法

即配置相似:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <useCache>true</useCache>
    </configuration>
    <executions>
        <execution>
            <id>prepare-war</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>exploded</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals> 
        </execution>
    </executions>
    <configuration>
        <file>${project.build.directory}/${project.build.finalName}/WEB-INF/web.xml</file>
        <token>@@sec.level@@</token>
        <value>local</value>
    </configuration>
</plugin>

注意:replacer 文档还建议使用 useCache 选项,这样可以防止插件覆盖之前创建的 exploded 目标。然而,这个选项并不真正适合这个目的。


同样,根据我的测试,下面的方法会起作用:

  • 使用 maven-war-pluginexploded 目标在 target 下的 <war_name>-tmp 目录中创建未来 war 文件的临时副本:那是不是问题,无论如何都应该通过 clean 命令丢弃 target 下的任何内容
  • 配置 replacer 插件以替换 web.xml 文件的副本
  • 使用其 webXml 选项配置默认 war 目标以指向其最终 war 文件
  • web.xml 文件

以下将应用上述方法:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <!-- explode the future war content for pre-package processing -->
            <id>prepare-war</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>exploded</goal>
            </goals>
            <configuration>
                <webappDirectory>${project.build.directory}/${project.build.finalName}-tmp</webappDirectory>
            </configuration>
        </execution>
        <execution>
            <!-- use the same execution id to further configure the default binding and execution -->
            <id>default-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
            <configuration>
                <!-- during the package phase, use the processed web.xml file -->
                <webXml>${project.build.directory}/${project.build.finalName}-tmp/WEB-INF/web.xml</webXml>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
        <execution>
            <!-- apply pre-package processing on web resources -->
            <id>process-web-resources</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <file>${project.build.directory}/${project.build.finalName}-tmp/WEB-INF/web.xml</file>
        <token>@@test@@</token>
        <value>local</value>
    </configuration>
</plugin>