.NET Core - override default build targets

Viewed 5926

I have an angular project that I'd like to include in my Visual Studio solution. I added a new project with type ASP.NET Core 2/Empty that has all of the files, excluding the node_modules and similar ones via wildcard directly in csproj.

The problem I'm having is with overriding default build target - when build target is triggered I'd like to call "ng build", but this doesn't happen. Some materials about this can be found here and here

1 Attempted to override default build target:

 <Target Name="Build">
    <Message Importance="high" Text="Overriding default build" />
 </Target>

But still it doesn't execute.

2 Tried to specify DefaultTargets, but it doesn't get called just the same:

 <Project DefaultTargets="NgBuild" Sdk="Microsoft.NET.Sdk.Web">
   ...
 <Target Name="NgBuild">
    <Message Importance="high" Text="Overriding default build" />
 </Target>

Only after I add attribute BeforeTargets="Build" the message is there.

So the question is - how do I override default build behavior? Ultimately, I'd like to have a target that will invoke <Exec Command="ng build"> inside of it and doesn't produce an .exe and doesn't require any .cs files in the project.

2 Answers

Seems, you need to specify a new target to solve your problem.

For example in my .csproj I have following lines:

  <Target Name="TestTarget">
    <Exec Command="echo hello" />
    <Exec Command="echo goodbye" />
  </Target> 

To call them, I need to execute the following command:

dotnet msbuild /t:TestTarget

In this scenario, you can specify commands and order of execution. Hope, this will help.

Related