复制资源时完全搜索和替换源文件中的字符串 [英] Full search and replace of strings in source files when copying resources

查看:74
本文介绍了复制资源时完全搜索和替换源文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用包前缀的Java源文件(它们在模拟某些JDK类).我将这些文件与前缀一起使用以针对某些单元测试运行.如果测试通过,我想生成一个包含源文件但从所有Java文件中删除包前缀的jar.

I have some java source files that use a package prefix (they are emulating some JDK classes). I use these files with the prefix to run against some unit tests. If the tests pass I want to produce a jar that contains the source files but with the package prefix removed from all the java files.

我正在使用Maven进行构建.有人知道这样做的方法吗?本质上,我想要的是类似资源插件过滤功能的东西,但是可以正确搜索和替换(例如:s/my.package.prefix .//g),而不是对$ {vars}进行过滤.

I am using maven for builds. Does any one know of a way to do this? Essentially what I want is something like the resources plugin filtering feature, but that does proper search and replace (like: s/my.package.prefix.//g), rather than filtering on ${vars}.

推荐答案

这可以通过antrun插件解决.首先,需要使用以下命令将源复制到目标目录:

This can be solved with the antrun plugin. Firstly the sources need to be copied to the target directory, with:

<build>
  ...
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </resource>
  </resources>
  ...
</build>

其次,您使用antrun插件的replace任务在prepare软件包阶段替换文件

Secondly you use the replace task of the antrun plugin to replace the files using the prepare package phase

<build>
    ...
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>prepare-package</phase>
        <configuration>
          <tasks>
            <replace token= "my.package.prefix." value="" dir="target/classes">                                 
              <include name="**/*.java"/>
            </replace>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  ...
</build>

这将在process-resources阶段将源文件复制到target/classs,在prepare-package阶段中搜索并替换target/classes目录中的文件,最后将它们压缩在package中阶段.

This will copy the source files to target/classes in the process-resources phase, do a search and replace on the files inplace in the target/classes directory in the prepare-package phase and finally they will jarred up in the package phase.

这篇关于复制资源时完全搜索和替换源文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆