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?
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?
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
.
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>
Tested in VS2019 16.10.1.
<Target Name="XXX" Condition="'$(PublishProtocol)'!=''">
dotnet build
or dotnet publish
both have the same result when adding Condition="'$(PublishProtocol)'!=''"
to anything in the .csproj file. –
Shire 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
.
<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
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".
<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.
To check if a specific publish is happening, you can put the profile name in your .pubxml
file, and then check that.
<?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>
<PropertyGroup>
<!-- Detect when publishing Production.pubxml -->
<CustomIsPublishing>false</CustomIsPublishing>
<CustomIsPublishing Condition="'$(PublishProfile)' == 'Production'">true</CustomIsPublishing>
</PropertyGroup>
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.
<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.
$(BuildType)
seems to be user-defined and thus not directly usable. –
Trihedron © 2022 - 2024 — McMap. All rights reserved.
PublishDependsOn
property defined for winforms, website, and webapplication projects? – Sakmar