在dotnet核心csproj文件

时间:2017-05-22 11:22:17

标签: msbuild .net-core

我是.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

1 个答案:

答案 0 :(得分:2)

在这种情况下,

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)
相关问题