August 18, 2020

Conditionally run a target for publish profile

In the last post we have seen how to write custom actions for BeforePublish and AfterPublish events. In this post we will take one more step towards customizing the action execution. Occasionally we need to perform custom actions like copying files to output folder but with some condition, like if a particular directory not already exists in the output or could be any other condition etc.

In this post we are not the exploring capability of actions you can perform with target tag but will see how you can add conditions with Target tag. So I will simply write custom messages to the console with different conditions.

The following Target tag outputs the text message to console based on the condition specified in Condition attribute. Here we are checking if the $(MSBuildProjectDirectory)\MyData folder exists, if so, then it will output a text message to the console otherwise it will do nothing.

<Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish" Condition="Exists('$(MSBuildProjectDirectory)\MyData')">
   <Message Text="Some text to the console" Importance="high" />
</Target>

Here we have used MSBuild reserved property MSBuildProjectDirectory which represents the absolute path of the directory where the project file is located, and then we are checking if a folder named MyData exists in that location.

Obviously you are not going to check for this condition in real project but this is just to demonstrate that you can use the Exists operrator in Condition attribute to check for a folder exists. You can reverse the same condition by using the ! operator to check if folder not exists.

<Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish" Condition="!Exists('$(MSBuildProjectDirectory)\MyData')">
   <Message Text="Some text to the console" Importance="high" />
</Target>

Another operator you can use in Condition attribute is == operator. For exmaple may want to check if the current publish profile's configuration is DEBUG then you want to run the target action. You can write this condition as follows:

<Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish" Condition="'$(Configuration)'=='DEBUG'">
   <Message Text="Some text to the console" Importance="high" />
</Target>

Here we have used MSBuild project property Configuration which contains the configuration value you specified in your publish profile.

Similary you can use != operator to check for inequality.

<Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish" Condition="'$(Configuration)'!='DEBUG'">
   <Message Text="Some text to the console" Importance="high" />
</Target>

This will run the target action if current configuration is not equal to DEBUG.

Some of the commonly used project propeties are:

  • MSBuildProjectName: Represents the filename of the project without extension.
  • MSBuildProjectFile: Represents the full filename of the project with extension.
  • Platform: The operating system you are building for. e.g "Any CPU", "x86", and "x64".
  • MSBuildProjectDirectory: Absolute path of the directory where the project file is located.

You may find more details about project properties on Microsoft Docs:

No comments:

Post a Comment