C# 预构建 MSBuild 任务以更新与构建的 exe 不同步的 AssemblyInfo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/541546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pre-build MSBuild task to update AssemblyInfo not in sync with built exe
提问by Richard Morgan
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\(".*"\)\]"
ReplacementText="AssemblyInformationalVersion("Product $(ConfigurationString) ($(buildDate))")]"
/>
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?
关于我做错了什么的任何想法?
采纳答案by laktak
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. As a workaround you can add
更新/解决方法:MS 已响应错误报告。作为解决方法,您可以添加
<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>
to your csproj file.
到您的 csproj 文件。
回答by si618
Interesting, I wrote my own custom task which is hooked into BeforeBuild and this is working fine. Never had a problem from VS or MSBuild via command line. So I would say explore BeforeBuild some more...
有趣的是,我编写了自己的自定义任务,该任务连接到 BeforeBuild 中,并且工作正常。通过命令行从未遇到过来自 VS 或 MSBuild 的问题。所以我会说探索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) 构建号作为版本的“构建”部分,它被构建到所有程序集中(我们在解决方案中的程序集之间共享相同的 AssemblyInfo.cs 和 AssemblyVersion)和然后将其(通过 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 代码,我需要我的工作许可,但是如果您在网上找不到任何合适的东西,自定义任务很容易写,上面应该有帮助。
回答by Olivier Dagenais
+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.
<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>
技巧+1 ,尽管接受的解决方案和链接的文章都没有指定应将该行添加到文件中的第一个<PropertyGroup>
元素.csproj
。
It is also a problem with Visual Studio only, as invoking msbuildfrom 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 的问题,因为从同一文件的命令行调用msbuild.csproj
(没有技巧)将立即看到生成的代码文件(而不是以前的版本)。
Also, I would like to recommend that this topic be tagged with the following, as I had to dig a lot to find it:
另外,我想建议用以下标记这个主题,因为我不得不挖掘很多才能找到它:
- VisualStudio
- compiler
- BeforeBuild
- generated
- .NET
- c#
- 视觉工作室
- 编译器
- 建造前
- 生成
- 。网
- C#
回答by Gomes
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.
使用类似下面的内容来更改任何文件的任何内容。在这里,我每次创建构建时都会更改 halo.aspx 中的构建日期值。
e.g. 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[^<>]+(?=[<])" ignoreCase="true"
ReplacementText="BuildDate: $(Day)-$(Month)-$(Year)" />