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?
If you go to the Properties page for your project, you should select the Build Events tab. You can type in the call to your batch file in the Post-build event command line text box. If you want to refer to the batch file using the paths included in the project or solution, you can click on the Edit Post-Build...
The path where your bat file is placed should be appended to the PATH variable. In your example append "C:\;" in the value for Path environment variable. Then you can execute MyBatch. bat from anywhere on the command line.
The key to executing a batch file directly in Visual Studio is to Add one using the External Tools selection of the Tools menu. To do this, you: Create a simple batch program that executes other batch programs.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With