VS2012发布到多个位置

时间:2013-01-11 11:31:35

标签: visual-studio-2012 web-deployment

我目前正在使用VS2012中的内置发布功能将ASP.NET MVC站点发布到Web服务器上的文件系统目录共享。无论如何我可以将它发布到多个位置,而不仅仅是单击“发布”按钮时的位置?

我不想创建第二个配置文件并且必须执行相同的过程两次,我已经看过通过添加一个额外的标记来修改pubxml文件,以查看发布例程是否可以选择它。但不幸的是,它似乎只是拿起了列表中的最后一个配置。

我知道理想的是实施CI解决方案,但目前我的手与发布功能相关,需要保持相对直接。

非常感谢

2 个答案:

答案 0 :(得分:4)

我们同样需要将我们的解决方案发布到多个文件共享位置,而几个月前我问这个问题时,我认为答案可能对社区有益。

由于VS发布配置文件是可以轻松扩展的普通MSBuild文件,因此这是我附带的解决方案。

请注意,我从构建过程中提取了一些代码片段,这些代码片段有点复杂,所以我不保证它会全部工作而不必稍微改变它。

在发布配置文件中,我添加了自定义 DeploymentPaths 项,如下所示。 请注意,您可以定义一个或多个其他位置。

<ItemGroup Label="Defines additional publish locations">
  <DeploymentPaths Include="\\SERVER1\ShareFolder\ProjectA\" />
  <DeploymentPaths Include="\\SERVER2\ShareFolder\ProjectA\" />
</ItemGroup>

然后我添加了一个自定义目标 CustomWebFileSystemPublish ,以便在 WebFileSystemPublish 之后运行。此目标调用另一个MSBuild文件publish.xml,该文件执行现有文件的删除并复制新文件。

<!-- Custom File System Publish to deploy to additional locations based on DeploymentPaths -->
<Target Name="CustomWebFileSystemPublish" AfterTargets="WebFileSystemPublish" Condition=" @(DeploymentPaths)!='' ">
  <CreateItem Include="$(MSBuildProjectDirectory)\$(_PackageTempDir)">
    <Output ItemName="AbsoluteSourcePathItem" TaskParameter="Include" />
  </CreateItem>
  <CreateProperty Value="%(AbsoluteSourcePathItem.Fullpath)">
    <Output PropertyName="AbsoluteSourcePath" TaskParameter="Value" />
  </CreateProperty>

  <Message Text="### CustomWebFileSystemPublish" Importance="high" />
  <Message Text="### DeploymentPaths: @(DeploymentPaths)" Importance="high" />

  <MSBuild Projects="$(MSBuildProjectFile)" Properties="AbsoluteSourcePath=$(AbsoluteSourcePath)" Targets="DoPublish" />
</Target>

<Target Name="DoPublish">
  <Message Text="### DoPublish $(AbsoluteOutputPath) | %(DeploymentPaths.Identity)" Importance="normal" />
  <!-- Adjust path to the publish.xml file depending on where you put it in your solution -->
  <MSBuild Projects="..\Deployment\publish.xml" Properties="OutputPath=$(AbsoluteSourcePath);DeployPath=%(DeploymentPaths.Identity)" />
</Target>

最后,这是publish.xml MSBuild文件

<!-- Publish.xml -->
<Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <Target Name="Start">
  <PropertyGroup>
   <!-- Ensure DeployPath has the expected trailing slash -->
   <DeployPath Condition=" '$(DeployPath)' != '' and !HasTrailingSlash('$(DeployPath)') ">$(DeployPath)\</DeployPath>
  </PropertyGroup>
  <Message Text=" # Deploying from $(OutputPath) to $(DeployPath) " Importance="normal" />
 </Target>

 <Target Name="CleanDeployFolder" DependsOnTargets="Start"
      Condition=" $(DeployPath)!=''">
  <Message Text=" # Cleaning files in $(DeployPath)" Importance="normal" />

  <!-- Defines the files to clean -->
  <ItemGroup>
   <DeployCleanFiles Include="$(DeployPath)\**\*.*" />
  </ItemGroup>

  <!--Delete files in Deploy folder (folders not deleted by Delete Task)-->
  <Delete Files="@(DeployCleanFiles)" />

  <Message Text=" # Cleaning files in $(DeployPath) Completed" Importance="normal" />
 </Target>

 <Target Name="CopyToDeployFolder" DependsOnTargets="CleanDeployFolder"
      Condition=" $(DeployPath)!=''">
  <Message Text=" # Copying files to $(DeployPath)" Importance="normal" />

  <ItemGroup>
   <OutputFiles Include="$(OutputPath)\**\*.*" />
  </ItemGroup>

  <Copy SourceFiles="@(OutputFiles)" DestinationFolder="$(DeployPath)%(OutputFiles.RecursiveDir)" />

  <Message Text=" # Copying files to $(DeployPath) Completed" Importance="normal" />
 </Target>

 <Target Name="Default" DependsOnTargets="CopyToDeployFolder" 
      Condition=" $(OutputPath)!='' And $(DeployPath)!='' ">
  <Message Text=" # Deploying from $(OutputPath) to $(DeployPath) Completed" Importance="normal" />
 </Target>
</Project>

答案 1 :(得分:1)

您可以创建一个小型Windows服务来监控目录,并在添加新文件时复制到多个位置

尝试FileSystemWatcher on MSDN