问题
In my Android project I have the following property set in my project.properties file
proguard.config=proguard.cfg
and I need a custom macro that will somehow set and unset this property.
How do I set unset this property using macro and regular expression? two things I am not clear on is how to set this to an empty property value. Would that be just proguard.config= or proguard.config=''
What would be the Macro for doing this?
<macrodef name="turn-on-proguard">
<sequential>
<replaceregexp file="./project.properties"
match='proguard.config="(.*)"'
replace='proguard.config=proguard.cfg'
byline="false">
</replaceregexp>
</sequential>
</macrodef>
<macrodef name="turn-off-proguard">
<sequential>
<replaceregexp file="./project.properties"
match='proguard.config="(.*)"'
replace='proguard.config='
byline="false">
</replaceregexp>
</sequential>
</macrodef>
Would this work? Update. turn-proguard-off does nothing.
回答1:
Your solution is very unclear for me. Why don't you escape the dot character? Why do you use quotation marks and grouping in your regexp? This is working script:
<macrodef name="turnonproguard">
<sequential>
<replaceregexp file="project.properties"
match='proguard\.config=.*'
replace='proguard.config=proguard.cfg'
byline="false"/>
</sequential>
</macrodef>
<macrodef name="turnoffproguard">
<sequential>
<replaceregexp file="project.properties"
match='proguard\.config=.*'
replace='proguard.config='
byline="false"/>
</sequential>
</macrodef>
<target name="on">
<turnonproguard/>
</target>
<target name="off">
<turnoffproguard/>
</target>
来源:https://stackoverflow.com/questions/10629726/how-to-use-custom-ant-rule-regexpression-to-change-property-in-propertyfile