visual-studio - 在 CoreBuild 之前运行目标?

标签 visual-studio msbuild

我正在向我的项目中添加一个自定义 .tt 模板生成目标,以便在 CoreBuild 之前运行,并且似乎有两种方法可以做到:

<Project...>
    <Target Name="TransformOnBuild" AfterTargets="BeforeBuild">
</Project>


<Project...>
    <Target Name="TransformOnBuild" BeforeTargets="CoreBuild">
</Project>

如果我的目标应该在我的项目构建之前运行,因为项目依赖于它,我使用后者会更好吗?我已经看到前者用于生成文本模板之类的事情,但这似乎是一种不可靠的方法,因为它可能会在 CoreBuild 之后运行,这为时已晚。还是有什么原因为什么 AfterTargets="BeforeBuild" 仍然可以保证在核心构建之前运行?

我还看过 BeforeTargets="BeforeBuild" ,它会更早地构建。这是放置`.tt 文本生成目标的更好地方吗?

最佳答案

基于@stjin 的回答,似乎正在使用一个很好的解决方案

BeforeTargets="CoreCompile" DependsOnTargets="PrepareForBuild"

这就是 .net sdk(用于 .net 核心/标准项目的新型 csproj) is doing for automatic AssemblyInfo.cs generation

它使用以下注释来解释原因:

Note that this must run before every invocation of CoreCompile to ensure that all compiler runs see the generated assembly info. There is at least one scenario involving Xaml where CoreCompile is invoked without other potential hooks such as Compile or CoreBuild, etc., so we hook directly on to CoreCompile. Furthermore, we must run after PrepareForBuild to ensure that the intermediate directory has been created.



请注意,“中间目录”(在本例中为 obj/[TargetFramework])是在本例中放置输出 .cs 文件的位置,这也可能是您想要执行的操作。

关于visual-studio - 在 CoreBuild 之前运行目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44043918/

相关文章:

vb.net - 在 Visual Studio 中使用自动构建进行条件编译

node.js - 如何从 gulp 调用执行 PowerShell 脚本?

msbuild - 无法找到 csc 错误 cs0006 元数据文件,使用 TFS 2012 构建 .net 3.5 应用程序

c# - 重命名类 Visual Studio 2017

c++ - VS2008中如何开启 "auto-build"选项

visual-studio - WPF 应用程序 : Visual Studio vs. 表达式混合

msbuild - clickonce Bootstrap 和 msbuild 的问题

visual-studio-2008 - MSBuild,使用不同的配置构建依赖项目

visual-studio - 你如何保持 Visual Studio 选项卡的排序

visual-studio - Visual Studio Community for Mac - 将浏览器从 Safari 更改为 Chrome