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

标签 maven replace maven-3 maven-war-plugin

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

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

<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 包中的文件,同时在后续构建中保持源代码不变?

最佳答案

您可以使用 exploded maven-war-plugin的目标进入临时文件夹(实际上就像 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 的内容之前创建的目标。但是,该选项并不真正适合此目的。


同样,根据我的测试,以下方法将有效:

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

以下内容将应用上述方法:

<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>

关于maven-replacer-plugin 用于替换构建中的 token 而不是源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39105557/

相关文章:

java - 以编程方式解析 Maven Artifact URL

mysql - 用特殊字符'更新和替换MySQL中的数据库

c# - 在大文本文件中用\r\n 替换\n

java - 在 Maven 构建时替换测试属性值

maven - 创建密封的Maven构建

java - 指定属性和日志文件的位置时出错

java - TestNG、Junit、Mockito?

java - 我想编写一个 shell 脚本,它将 mvn clean test 命令作为输入,当我运行该脚本时,它应该运行我的 Maven 项目

java - Maven测试: Checkout some other repository for some test classes During Jenkin Build

python - 替换列表列表中的值