Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Pre and Post build actions in dotnet core csproj files

Tags:

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>

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>

References

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

like image 619
Sameera Jayaseckara Avatar asked May 22 '17 11:05

Sameera Jayaseckara


1 Answers

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

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.

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" />

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)
like image 178
Martin Ullrich Avatar answered Sep 25 '22 10:09

Martin Ullrich