考虑以下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目标(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>
由命令调用:
msbuild formatvesion.proj /t:FormatRevision /p:MajorVersion=1.0
或者,如果您希望使用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>