Execute .bat file at end of VS2017 Asp.Net Core publish action?
Asked Answered
S

1

7

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?

Schoolmate answered 17/5, 2017 at 16:18 Comment(2)
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.Raddi
I do just need to execute the file through VS publish.Schoolmate
R
11

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>
Raddi answered 17/5, 2017 at 19:52 Comment(3)
How to make this cross-platform? E.g. execute .bat on Windows and .sh on Linux? (I do publish portable app, without -r switch).Combes
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 <Exec> with == and != on this condition)Raddi
I tried it but it does not work. What is the required location of the bat file in that case?Heterosporous

© 2022 - 2024 — McMap. All rights reserved.