【问题标题】:Execute .bat file at end of VS2017 Asp.Net Core publish action?在 VS2017 Asp.Net Core 发布操作结束时执行 .bat 文件?
【发布时间】:2017-10-17 05:26:19
【问题描述】:

在 Visual Studio 2017 中,当使用文件系统发布方法发布 Asp.Net Core 网站时,我想在发布操作将文件复制到输出目录后触发 .bat 文件的执行。

我了解到发布操作的设置由 Visual Studio 存储在项目的 Properties/PublishProviles 目录中的 .pubxml 文件中。所以在可能的情况下文件是FolderProfile.pubxml,它目前看起来像这样:

    <?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>

根据.pubxml 文件中的 cmets 和其他研究,我理解该文件本质上是一个 msbuild 文件,最终 msbuild 用于执行发布操作。 msbuild 文件看起来非常灵活,但有点复杂。我真的很纠结这个。

如果我的项目根目录中有一个名为finishPub.bat 的批处理文件,我如何修改上述.pubxml 文件以在将网站复制到输出文件夹后执行finishPub.bat 文件?

【问题讨论】:

  • 您想仅通过 VS /“web deploy”发布执行该文件,还是在使用 dotnet publish -o C:\foo 时执行该文件?第一个也使用相同的机制将dotnet publish 复制到中间位置,然后复制到publishUrl 中指定的位置。可能取决于bat 文件是否特定于所使用的发布配置文件。
  • 我只需要通过VS发布执行文件。

标签: msbuild asp.net-core-mvc visual-studio-2017 pubxml


【解决方案1】:

您可以使用自定义目标修改您的发布配置文件:

<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>

【讨论】:

  • 如何做这个跨平台的?例如。在 Windows 上执行 .bat 并在 Linux 上执行 .sh? (我确实发布了便携式应用程序,没有-r 开关)。
  • 如果你只使用“示例”,我认为它会寻找我们特定的东西(*nix 上的无扩展和 Windows 上的 .bat//cmd/exe)。但是您可以使用我们Condition="'$(OS)' =='Windows_NT'" 来限制 win/nix 上的任务(因此在这种情况下,==!= 有两个不同的&lt;Exec&gt;
  • 我试过了,但它不起作用。在这种情况下,bat 文件所需的位置是什么?
猜你喜欢
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
相关资源
最近更新 更多