在 dotnet core csproj 文件中使用 Pre 和 Post 构建操作 [英] Using Pre and Post build actions in dotnet core csproj files

查看:17
本文介绍了在 dotnet core csproj 文件中使用 Pre 和 Post 构建操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a .NET Core novice and I am trying to set up a pre-build action in my csproj file. According to [1], we can use Target element to specify a pre-build step as follows:

<Target Name="MyPreCompileTarget" BeforeTargets="Build">
        <Exec Command="generateCode.cmd"/>
</Target>

然而,这个元素似乎没有被 MSBuild 工具选中.我的完整 csproj 文件如下:

However, this element does not seem to be picked up by the MSBuild tool. My complete csproj file is given below:

<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 在这种情况下是命令外壳程序 (cmd.exe) 的内置程序,因此它赢得了不行.

echo is a builtin of the command shell (cmd.exe) in that case so it won't work.

如果你只使用例如generateCode,msbuild 还会根据您运行的平台查找与该名称匹配的 .bat.sh 文件.

If you only use e.g. generateCode, msbuild will also look for .bat or .sh files matching that name depending on the platform you run on.

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

You can run the dotnet build command with /v:diag to get a full diagnostic output.

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

You can also verify if your target is actually run by adding a task like this inside the target:

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

此外,由于 echo 在 mac 上可用,当我在 Mac 上运行它时,您的项目文件会执行预期的操作:

Also, since echo is available on mac, your project file does the expected when I run it on a 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)

这篇关于在 dotnet core csproj 文件中使用 Pre 和 Post 构建操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆