Reputation: 3556
I've added a target section to my csproj file like this.
<Target Name="Spa">
<Exec Command="ng --version" WorkingDirectory="../Spa" />
<Exec Command="ng build --prod" WorkingDirectory="../Spa" />
<Exec Command="del .\wwwroot\* /F /Q /S" />
<Exec Command="copy ..\Spa\dist\Spa\* .\wwwroot" />
</Target>
When I'm executing it using the command below, it does precisely what it's supposed to.
dotnet msbuild /t:Spa
However, it'd be nice if the target could be invoked just prior to the execution of publishing within Visual Studio (b+h+Tab+Enter).
I've read somewhere that it's possible and the docs claim that BeforePublish is the correct target name. However, when I change the name Spa to BeforePublish, I'm not getting the effect of my SPA being built and copied over.
What am I missing and how do I automate the process?
Upvotes: 2
Views: 3455
Reputation: 76910
How to run MsBuild with specific target when publishing?
I post an answer here to make sure this question more clear.
For this question, The first thing to note is that it is related to the project type. If you are publish a WPF/Winodws Forms project, the <Target Name="BeforePublish ">
should be works as expected. That because these project types include the target publish
by default, so the target "BeforePublish" will work as expected.
However, the web project not contain this default target Publish
, so if you use <Target Name="BeforePublish ">
in the web project, it will not executed as expected. To resolve this issue, we could add a BeforeTargets="BeforePublish"
to the target, like:
<Target Name="Spa" BeforeTargets="BeforePublish">
...
</Target>
Then Overriding "DependsOn" Properties:
<PropertyGroup>
<BuildDependsOn>
BeforeBuild;
CoreBuild;
AfterBuild;
BeforePublish
</BuildDependsOn>
</PropertyGroup>
Or you can simple add AfterTargets="Build"
to the target Spa
, it should woks fine:
<Target Name="Spa" AfterTargets="Build">
...
</Target>
The second thing to note is that whether the target section needs to be at the bottom of the csproj file is depends on the style csproj. Just as Martin said, if you are in the old style csproj, those targets BeforeBuild
, AfterBuild
are actually pre-defined in the Microsoft.Common.targets
file that contains the Visual Studio Build process, so we have to set our custom target at the bottom of the csproj to overwrite it in the Microsoft.Common.targets
file. If you are in the new style csproj(), it doesn't matter where you set it.
Hope this helps.
Upvotes: 2