[英]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.