如何在Maven插件的上下文中修改源代码?

时间:2013-07-19 10:49:07

标签: maven maven-plugin

我想编写一个Maven插件,它可以在构建期间在特定的源文件中进行简单的查找和替换。这甚至可能吗?

1 个答案:

答案 0 :(得分:3)

Yes, it is possible. You should read Guide to Developing Java Plugins.

Maybe instead of creating a new plugin, you can use an existing one.

Example com.google.code.maven-replacer-plugin:replacer:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <configuration>
        <file>FILE PATH (example: ${basedir}/src/main/webapp/index.html)</file>
        <token>SEARCHED TEXT (example: .css)</token>
        <value>REPLACEMENT (example: .min.css)</value>
    </configuration>
</plugin>

If you want to replace XML data, then you should use CDATA sections:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <configuration>
        <file>${basedir}/src/main/webapp/WEB-INF/web.xml</file>
        <token><![CDATA[SEARCHED TEXT (example: <foo>)]]></token>
        <value><![CDATA[REPLACEMENT (example: <foo2>)]]></value>
    </configuration>
</plugin>