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