6

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

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

我知道理想的情况是实施 CI 解决方案,但目前我的双手与发布功能捆绑在一起,需要保持相对简单。

非常感谢

4

2 回答 2

4

我们同样需要将我们的解决方案发布到多个文件共享位置,虽然几个月前有人问过这个问题,但我认为答案可能会使社区受益。

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

请注意,我从我们的构建过程中提取了一些更复杂的代码片段,因此我不保证无需稍作改动就可以全部工作。

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

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

然后我添加了一个自定义目标CustomWebFileSystemPublishWebFileSystemPublish之后运行。此目标调用另一个 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>
于 2013-10-06T16:27:16.213 回答
1

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

在 MSDN 上尝试FileSystemWatcher

于 2013-01-11T11:35:37.420 回答