如何通过 dotnetpublish 将预处理器指令传递给 MSBuild

发布于 2025-01-20 10:18:38 字数 1330 浏览 3 评论 0原文

我有一个带有水疗中心的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

我的问题是:

  1. 将参数从构建命令行传递到msbuild引擎的最佳方法是排除/包括> 在CSPROJ文件中发布目标?
  2. 我可以重复使用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:

  1. 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?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2025-01-27 10:18:38

让它像以下那样工作。 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:

#if DOCKER_BUILD
   // some docker specific code
#endif

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:

#if DOCKER_BUILD
   // some docker specific code
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文