有没有任何 MSBuild 属性可以显示我们正在发布?

6

如果是发布版本,我希望你能有条件地取消定义DEBUG

是否有一个属性可以检查当前是否正在发布?

5个回答

4

在VS2019 16.10.1中进行测试。

<Target Name="XXX" Condition="'$(PublishProtocol)'!=''">

4
你可以引入自己的目标来设置属性,然后进行关键操作,或者执行需要的操作。下面的项目修改演示了如何通过自己的前置和后置目标将依赖项连接到现有的发布目标中。前置目标设置一个属性。然后,在你的项目已经定义了$(DefineConstants)属性的DEBUG部分,你根据在发布时设置的属性条件地决定是否将DEBUG添加到常量列表中。
<PropertyGroup>
   <PublishDependsOn>MyBeforePublish;$(PublishDependsOn);MyAfterPublish</PublishDependsOn>
</PropertyGroup>

<Target Name="MyBeforePublish">
   <PropertyGroup>
      <DetectPublishBuild>true</DetectPublishBuild>
   </PropertyGroup>
</Target>
<Target Name="MyAfterPublish">
   <PropertyGroup>
      <DetectPublishBuild>false</DetectPublishBuild>
   </PropertyGroup>
</Target>

...

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
   <PlatformTarget>x86</PlatformTarget>
   <DebugSymbols>true</DebugSymbols>
   <DebugType>full</DebugType>
   <Optimize>false</Optimize>
   <OutputPath>bin\Debug\</OutputPath>
   <DefineConstants>TRACE</DefineConstants>
   <DefineConstants
      Condition="'$(DetectPublishBuild)' != 'true'"
      >DEBUG;$(DefineConstants)</DefineConstants>
   <ErrorReport>prompt</ErrorReport>
   <WarningLevel>4</WarningLevel>
</PropertyGroup>

Winforms、网站和Web应用程序项目中是否定义了 "PublishDependsOn" 属性? - Maslow

2

1
请参阅 this issue-comment">dotnet/sdk 存储库中的内容。微软似乎引入了 MSBuild 变量 _IsPublishing,可以用来检查是否处于发布构建状态。 如果我们确实执行了 dotnet publish,此变量将包含 yes;如果未执行,则为空。
请注意,在使用 dotnet build /t:Publishmsbuild /t:Publish 时,该变量不会被设置。

-3
<Choose>
      <When Condition="'$(BuildType)' == 'publish'">
         <PropertyGroup>
           <DefineConstants>Release</DefineConstants>
         </PropertyGroup>
      </When>      
</Choose>

除了发布之外,您可能还需要其他值。但是,这应该可以工作。

我们在我们的地方所做的是实际上有一个发布、调试和发布。我们通过将其从发布中复制来创建发布,因此它具有所有设置。


$(BuildType) 似乎是用户定义的,因此不能直接使用。 - Jack Miller
未找到BuildType。 - huang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接