Sunday, 1 July 2012

Difference between NAnt Readonly and Overwrite

I'm assuming you're aware of NAnt basics (add a comment if you're not and I'll write up a detailed article).

Intent
=====================
Often, there is a confusion between overwrite and readonly attributes for a property in NAnt. The intent of this small post is just to clear concepts revolving this.

readonly: Marking a property as readonly=true  means, later it can not be changed during NAnt execution. If there is an attempt to do so, build doesn't fails, but displays a warning message. 
Default is false

Remember: When you attempt to set a property value by passing it from command line, you'll always get a read only warning message if you are setting value of same property in your build file. This is so because, passing a value from command line supersedes any declaration of same property and is always readonly (Do not confuse yourself that why I'm getting warning message about readonly when I've set readonly="false". It is because by passing value from command, it is marked readonly by default and your build file code executes later)
Default is true

Example:1
<project name="property-example"> 
 <property name="something" readonly="true" value="111" /> 
 <property name="something" value="222" />
 <echo message="debug: ${something}"/> 
</project>
Output = 111 (with a warning message)

Example:2
<project name="property-example">
  <property name="something" readonly="false" value="111" />
  <property name="something" value="777"></property />
  <echo message="debug: ${something}" />
</project>
Output: 777  (No warning)

overwrite: Marking a property overwrite=true means if a property already exists  and is not read only can it be altered or not (If it is readonly, you'd never be able to change it anyways)?
<project name="property-example">
  <property name="something" overwrite="true" value="111" />
<property name="something" value="222" />
<property name="something" overwrite="false" value="333" />
<echo message="debug: ${something}" />
</project>

Output: 222 (First abc is set to 111. Later there is an attempt to change it to 222 and since by default overwrite is true, value updates to 222. In 3rd attempt, overwrite is false i.e. if there is any existing value of abc, do not change it, which causes value to remain unchanged)

Another example where you need overwrite=false:
If you're passing a value from command line and anywhere you have made an attempt to set the value of same property, you'd get a warning. To suppress warning, set overwrite=false. In this case, build file tag will be ignored and no warning will be raised.


Example
<project name="property-example">
  <property name="something" value="111"/>
</project>
And if you execute NAnt by setting value of "somthing" to something, like
Nant -f:abc.build -D:somthing="123"
You'll always see warning for the reason mentioned in readonly section of this topic. However, if you set overwrite="false", the warning will disappear because NAnt will not make an attempt to change readonly value passed from command line.

No comments:

Post a Comment