如何在传递 --no-build 时跳过 .csproj 中的 Target?

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

当我 运行 dotnet publish --no-build ... 时,如何从 运行ning 中的 .csproj 文件中的 <Target> 中停止 <Exec> 标签中的命令?我可以停止使用整个 <Target> 部分吗?

样本:

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

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");

这个dotnet docs也叫这个:

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.

您应该能够在 Condition 检查中测试 属性:

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