【问题标题】:When does msbuild determine list of files to build, using new 'implicit' csproj format?msbuild 何时使用新的“隐式”csproj 格式确定要构建的文件列表?
【发布时间】:2019-09-27 09:43:29
【问题描述】:

Visual Studio 2019,使用新的“隐式”C# 项目格式,您不会在 csproj 中包含文件。

有一个预构建作业会生成两个 .cs 文件,但显然当 msbuild 调用 csc 时(在预构建事件之后),它不会将这两个文件包含在要编译的文件列表中。我什至尝试过引入 30 秒的延迟。所以看起来 msbuild 在运行预构建事件之前修复了文件列表。

显然,在第一次之后这不是问题,但在构建服务器或新的开发盒上不是很好。

有没有干净的解决方案?

【问题讨论】:

    标签: c# msbuild csproj


    【解决方案1】:

    如果还没有这些文件,您需要手动将这些文件添加到编译项目中:

    <Target Name="GenerateStuff" BeforeTargets="BeforeBuild">
      <Exec Command="generate foo.cs" />
      <ItemGroup>
        <Compile Include="foo.cs" Exclude="@(Compile)" />
      </ItemGroup>
    </Target>
    

    此表示法还支持通配符 (Include="gen*.cs")

    【讨论】:

      【解决方案2】:

      找到解决办法:

      将 InitialTargets 属性添加到项目元素:

      <Project Sdk="Microsoft.NET.Sdk" InitialTargets="Generation">
      

      将 PreBuild 更改为此

      <Target Name="Generation">
        <Exec Command="whatever" />
      </Target>
      

      (请注意,如果您打开了 Visual Studio,这几乎工作得很好,因为即使不构建文件也会自动重新出现!)

      【讨论】:

        猜你喜欢
        • 2020-05-14
        • 2011-05-15
        • 1970-01-01
        • 2013-06-23
        • 2014-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多