6

In Visual Studio 2017 when publishing an Asp.Net Core Website using the File System publish method I want to trigger the execution of a .bat file after the publish operation copied the files to the output directory.

I have learned that the settings for the publish operation are stored by Visual Studio in the project's Properties/PublishProviles directory in a .pubxml file. So in may case the file is FolderProfile.pubxml and it currently looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <!--
    This file is used by the publish/package process of your Web project. You can customize the behavior of this process
    by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
    -->
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <PublishProvider>FileSystem</PublishProvider>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <SiteUrlToLaunchAfterPublish />
        <LaunchSiteAfterPublish>False</LaunchSiteAfterPublish>
        <ExcludeApp_Data>False</ExcludeApp_Data>
        <PublishFramework>net461</PublishFramework>
        <ProjectGuid>f58b5ca0-8221-4c97-aa6d-7fba93a3abeb</ProjectGuid>
        <publishUrl>C:\inetpub\wwwGiftOasisResponsivePublished</publishUrl>
        <DeleteExistingFiles>True</DeleteExistingFiles>
      </PropertyGroup>
    </Project>

Based on the comments in the .pubxml file and additional research, it's my understanding this file is essentially an msbuild file and ultimately msbuild is used to perform the publish operation. msbuild files seem very flexible but more than a little complicated. I'm really struggling with this one.

If I had a batch file in the root of my project called finishPub.bat, how could I modify the above .pubxml file to cause the execute of the finishPub.bat file after the website has been copied to the output folder?

RonC
  • 31,330
  • 19
  • 94
  • 139
  • Do you want to execute that file only through VS / "web deploy" publish or also when using `dotnet publish -o C:\foo`? first one also uses the same mechanism for `dotnet publish` to an intermediate location and then copying to the location specified in `publishUrl`.. Probably depends on whether the bat file is specific to the publish profile used. – Martin Ullrich May 17 '17 at 19:21
  • I do just need to execute the file through VS publish. – RonC May 17 '17 at 19:26

1 Answers1

10

You can amend your publish profile with a custom target:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    ...
  </PropertyGroup>
  <Target Name="ExecuteBatAfterPublish" AfterTargets="AfterPublish">
    <Exec Command="example.bat" WorkingDirectory="$(publishUrl)" />
  </Target>
</Project>
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • How to make this cross-platform? E.g. execute .bat on Windows and .sh on Linux? (I do publish portable app, without `-r` switch). – xmedeko Sep 07 '18 at 11:31
  • 1
    if you only use "example" I think it looks for us-speicifc things (exgtensionless on *nix and .bat//cmd/exe on windows). But you can us `Condition="'$(OS)' =='Windows_NT'"` to limit tasks on win/nix (so two different `` with `==` and `!=` on this condition) – Martin Ullrich Sep 07 '18 at 12:05
  • I tried it but it does not work. What is the required location of the bat file in that case? – Tillito Feb 25 '22 at 09:50