预构建 MSBuild 任务更新 AssemblyInfo 与构建的 exe 不同步

发布于 2024-07-13 09:25:46 字数 804 浏览 8 评论 0原文

我在 Visual Studio 2008 中使用调用 msbuild 的预构建任务:

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

在 version.targets 内,我正在更新 AssemblyInfo.cs 文件以替换版本信息:

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

当我通过 Visual Studio 2008 构建项目时,它的构建没有任何问题。

但是,当我查看生成的 exe 的版本信息时,它包含上一个时间戳,即使 AssemblyInfo.cs 已更改为“正确”的时间戳。

似乎主编译任务看不到预构建的更改,并且它总是落后一处。

对我做错了什么有什么想法吗?

I am using a pre-build task in Visual Studio 2008 that invokes 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:

   <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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

合约呢 2024-07-20 09:25:46

我不认为你做错了什么——这是一个错误。

我已经在此处报告了它 - 检查您是否可以重现它并添加一个验证,也许我们可以通过 MS 修复它。

编辑:我尝试了“Si”的建议来更新“BeforeBuild”事件中的文件 - 但是我仍然在 Visual Studio 2008/SP1 中得到相同的错误结果。

更新/解决方法:微软已对错误报告做出回应。 作为解决方法,您可以将其添加

<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>

到 csproj 文件中。

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.

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.

UPDATE/WORKAROUND: MS has responded to the bug report. As a workaround you can add

<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>

to your csproj file.

请恋爱 2024-07-20 09:25:46

FALSE技巧+1,尽管接受的解决方案和链接的文章都没有指定该行应该添加到第一个 .csproj 文件中的 元素

这也是 Visual Studio 独有的问题,因为从命令行在同一个 .csproj 文件上调用 msbuild(没有技巧)将看到生成的代码文件已编译立即(而不是以前的版本)。

另外,我想建议该主题使用以下标记,因为我必须进行大量挖掘才能找到它:

  1. VisualStudio
  2. 编译器
  3. BeforeBuild
  4. 生成的
  5. .NET
  6. c#

+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.

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).

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
  4. generated
  5. .NET
  6. c#
可遇━不可求 2024-07-20 09:25:46

有趣的是,我编写了自己的自定义任务,该任务与 BeforeBuild 挂钩并且工作正常。 通过命令行从 VS 或 MSBuild 从未遇到过问题。 所以我想说更多地探索 BeforeBuild...

我知道,因为我们使用构建服务器 (CruiseControl.NET) 内部版本号作为版本的“构建”部分,该部分内置于所有程序集中(我们共享相同的 AssemblyInfo.cs)解决方案中跨程序集的 AssemblyVersion 和 AssemblyFileVersion),然后将其推送(通过 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>

如果您进行一些谷歌搜索,您应该找到其他示例...抱歉,我不能给您 UpdateVersion 代码,我需要我工作的许可,但如果您在网上找不到任何合适的内容,自定义任务很容易写下来,上面的内容应该有帮助。

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...

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.

<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.

在你怀里撒娇 2024-07-20 09:25:46

使用如下所示的内容来更改任何文件的任何内容。 每次创建构建时,我都会更改 Hallo.aspx 中的构建日期值。


例如 Hallo.aspx 内容

12-23.2011


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

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.


e.g. Hallo.aspx content

<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)" /> 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文