.net.net-corecsprojdotnet-publishdotnet-build

How to skip Target in .csproj when --no-build is passed?


How can I stop commands in <Exec> tags within a <Target> in my .csproj file from running when I run dotnet publish --no-build ...? Can I stop the entire <Target> section from being used?

Sample:

<Target Name="BuildSpa" BeforeTargets="Build" AfterTargets="ComputeFilesToPublish" Condition="'$(Configuration.ToUpper())' == 'RELEASE'">
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
</Target>

Solution

  • The CLI argument --no-build sets the msbuild property NoBuild to true:

            public static readonly Option NoBuildOption = new Option<bool>("--no-build", LocalizableStrings.NoBuildOptionDescription)
                .ForwardAs("-property:NoBuild=true");
    

    The dotnet docs also call this out:

    This can be avoided by passing --no-build property to dotnet.exe, which is the equivalent of setting <NoBuild>true</NoBuild> in your project file, along with setting <IncludeBuildOutput>false</IncludeBuildOutput> in the project file.

    You should be able to test for that property in your Condition check:

    <Target ... Condition="'$(Configuration.ToUpper())' == 'RELEASE' and '$(NoBuild)' != 'true' ">
    ...