在Maven中过滤源代码 [英] Filtering source code in Maven

查看:92
本文介绍了在Maven中过滤源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小的BeanShell脚本,用源代码中的实际行号替换了"__LINE__".在Ant中效果很好.

I wrote a small BeanShell script that replaces "__LINE__" with the actual line number in source code. It works well in Ant.

我正在寻找一种在Maven中过滤源代码的方法,以便我的BeanShell脚本可以生成一个新的源代码目录,然后对其进行编译.

I am looking for a way to filter source code in Maven so that my BeanShell script can generate a new source code directory that then gets compiled.

我了解资源文件过滤.是否有类似的源代码工具?

I know about resource file filtering. Is there any similar facility for source code?

推荐答案

几个月前,过滤源代码仍然很棘手,但是MOJO项目现在有了一个标准插件.现在,您可以使用经典的插件声明来做到这一点.

Filtering source code was still tricky some months ago, but there's now a standard plugin at the MOJO project. You can now do that with a classic plugin declaration.

要过滤源代码(例如,当您想在Java代码中使用常量来检索项目版本或artifactId)时,现在应使用

To filter source code (for example, when you'd like to have a constant in your Java code to retrieve the project version or artifactId), you should now use the templating-maven-plugin.

  1. 将在构建过程中应过滤的代码放入src/main/java-templates下,就像通常在src/main/java下对未过滤的源代码一样.在代码中使用${project.version}或来自POM的任何属性.

  1. Put your code that should be filtered during the build under src/main/java-templates as you would normally do under src/main/java for non filtered sources. Use ${project.version} or whatever property coming from the POM in your code.

只需输入以下内容:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>templating-maven-plugin</artifactId>
  <version>1.0-alpha-3</version> <!-- Be sure to use the last version. Check on the website's plugin -->
  <executions>
      <execution>
      <id>filter-src</id>
      <goals>
          <goal>filter-sources</goal>
      </goals>
      </execution>
  </executions>
</plugin>

  • 完成:-).放入src/main/java-templates中的代码将被过滤并添加到类路径中.

  • Be done :-). The code you put inside src/main/java-templates gets filtered and added to the classpath.

    用法非常简单(请参见此处的示例).

    The usage is very straightforward (see the example here).

    与Maven的配置相比,这更好地遵循了约定的思想.您基本上是在替换数十行XML行和一些技巧,以使工作干净整洁.

    This respects far better the idea of convention over configuration of Maven. You're basically replacing tens of XML lines and some hacks to get something done cleanly.

    旁注:例如,这在Eclipse上可以正常使用.

    Side note: this works fine with Eclipse for example.

    这篇关于在Maven中过滤源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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