使用MsBuild生成自定义的MsDeploy清单(包目标)

时间:2011-09-15 10:10:59

标签: msbuild manifest msdeploy slowcheetah

我正在使用Web Deploy为我的产品打包和部署网站。特别是,我的解决方案中有两个不同的项目,我使用此方法进行部署。

我在解决方案(Windows服务)中有第三个项目,也需要在Web服务器上安装。

我知道我可以编写自定义清单(适用于dirPathfilePathrunCommand提供程序)并直接调用MsDeploy来部署它。但是如果可能的话,我想利用现有的MsBuild任务打包我的服务。

我发现可以通过msbuild目标对清单文件进行一些自定义:

http://social.msdn.microsoft.com/Forums/en/msbuild/thread/1044058c-f762-456b-8a68-b0863027ce47

特别是使用MsDeploySourceManifest项目。

在浏览了相应的.targets文件后,如果我使用contentPath目标,则iisAppPackage似乎会附加到我的清单。理想情况下,我只想复制一个程序集(或目录),可能设置ACL,并在服务上执行installutil.exe。

是否可以通过编辑我的csproj文件来完全自定义Package目标生成的清单?

如果没有,是否有一种简单的方法来构建一个与Package等效的新目标,但允许我吐出一个完全自定义的清单?

1 个答案:

答案 0 :(得分:16)

对于那些试图了解其工作原理的人来说,我还没有完整的写作,但我现在已经写了一篇关于如何至少实现这一目标的文章。

<击> http://thehappypath.net/2011/11/21/using-msdeploy-for-windows-services/

编辑:链接现在已经死了。如果您有兴趣,请告诉我,我可以将其发布到其他地方)。

我的指南完成了以下整体步骤:

  • 确保服务在安装时自行启动(并不重要,但更容易处理)
  • 即使您没有Web项目,也要将Microsoft.WebApplication.targets文件添加到项目中。这将启用Package MsBuild目标。
  • 将自定义.targets文件添加到构建自定义MsBuild包清单
  • 的项目中
  • 将一些批处理脚本添加到项目中以停止/卸载并安装服务
  • 添加一个Parameters.xml文件以支持更轻松地更改目标部署目录
  • 使用the SlowCheetah Visual Studio addon
  • 设置app.config转换

然后,您可以使用以下命令行打包项目:

msbuild MyProject.csproj /t:Package /p:Configuration=Debug

您可以使用以下命令行部署生成的包:

MyService.Deploy.cmd /Y /M:mywebserver -allowUntrusted

最无证件的部分(我的指南除外)是创建自定义清单。这是我当前文件的转储(注意,它仍然有点错误,但可以修复 - 请参阅此问题:MsDeploy remoting executing manifest twice - 并尝试继续使用直接批处理文件runCommand)。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- This file must be included before Microsoft.Web.Publishing.targets so we can hook into BeforeAddIisSettingAndFileContentsToSourceManifest -->

  <PropertyGroup>

    <!-- Include our targets -->
    <IncludeStopServiceCommand>True</IncludeStopServiceCommand>
    <IncludeSetCustomAclsProvider>True</IncludeSetCustomAclsProvider>
    <IncludeInstallServiceCommand>True</IncludeInstallServiceCommand>
    <IncludeMoveAppConfigToCorrectPackagePath>True</IncludeMoveAppConfigToCorrectPackagePath>

    <!-- Uncomment to enable more verbose MsBuild logging -->
    <!-- <EnablePackageProcessLoggingAndAssert>True</EnablePackageProcessLoggingAndAssert> -->

    <!-- Enable web.config transform, but hack it to work for app.config -->
    <ProjectConfigFileName>app.config</ProjectConfigFileName>
    <TransformWebConfigEnabled>True</TransformWebConfigEnabled>
    <UseParameterizeToTransformWebConfig>True</UseParameterizeToTransformWebConfig>

    <!-- Enable web project packaging, but hack it to work for non-web app -->
    <DeployAsIisApp>False</DeployAsIisApp>
    <IncludeIisSettingsOnPublish>False</IncludeIisSettingsOnPublish>
    <IncludeSetAclProviderOnDestination>False</IncludeSetAclProviderOnDestination>
    <DisableAllVSGeneratedMSDeployParameter>True</DisableAllVSGeneratedMSDeployParameter>

    <!-- Insert our custom targets into correct places in build process -->
    <BeforeAddIisSettingAndFileContentsToSourceManifest Condition="'$(BeforeAddIisSettingAndFileContentsToSourceManifest)'==''">
      $(BeforeAddIisSettingAndFileContentsToSourceManifest);
      AddStopServiceCommand;
    </BeforeAddIisSettingAndFileContentsToSourceManifest>

    <AfterAddIisSettingAndFileContentsToSourceManifest Condition="'$(AfterAddIisSettingAndFileContentsToSourceManifest)'==''">
      $(AfterAddIisSettingAndFileContentsToSourceManifest);
      AddSetCustomAclsProvider;
      AddInstallServiceCommand;
    </AfterAddIisSettingAndFileContentsToSourceManifest>

    <OnAfterCopyAllFilesToSingleFolderForPackage Condition="'$(OnAfterCopyAllFilesToSingleFolderForPackage)'==''">
      $(OnAfterCopyAllFilesToSingleFolderForPackage);
      MoveAppConfigToCorrectPackagePath;
    </OnAfterCopyAllFilesToSingleFolderForPackage>

  </PropertyGroup>

  <!-- Custom targets -->
  <Target Name="AddStopServiceCommand" Condition="'$(IncludeStopServiceCommand)'=='true'">
    <Message Text="Adding runCommand to stop the running Service" />
    <ItemGroup>

      <MsDeploySourceManifest Include="runCommand">
        <path>$(_MSDeployDirPath_FullPath)\bin\servicestop.bat</path>
        <waitInterval>20000</waitInterval>
        <AdditionalProviderSettings>waitInterval</AdditionalProviderSettings>
      </MsDeploySourceManifest>

    </ItemGroup>
  </Target>

  <Target Name="AddSetCustomAclsProvider" Condition="'$(IncludeSetCustomAclsProvider)'=='true'">
    <ItemGroup>

      <MsDeploySourceManifest Include="setAcl">
        <Path>$(_MSDeployDirPath_FullPath)</Path>
        <setAclUser>LocalService</setAclUser>
        <setAclAccess>FullControl</setAclAccess> <!-- Todo: Reduce these permissions -->
        <setAclResourceType>Directory</setAclResourceType>
        <AdditionalProviderSettings>setAclUser;setAclAccess;setAclResourceType</AdditionalProviderSettings>
      </MsDeploySourceManifest>

    </ItemGroup>
  </Target>

  <Target Name="AddInstallServiceCommand" Condition="'$(IncludeInstallServiceCommand)'=='true'">
    <Message Text="Adding runCommand to install the Service" />
    <ItemGroup>

      <MsDeploySourceManifest Include="runCommand">
        <path>cmd.exe /c $(_MSDeployDirPath_FullPath)\bin\serviceinstall.bat</path>
        <waitInterval>20000</waitInterval>
        <dontUseCommandExe>false</dontUseCommandExe>
        <AdditionalProviderSettings>waitInterval;dontUseCommandExe</AdditionalProviderSettings>
      </MsDeploySourceManifest>

    </ItemGroup>
  </Target>

  <Target Name="MoveAppConfigToCorrectPackagePath"
          Condition="'$(IncludeMoveAppConfigToCorrectPackagePath)'=='true'">
    <PropertyGroup>
      <OriginalAppConfigFilename>$(_PackageTempDir)\App.Config</OriginalAppConfigFilename>
      <TargetAppConfigFilename>$(_PackageTempDir)\bin\$(TargetFileName).config</TargetAppConfigFilename>
    </PropertyGroup>

    <Copy SourceFiles="$(OriginalAppConfigFilename)" DestinationFiles="$(TargetAppConfigFilename)" 
          Condition="Exists($(OriginalAppConfigFilename))" />
    <Delete Files="$(OriginalAppConfigFilename)" 
            Condition="Exists($(OriginalAppConfigFilename))" />
  </Target>

</Project>