Consider the following ITask:
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace My.MSBuild.Tasks
{
public class FormatRevision : Task
{
#region Public Properties
[Required]
public int Revision { get; set; }
[Required]
public string MajorVersion { get; set; }
[Output]
public string OutputVersion { get; private set; }
#endregion
#region ITask Methods
public override bool Execute()
{
OutputVersion = string.Format("{0}.{1}"
, MajorVersion
, Revision < 10 ?
"00" + Revision : Revision < 100 ?
"0" + Revision : Revision.ToString());
Log.LogMessage("Revision: {0} -> Output Version: {1}"
, Revision, OutputVersion);
return true;
}
#endregion
}
}
MSBuild target (formatvesion.proj):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="FormatRevision">
<FormatRevision MajorVersion="$(MajorVersion)" Revision="$(Revision)">
<Output TaskParameter="OutputVersion" PropertyName="FormattedVersion"/>
</FormatRevision>
</Target>
<UsingTask TaskName="My.MSBuild.Tasks.FormatRevision" AssemblyFile="My.MSBuild.Tasks.dll" />
</Project>
Invoked by command:
msbuild formatvesion.proj /t:FormatRevision /p:MajorVersion=1.0;Revision=7
Alternatively, if you wish to use CreateProperty:
<PropertyGroup>
<FileParserVersion>File Parser $(Major).$(Minor).$(Build) Build $(Revision)</FileParserVersion>
<VersionComment>Automated build: $(FileParserVersion)</VersionComment>
</PropertyGroup>
<PropertyGroup>
<PaddedRevision Condition="$(Revision) < 1000">$(Revision)</PaddedRevision>
<PaddedRevision Condition="$(Revision) < 100">0$(Revision)</PaddedRevision>
<PaddedRevision Condition="$(Revision) < 10">00$(Revision)</PaddedRevision>
</PropertyGroup>
<Target Name="test">
<CreateProperty
Value="FILEPARSER_$(Major)_$(Minor)_$(Build)_BUILD_$(PaddedRevision)">
<Output TaskParameter="Value" PropertyName="MyFileVersion" />
</CreateProperty>
<Message Text="$(VersionComment) -> $(MyFileVersion)" />
</Target>