简体   繁体   English

预构建MSBuild任务以更新AssemblyInfo与内置exe不同步

[英]Pre-build MSBuild task to update AssemblyInfo not in sync with built exe

I am using a pre-build task in Visual Studio 2008 that invokes msbuild: 我在Visual Studio 2008中使用调用msbuild的预构建任务:

C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe $(MSBuildProjectDirectory)\version.targets /p:Configuration=$(ConfigurationName)

Inside version.targets, I am updating the AssemblyInfo.cs file to replace version information: 在version.targets中,我正在更新AssemblyInfo.cs文件以替换版本信息:

   <FileUpdate
        Encoding="ASCII"
        Files="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"
        Regex="AssemblyInformationalVersion\(&quot;.*&quot;\)\]" 
        ReplacementText="AssemblyInformationalVersion(&quot;Product $(ConfigurationString) ($(buildDate))&quot;)]"
    />

When I build the project through Visual Studio 2008, it builds without any problems. 当我通过Visual Studio 2008构建项目时,它构建没有任何问题。

But when I look at the resulting exe's version information, it contains the previous time stamp even though the AssemblyInfo.cs has been changed with the "correct" one. 但是当我查看生成的exe的版本信息时,它包含上一个时间戳,即使AssemblyInfo.cs已经使用“正确”更改了。

It seems that the pre-build's changes aren't seen by the main compilation task and it's always one behind. 似乎主编译任务没有看到预编译的变化,它总是落后。

Any ideas of what I'm doing wrong? 我做错了什么想法?

I don't think you are doing anything wrong - it's a bug. 我不认为你做错了什么 - 这是一个错误。

I have reported it here - check if you can reproduce it and add a validation, maybe we can get it fixed by MS. 我在这里报告 - 检查你是否可以重现它并添加验证,也许我们可以通过MS修复它。

EDIT: I tried the suggestion by "Si" to update the file in the "BeforeBuild" event - however I still get the same wrong result with Visual Studio 2008/SP1. 编辑:我尝试了“Si”的建议来更新“BeforeBuild”事件中的文件 - 但是我仍然得到与Visual Studio 2008 / SP1相同的错误结果。

UPDATE/WORKAROUND : MS has responded to the bug report. 更新/解决方法 :MS已回复错误报告。 As a workaround you can add 作为一种解决方法,您可以添加

<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>

to your csproj file. 到你的csproj文件。

+1 for the <UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable> trick, although neither the accepted solution nor the linked article specified that the line should be added to the first <PropertyGroup> element in the .csproj file . 虽然接受的解决方案和链接文章都没有指定该行应该添加到.csproj文件中的第一个 <PropertyGroup>元素 ,但<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>技巧的+1。

It is also a problem with Visual Studio only, as invoking msbuild from the command-line on the same .csproj file (without the trick) will see the generated code files compiled right away (and not the previous versions). 它也只是Visual Studio的一个问题,因为在同一个.csproj文件中从命令行调用msbuild (没有技巧)会立即看到生成的代码文件(而不是以前的版本)。

Also, I would like to recommend that this topic be tagged with the following, as I had to dig a lot to find it: 此外,我想建议将此主题标记为以下内容,因为我必须挖掘很多才能找到它:

  1. VisualStudio 视觉工作室
  2. compiler 编译器
  3. BeforeBuild BeforeBuild
  4. generated 产生
  5. .NET 。净
  6. c# C#

Interesting, I wrote my own custom task which is hooked into BeforeBuild and this is working fine. 有意思的是,我编写了自己的自定义任务,它挂在了BeforeBuild中,这很好用。 Never had a problem from VS or MSBuild via command line. 从未通过命令行从VS或MSBuild出现问题。 So I would say explore BeforeBuild some more... 所以我会说更多探索BeforeBuild ......

I know because we use our build server (CruiseControl.NET) build number as the "Build" part of the version, which is built into all assemblies (we share the same AssemblyInfo.cs for AssemblyVersion and AssemblyFileVersion across assemblies in the solution) and this is then pushed (via FileUpdate task) to a variable in our WiX project and also used to label the MSI file name. 我知道因为我们使用构建服务器(CruiseControl.NET)构建号作为版本的“构建”部分,它构建在所有程序集中(我们在解决方案中的程序集中为AssemblyVersion和AssemblyFileVersion共享相同的AssemblyInfo.cs)和然后将其(通过FileUpdate任务)推送到我们的WiX项目中的变量,并用于标记MSI文件名。

<Target Name="BeforeBuild">
  <CallTarget Targets="UpdateAssemblyInfo" />
</Target>

<Target Name="UpdateAssemblyInfo" Condition="'$(CIBuildNumber)' != ''">
  <UpdateVersion Attribute="AssemblyFileVersion"
    AssemblyInfo=".\Properties\AssemblyInfo.cs" 
    BuildNumber="$(CIBuildNumber)" />
</Target>

If you do some googling you should find other examples...sorry I can't give you the UpdateVersion code, i'd need permission from my work, but if you can't find anything suitable on the net, custom tasks are easy to write and the above should help. 如果你做一些谷歌搜索你应该找到其他的例子...抱歉我不能给你UpdateVersion代码,我需要我的工作许可,但如果你在网上找不到合适的东西,自定义任务很容易写,以上应该有所帮助。

Use something like below to change any content for any file. 使用下面的内容更改任何文件的任何内容。 Here i am changing Build date value in hallo.aspx when every time i create a build. 这里我每次创建构建时都会在hallo.aspx中更改构建日期值。


eg Hallo.aspx content 例如Hallo.aspx的内容

<BuildDate> 12-23.2011 </BuildDate> <BuildDate> 12-23.2011 </ BuildDate>


<!-- regular expression to get value between html node: "[^<>]+(?=[<])" --> 
  <FileUpdate Files="$(AboutDir)Hallo.aspx" 
    Regex="Builddate[^&lt;&gt;]+(?=[&lt;])" ignoreCase="true" 
    ReplacementText="BuildDate: $(Day)-$(Month)-$(Year)" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM