msbuild任务读取DLL的AssemblyFileVersion

时间:2011-09-01 02:55:31

标签: msbuild versioning

我需要阅读dll的AssemblyFileVersion而不是Version。我试过了:

<Target Name="RetrieveIdentities">
    <GetAssemblyIdentity AssemblyFiles="some.dll">
        <Output
            TaskParameter="Assemblies"
            ItemName="MyAssemblyIdentities"/>
    </GetAssemblyIdentity>
    <Message Text="%(MyAssemblyIdentities.FileVersion)" />
</Target>

脚本运行但不输出任何内容。如果我将FileVersion更改为Version,则会正确输出AssemblyVersion。如何使用我的脚本获取AssemblyFileVersion

2 个答案:

答案 0 :(得分:5)

借用this answer,我能够创建自定义MSBuild任务:

<UsingTask
  TaskName="GetFileVersion"
  TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">

  <ParameterGroup>
    <AssemblyPath ParameterType="System.String" Required="true" />
    <Version ParameterType="System.String" Output="true" />
  </ParameterGroup>
  <Task>
    <Using Namespace="System.Diagnostics" />
    <Code Type="Fragment" Language="cs">
      <![CDATA[
      Log.LogMessage("Getting version details of assembly at: " + this.AssemblyPath, MessageImportance.High);

      this.Version = FileVersionInfo.GetVersionInfo(this.AssemblyPath).FileVersion;  
    ]]>
    </Code>
  </Task>
</UsingTask>

然后从目标中消耗它:

<GetFileVersion AssemblyPath="some.dll">
  <Output TaskParameter="Version" PropertyName="MyAssemblyFileVersion" />
</GetFileVersion>
<Message Text="File version is $(MyAssemblyFileVersion)" />

答案 1 :(得分:4)

MSBuild Extension包具有可能有用的MaxAssemblyFileVersion属性。

<强>更新

documentation开始,GetAssemblyIdentity任务不会返回FileVersion

  

Assemblies参数输出的项目包含项目元数据   名为Version,PublicKeyToken和Culture的条目。

另外,请参阅以下StackOverflow帖子。

Read AssemblyFileVersion from AssemblyInfo post-compile