Is there any MSBuild property that shows we are in publish?
Asked Answered
S

6

7

I want to conditionally undefine DEBUG if it's a publish build.

is there a property I can check to see if we're currently publishing?

Sakmar answered 6/6, 2011 at 16:30 Comment(0)
C
3

See this issue-comment in the dotnet/sdk repository. Microsoft seemingly introduced the MSBuild-variable _IsPublishing which can be used to check if we're in a publish-build. This variable will contain yes if we are indeed doing dotnet publish and is empty if we are not.

Note however, that this variable will not be set, when using dotnet build /t:Publish or msbuild /t:Publish.

Corkscrew answered 15/8, 2023 at 11:2 Comment(0)
O
4

You can wire in your own target to set a property that you can then key behavior off of, or do whatever you want. The project modification below shows how to wire into the existing Publish target dependencies with your own before and after target. The before target sets a property. Then, in the existing part of your project where DEBUG is defined within the $(DefineConstants) property, you conditionally decide on whether or not to add DEBUG into the constant list, based on the property you set when the build is being performed because of a Publish.

<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>
Occurrence answered 6/6, 2011 at 17:30 Comment(1)
Is the PublishDependsOn property defined for winforms, website, and webapplication projects?Sakmar
R
4

Tested in VS2019 16.10.1.

<Target Name="XXX" Condition="'$(PublishProtocol)'!=''">
Redoubtable answered 13/6, 2021 at 17:7 Comment(1)
Does not seem to work on the CLI. Running dotnet build or dotnet publish both have the same result when adding Condition="'$(PublishProtocol)'!=''" to anything in the .csproj file.Shire
C
3

See this issue-comment in the dotnet/sdk repository. Microsoft seemingly introduced the MSBuild-variable _IsPublishing which can be used to check if we're in a publish-build. This variable will contain yes if we are indeed doing dotnet publish and is empty if we are not.

Note however, that this variable will not be set, when using dotnet build /t:Publish or msbuild /t:Publish.

Corkscrew answered 15/8, 2023 at 11:2 Comment(0)
E
2
<Copy SourceFiles="Web.Base.config" DestinationFiles="Web.config" OverwriteReadOnlyFiles="True" Condition="!('$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == '')" />

This will perform the "Copy" only when the build is using the PublishProfile flag.

http://sedodream.com/2013/01/06/commandlinewebprojectpublishing.aspx

Eckert answered 9/12, 2016 at 12:10 Comment(0)
S
0

To ensure a publish is happening, you could check that $(PublishProfile) is not equal to "Default". In my testing this is the default value.

Important: Setting <PublishProfile> to an empty string causes issues, so I just check if it is not "Default".

Set the custom publishing flag

<PropertyGroup>
  <!-- Detect when publishing -->
  <CustomIsPublishing>false</CustomIsPublishing>
  <CustomIsPublishing Condition="'$(PublishProfile)' != 'Default'">true</CustomIsPublishing>
</PropertyGroup>

This way no matter which profile is ran you know a publish is happening.

Checking if a specific publish is happening

To check if a specific publish is happening, you can put the profile name in your .pubxml file, and then check that.

Setting $(PublishProfile) via .pubxml

<?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
<Project>
  <PropertyGroup>
    <PublishProfile>Production</PublishProfile>
    <!-- Other publish properties below... -->
  </PropertyGroup>
</Project>

Checking if publishing with Production.pubxml

<PropertyGroup>
  <!-- Detect when publishing Production.pubxml -->
  <CustomIsPublishing>false</CustomIsPublishing>
  <CustomIsPublishing Condition="'$(PublishProfile)' == 'Production'">true</CustomIsPublishing>
</PropertyGroup>

Test

You can test the result by printing a message with $(CustomIsPublishing). Seeing as though this is a boolean it can be used for Conditions.

I have tested by publishing a project via VS2022 and also via cli.

Snelling answered 23/9 at 4:11 Comment(0)
A
-3
<Choose>
      <When Condition="'$(BuildType)' == 'publish'">
         <PropertyGroup>
           <DefineConstants>Release</DefineConstants>
         </PropertyGroup>
      </When>      
</Choose>

You may need other values in there besides release. But, this should work.

What we do at our place though is to actually have a publish, debug, and release. We created publish by having it copy from release so it has all the settings in it.

Abed answered 6/6, 2011 at 17:35 Comment(2)
$(BuildType) seems to be user-defined and thus not directly usable.Trihedron
BuildType not found.Redoubtable

© 2022 - 2024 — McMap. All rights reserved.