如何通过 dotnetpublish 将预处理器指令传递给 MSBuild
我有一个带有水疗中心的ASP.NET Core 6.0 WebAPI解决方案。默认模板默认情况下通过在下面运行发布
目标。
WebAPI.CSPROJ文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<!-- ... -->
<!-- .NET Core Web API is being build here-->
<!-- ... -->
<Target Name="PublishAngular" AfterTargets="ComputeFilesToPublish">
<!-- ... -->
<!-- SPA is getting built here-->
<!-- ... -->
</Target>
</Project>
我将WebAPI迁移到Docker,并希望最终将SPA从中拆分。但这是一个漫长的过程,因此,暂时我想有一种方法来指定我是否要使用构建命令行有条件地构建水疗中心。
我正在使用一个名为docker_build的前处理器指令,所以我在C#文件中执行这样的操作:
#if !DOCKER_BUILD
services.ConfigureSpa();
#endif
我的问题是:
- 将参数从构建命令行传递到msbuild引擎的最佳方法是排除/包括
>
在CSPROJ文件中发布
目标? - 我可以重复使用Docker_build预处理程序来实现这一目标吗?
我的构建命令行看起来像这样:
dotnet Publish -c Release -o Out
我尝试在Docker_build中传递AS:
dotnet Publish -C Release -o preasion -o Out/p:docker_build
and code> and code>和像这样修改SPA构建目标:
&lt; target name =“ PublishAngular” AfterTargets =“ ComputeFileStopublish”条件=“!Docker_build”&gt;
,
但没有太多成功。我在做什么错?
I have an ASP.NET Core 6.0 WebApi solution with SPA. The default template builds the SPA by default by running PublishAngular
target below.
WebApi.csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<!-- ... -->
<!-- .NET Core Web API is being build here-->
<!-- ... -->
<Target Name="PublishAngular" AfterTargets="ComputeFilesToPublish">
<!-- ... -->
<!-- SPA is getting built here-->
<!-- ... -->
</Target>
</Project>
I am migrating the WebApi to Docker, and want to eventually completely split off SPA from it. But it's a lengthy process, so for the time being I want to have a way to specify whether I want to build the SPA conditionally using build command line.
I am using a pre-processor directive called DOCKER_BUILD, so I do something like this in my C# files:
#if !DOCKER_BUILD
services.ConfigureSpa();
#endif
My questions are:
- What's the best way of passing a parameter from the build command line to MSBuild engine to exclude/include the
PublishAngular
target in csproj file? - Can I re-use the DOCKER_BUILD preprocessor diective to accomplish that?
My build command line looks like this:
dotnet publish -c Release -o out
I tried passing in DOCKER_BUILD as:
dotnet publish -c Release -o out /p:DOCKER_BUILD
and modifying the SPA build target like this:
<Target Name="PublishAngular" AfterTargets="ComputeFilesToPublish" Condition="!DOCKER_BUILD">
but didn't have much success. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让它像以下那样工作。 Build command line:
dotnet msbuild -target:Publish /p:DefineConstants=DOCKER_BUILD
csproj target condition:
The
DefineConstants
parameter also works with code inside C# files so this works as well:Got it working as following. Build command line:
dotnet msbuild -target:Publish /p:DefineConstants=DOCKER_BUILD
csproj target condition:
<Target Name="PublishAngular" AfterTargets="ComputeFilesToPublish" Condition="!$(DefineConstants.Contains('DOCKER'))">
The
DefineConstants
parameter also works with code inside C# files so this works as well: