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

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

问题描述

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

I am a .net-core novice and I am trying to setup 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] 推荐答案

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.

您可以将dotnet build命令与/v:diag一起运行以获取完整的诊断输出.

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天全站免登陆