gpt4 book ai didi

msbuild - 在 dotnet core csproj 文件中使用 Pre 和 Post 构建操作

转载 作者:行者123 更新时间:2023-12-01 13:35:07 26 4
gpt4 key购买 nike

我是 .NET Core 新手,我正在尝试在我的 csproj 文件中设置预构建操作。根据 [1],我们可以使用 Target 元素来指定预构建步骤,如下所示:

<Target Name="MyPreCompileTarget" BeforeTargets="Build">
<Exec Command="generateCode.cmd"/>
</Target>
但是,这个元素似乎没有被 MSBuild 工具选中。我的完整 csproj 文件如下:
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<Folder Include="wwwroot\"/>
</ItemGroup>


<Target Name="MyPreCompileTarget" BeforeTargets="Build">
<Exec Command="echo meow meow"/>
</Target>

<ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" Version="7.0.0"/>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="1.0.3"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\my-lib\<my-lib>.csproj"/>
</ItemGroup>

</Project>
引用
[1] - https://docs.microsoft.com/en-us/dotnet/articles/core/tools/project-json-to-csproj#the-csproj-format

最佳答案

echo在这种情况下是命令 shell 程序 ( cmd.exe ) 的内置函数,因此它将无法工作。

如果您只使用例如generateCode , msbuild 还将查找 .bat.sh与该名称匹配的文件,具体取决于您运行的平台。

您可以运行 dotnet build命令与 /v:diag以获得完整的诊断输出。

您还可以通过在目标中添加这样的任务来验证您的目标是否实际运行:

<Message Importance="high" Text="Test Message" />

此外,由于 echo在 mac 上可用,当我在 Mac 上运行它时,您的项目文件按预期执行:
MacBook-Pro:footest martin$ dotnet build
Microsoft (R) Build Engine version 15.3.234.47922 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

footest -> /Users/martin/tmp/footest/bin/Debug/netcoreapp1.1/footest.dll
meow meow

Build succeeded.
0 Warning(s)
0 Error(s)

关于msbuild - 在 dotnet core csproj 文件中使用 Pre 和 Post 构建操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44111589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com