0

I want to run a gulp task before publishing to a local folder. So tried using "BeforePublish" target, but it not working properly. Am i doing anything wrong in it ?

<Target Name="BeforePublish">
    <Message Text="Before publish triggered...."></Message>
  </Target>
Jaffer Sathick
  • 466
  • 1
  • 10
  • 21

2 Answers2

2

After several hours of fighting, I think I came up with really simple and universal solution. None of solutions I found were working for me ('Publish' target is for ClickOnce only), so I came up with this:

Publish file (pubxml) is just project-like file. So add some value, for example:

<IsPublish>True</IsPublish>

and then in project file:

<Target Name="MessageBeforePublish" AfterTargets="Build" Condition="'$(IsPublish)' == 'True'">
    <Message Text="Before publishing..." Importance="high" />
  </Target>

So, to be clear, my pubxml file looks like that:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>x64</Platform>
    <PublishDir>***my dir***</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>False</PublishSingleFile>
    <PublishReadyToRun>True</PublishReadyToRun>
    <PublishTrimmed>False</PublishTrimmed>
    <IsPublish>True</IsPublish>
  </PropertyGroup>
</Project>

Notice the last property (IsPublish) added by me. That's it.

Adam Jachocki
  • 1,897
  • 1
  • 12
  • 28
1

I use the VS2017 in my side. I could get the text in the command prompt with the MSbuild.exe.

So please make sure that you edit the .xxproj file correctly, and run the msbuild command line correctly in your side. I just test it using a simple Winform app.

enter image description here

Update:

If you just want to call it during you publish using the VS IDE, I think you could get the workaround here:

How to execute a PowerShell script only before a web deploy Publish task in VS 2012?

Jack Zhai
  • 6,230
  • 1
  • 12
  • 20
  • Actually I want it to be triggered when I right click and publish my web project – Jaffer Sathick Jul 07 '17 at 08:59
  • 1
    @Jaffer Sathick, I just provide the way using MSbuild command line, Maybe you could get a path with this case: https://stackoverflow.com/questions/12907236/how-to-execute-a-powershell-script-only-before-a-web-deploy-publish-task-in-vs-2 – Jack Zhai Jul 07 '17 at 09:53
  • 1
    @Jaffer Sathick, Please mark the solution as the answer since it was resolved, I edit my answer:) – Jack Zhai Jul 12 '17 at 09:37
  • Not works inside VS2019 IDE (not the command line). – huang Jun 12 '21 at 21:00