【问题标题】:How to remove comments from properties file using maven build?如何使用 Maven 构建从属性文件中删除注释?
【发布时间】:2019-05-19 05:51:48
【问题描述】:

假设我的属性文件中有以下内容

#production db
db.url=com.some.prod.url
db.port=4565

#development db
#db.url=com.some.dev.url
#db.port=4577

构建完成后,我希望我的属性文件清除构建文件中所有注释的属性。所以,最终结果变成:

db.url=com.some.prod.url
db.port=4565

【问题讨论】:

  • 属性文件中的cmets有什么问题?
  • 不想在 dev/qa 服务器上公开与生产相关的属性,反之亦然。

标签: java maven maven-plugin properties-file


【解决方案1】:

你可以试试 Maven Antrun Plugin 和 ant task replaceregexp

【讨论】:

  • 让我检查并返回
【解决方案2】:

终于想通了。 使用maven-replacer-plugin,并用<blank>替换了<new line><hash><any set of characters>的正则表达式,即\r\n#(.*)

更新:为了能够在 jenkins(在我的例子中是 linux 实例)上执行此操作,添加了一个模式 \n#(.*)

       <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.2</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>target/**/*.properties</include>
                </includes>
                <commentsEnabled>false</commentsEnabled>
                <regex>true</regex>
                <replacements>
                    <replacement>
                        <token>\r\n#(.*)</token>
                        <value></value>
                    </replacement>
                    <!-- Following line was added in update(linux compatible) -->
                    <replacement>
                        <token>\n#(.*)</token>
                        <value></value>
                    </replacement>
                </replacements>
            </configuration>
        </plugin>

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2012-08-18
    相关资源
    最近更新 更多