简体   繁体   English

目标 BeforeBuild 在 csproj 中不起作用

[英]Target BeforeBuild doesn't work in csproj

I'm trying to use the target event "BeforeBuild" in.csproj (vs2017), but it's not working.我正在尝试在 .csproj (vs2017) 中使用目标事件“BeforeBuild”,但它不起作用。 Someone would know what is wrong:有人会知道出了什么问题:

<Project DefaultTargets="BeforeBuild" Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <Target Name="BeforeBuild">
    <Message Text="Test123"></Message>
  </Target>
</Project>

The expected result is a message: Test123 on output.预期结果是一条消息:Test123 on output。

[]s []秒

BeforeBuild dosen't working in csproj BeforeBuild无法在csproj中工作

That because Before/AfterTarget in csproj gets overridden by SDKs target file . 那是因为csproj中的Before / AfterTarget被SDK目标文件覆盖

if you're using the new Sdk attribute on the Project element, it's not possible to put a target definition after the default .targets import. 如果在Project元素上使用新的Sdk属性,则无法在默认.targets导入之后放置目标定义。 This can lead to targets that people put in their project files unexpectedly not running, with no indication why unless you examine the log file and see the message that the target has been overridden. 这可能导致人们放入其项目文件中的目标意外地无法运行,除非您检查日志文件并看到目标已被覆盖的消息,否则没有任何指示。

dsplaisted have filed Microsoft/msbuild#1680 for this issue. dsplaisted已针对此问题提交Microsoft / msbuild#1680 As a workaround, you can do the following: 作为一种解决方法,您可以执行以下操作:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PreBuildEvent />

  </PropertyGroup>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

  <Target Name="BeforeBuild">
    <Message Text="Test123"></Message>
  </Target>

Or: 要么:

  <Target Name="test" BeforeTargets="Build">
    <Message Text="Test123" />
  </Target>

From the official docs :来自官方文档

Warning警告

Be sure to use different names than the predefined targets listed in the table in the previous section (for example, we named the custom build target here CustomAfterBuild , not AfterBuild ), since those predefined targets are overridden by the SDK import which also defines them.请务必使用与上一节表中列出的预定义目标不同的名称(例如,我们将自定义构建目标命名为CustomAfterBuild而不是 AfterBuild ),因为这些预定义目标被 SDK 导入覆盖,它也定义了它们。 You don't see the import of the target file that overrides those targets, but it is implicitly added to the end of the project file when you use the Sdk attribute method of referencing an SDK.您看不到覆盖这些目标的目标文件的导入,但是当您使用引用 SDK 的 Sdk 属性方法时,它会隐式添加到项目文件的末尾。

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

相关问题 为什么Exec Command在netstandard2.0的.csproj中不起作用? - Why doesn't Exec Command work in a .csproj for netstandard2.0? 如何从 BeforeBuild 任务中的 csproj 文件中删除节点? - How to Remove node from csproj file in BeforeBuild Task? 当我在.csproj文件中使用通配符时,为什么Intellisense在VS2010中不起作用? - Why doesn't Intellisense work in VS2010 when I use wildcards in my .csproj file? 在某些项目中,BeforeBuild 目标永远不会被执行 - BeforeBuild target never gets executed in some projects 如何解析BeforeBuild目标中的DefineConstants值? - How can I parse DefineConstants value in a BeforeBuild target? SetEnvironmentVariable对%var%\\ target.extension不起作用 - SetEnvironmentVariable doesn’t work for %var%\target.extension 当目标应用程序以管理员身份运行时,ShowWindow功能不起作用 - ShowWindow Function Doesn't Work When Target Application Is Run As Administrator .csproj文件无法正确导入其他项目 - .csproj file doesn't properly import other project Nuget 不还原解决方案的包,但在为 .csproj 执行时有效 - Nuget doesn't restore packages for solution, but works when executed for .csproj Visual Studio 不支持特定的 csproj 文件 - Visual studio doesn't support specific csproj file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM