简体   繁体   English

msbuild 何时使用新的“隐式”csproj 格式确定要构建的文件列表?

[英]When does msbuild determine list of files to build, using new 'implicit' csproj format?

Visual Studio 2019, with the new 'implicit' C# project format where you don't include files in the csproj. Visual Studio 2019,使用新的“隐式”C# 项目格式,您不会在 csproj 中包含文件。

There is a pre-build job which generates two.cs files, but apparently when msbuild invokes csc (after the pre-build event), it doesn't include these two files in the list of files to compile.有一个预构建作业会生成两个.cs 文件,但显然当 msbuild 调用 csc (在预构建事件之后)时,它不会将这两个文件包含在要编译的文件列表中。 I've even tried introducing a 30 second delay.我什至尝试过引入 30 秒的延迟。 So it looks like msbuild fixes the file list before running the pre-build event.所以看起来 msbuild 在运行预构建事件之前修复了文件列表。

Obviously, after the first time this is not a problem, but it's not great on a build server or new dev box.显然,在第一次之后这不是问题,但在构建服务器或新的开发盒上并不是很好。

Is there a clean solution to this?有一个干净的解决方案吗?

You will need to add these files to the Compile items manually if it is not already in there:如果还没有这些文件,则需要手动将这些文件添加到编译项目中:

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

This notation also supports wildcats ( Include="gen*.cs" )此表示法还支持通配符( Include="gen*.cs"

Found a solution:找到了解决方案:

Add InitialTargets attribute to project element:将 InitialTargets 属性添加到项目元素:

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

Change PreBuild to this将 PreBuild 更改为此

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

(Note that if you have Visual Studio open, this works almost too well, as the files will reappear automatically even without building!) (请注意,如果您打开了 Visual Studio,这几乎可以很好地工作,因为即使没有构建文件也会自动重新出现!)

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

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