You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven:如何对解压依赖中的文件进行变量过滤替换

Maven中过滤依赖解压后的脚本文件

我有一个从依赖中解压得到的脚本,其中包含未定义的变量。我的目标是在Maven项目中配置属性来定义该变量,并对脚本文件进行过滤,将变量替换为对应属性值。

我尝试了以下几个方案:

方案1:直接配置resources节点

<build>
    <resources>
      <resource>
        <directory>${project.build.directory}/dir1</directory>
          <filtering>true</filtering>
            <includes>
              <include>fileToBeFiltered.sh</include>
            </includes>
          </resource>
      <resources>
  </build>

该配置在文件解压前执行,此时没有可过滤的文件。

方案2:配置maven-resources-plugin的resources目标

<build>
   <plugins>
       <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <executions>
              <execution>
                 <id>filter script</id>
                 <phase>generate-resources</phase>
                 <goals>
                    <goal>resources</goal>
                 </goals>
                 <configuration>
                    <resources>
                      <resource>
                         <directory>${project.build.directory}/dir1</directory>
                         <filtering>true</filtering>
                         <includes>
                             <include>fileToBeFiltered.sh</include>
                         </includes>
                       </resource>
                    <resources>
                 </configuration>
              </execution>
          </executions>
       </plugin>
   </plugins>    
</build>

该配置会默认查找src/main/resources目录,而非指定的${project.build.directory}/dir1目录。

方案3:使用maven-resources-plugin的copy-resources目标

<build>
       <plugins>
           <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-resources-plugin</artifactId>
              <executions>
                  <execution>
                     <id>filter script</id>
                     <phase>generate-resources</phase>
                     <goals>
                        <goal>copy-resources</goal>
                     </goals>
                     <configuration>
                        <outputDirectory>${project.build.directory}/dir2</outputDirectory>
                        <resources>
                          <resource>
                             <directory>${project.build.directory}/dir1</directory>
                             <filtering>true</filtering>
                             <includes>
                                 <include>fileToBeFiltered.sh</include>
                             </includes>
                           </resource>
                        <resources>
                     </configuration>
                  </execution>
              </executions>
           </plugin>
       </plugins>    
    </build>

该方案能完成文件过滤,但生成的过滤文件在非目标目录dir2。我尝试将outputDirectory设为源目录${project.build.directory}/dir1以覆盖原文件,但未成功。

我认为存在更简便的实现方式,但尚未发现。另外,我无法修改依赖对应的artifact。


内容的提问来源于stack exchange,提问作者Kyle Seaman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:55:35