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

ε祈祈猫儿з 提交于 2019-11-27 19:06:57

It depends on how you define before but below is one technique.

When you create a publish profile with VS2012 it will create you a .pubxml file in the Properties\PublishProfiles folder (My Project\PublishProfiles for VB). These are MSBuild files and you can edit them to customize the publish process. In your case you can inject a target into the publish process, before the publish actually occurs. You can do that by extending the PipelineDependsOn property as below.

<PropertyGroup>
  <PipelineDependsOn>
    CustomBeforePublish;
    $(PipelineDependsOn);
  </PipelineDependsOn>
</PropertyGroup>

<Target Name="CustomBeforePublish">
  <Message Text="********************************** CustomBeforePublish ***********************************" Importance="high"/>
</Target>

FYI regarding the customization of .wpp.targets, that was the only technique which we had for VS2010. My recommendation here is as follows; customize the .pubxml file for most cases and to only create a .wpp.targets file if you want to customize every publish of the given project.

Declare the following ProjectName.wpp.targets file in the root of your web application:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <BeforeAddContentPathToSourceManifest>
      $(BeforeAddContentPathToSourceManifest);
      AddCopyright;
    </BeforeAddContentPathToSourceManifest>
  </PropertyGroup>

  <Target Name="AddCopyright">
    <!-- I recommend passing in $(_MSDeployDirPath_FullPath) to your script
         as the base path to search to avoid having to perform a VCS rollback 
         (files are copied there before the deployment)
     -->
    <Exec Command="powershell.exe -file &quot;$(SolutionDir)Copyright.ps1&quot; &quot;$(_MSDeployDirPath_FullPath)&quot;" />
  </Target>
</Project>

Sayed's answer nails the problem. However, I thought about providing a fully working answer (testing in Visual Studio 2017):

 <PropertyGroup>
    <PipelineDependsOn>
       PreBuildScript;
       $(PipelineDependsOn);
    </PipelineDependsOn>
  </PropertyGroup>

  <Target Name="PreBuildScript">
     <Message Text="Executing prebuild script" Importance="high"/>
     <Exec Command="powershell.exe -file &quot;$(ProjectDir)\InnerFolder\script.ps1&quot;" />
   </Target>

Note: This will execute for both Preview and actual Publish action, so one can find out pre-publish errors before the actual Publish.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!