[英]How to remove commented properties from properties file using ant build?
我想通過ant build從屬性文件中刪除注釋的屬性。 為了安全起見,我不想在沙盒服務器上公開我的生產屬性。
屬性文件:
#production properties
#redis.master.url=redis.prod.master.compny.com
#redis.slave.url=redis.prod.slave.compny.com
#sandboxproperties
redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com
因此,我的war軟件包應具有以下屬性文件:
redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com
根據Ant docs :
Apache Ant提供了一個用於編輯屬性文件的可選任務。 想要對應用程序服務器和應用程序的配置文件進行無人看管的修改時,這非常有用。 當前,該任務維護着一個工作的屬性文件,該文件可以添加屬性或對現有屬性進行更改。 從Ant 1.8.0起,將保留原始屬性文件的注釋和布局。
因此,根據您使用的是哪個版本的ant構建,您也許可以從屬性文件中刪除注釋。
您可以使用ant中的腳本來做到這一點:
<macrodef name="remove-properties-comments">
<attribute name="inFile" />
<attribute name="outFile" />
<sequential>
<script language="javascript">
<![CDATA[
// get the arguments
var inFile = "@{inFile}"
var outFile = "@{outFile}"
// or get properties from the ant environment
// eg: <property name="property.from.ant.project" value="value" />
// var antProp = project.getProperty("property.from.ant.project");
// load Java types
var File = Java.type("java.io.File")
var PrintWriter = Java.type("java.io.PrintWriter")
var Scanner = Java.type("java.util.Scanner")
// init reader and writer
var reader = new Scanner(new File(inFile))
var writer = new PrintWriter(outFile)
// if previous line ended in '\' then it is a muliline property
// so the following line should always be included
var multiline = false
while (reader.hasNextLine()) {
var line = reader.nextLine();
// you could exclude blank lines too if you want
if (multiline || !(line.startsWith("#") || line.startsWith("!"))) {
writer.println(line);
}
multiline = line.endsWith("\\");
}
]]>
</script>
</sequential>
</macrodef>
<target name="test">
<remove-properties-comments inFile="path/to/inFile.properties" outFile="path/to/outFile.properties" />
</target>
我只是通過使用replaceregexp
這個問題。
<target>
<replaceregexp match="\n#(.*)" replace="" flags="g" byline="false">
<fileset dir="${build.home}/WEB-INF/classes" includes="**/*.properties" />
</replaceregexp>
</target>
這里\\n#(.*)
與<newline>
( \\n
\\n#(.*)
匹配,后跟#
后跟任意字符集( *
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.