Christopher Pisz
Christopher Pisz

Reputation: 4010

Is there a macro or mechanism to get project version in MSBuild

I need to call a .bat file to so some custom packaging for my project output. Is there some manner of built in mechanism to get the version as a string, such that I can pass it as an argument to my bat file?

I see there is an assemblyInfo.cs file to version assemblies, but how can I get that information into my build process?

Upvotes: 1

Views: 1125

Answers (1)

Leo Liu
Leo Liu

Reputation: 76760

Is there a macro or mechanism to get project version in MSBuild

The answer is yes. If you don't mind editing the Visual Studio project file, then there is a simple solution that allows you to use a macro which looks like this:@(VersionNumber):

To accomplish this, unload your project. Then at the very end of the project, just before the end-tag, place below scripts:

Is there a way to do this?

The answer is yes. If you don't mind editing the Visual Studio project file, then there is a simple solution that allows you to use a macro which looks like this:@(VersionNumber):

To accomplish this, unload your project. Then at the very end of the project, just before the end-tag, place below scripts:

<PropertyGroup>
   <PostBuildEventDependsOn>
     $(PostBuildEventDependsOn);
     PostBuildMacros;
   </PostBuildEventDependsOn>    
</PropertyGroup>

<Target Name="PostBuildMacros">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="Targets" />
  </GetAssemblyIdentity>
  <ItemGroup>
    <VersionNumber Include="@(Targets->'%(Version)')"/>
  </ItemGroup>
</Target>

Now as promised, the assembly version is available to your post build event with this macro. So we could get it by the command in the build event:

echo @(VersionNumber)

And we could write it to the bat file with the command line:

echo @(VersionNumber) > $(TargetDir)install.bat

Hope this helps.

Upvotes: 2

Related Questions