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

Mer*_*ham 18 msbuild manifest msdeploy slowcheetah

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

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

我知道我可以编写自定义清单(对dirPath,filePathrunCommand供应商),并直接调用MsDeploy部署它.但是,如果可能的话,我想利用现有的MsBuild任务打包我的服务.

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

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

特别是通过使用该MsDeploySourceManifest项目.

通过适当的.targets文件戳后,它看起来像要么contentPath或者iisApp如果我使用将得到追加到我的清单Package的目标.理想情况下,我只想复制程序集(或目录),可能设置ACL,并在服务上执行installutil.exe.

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

如果没有,是否有一种简单的方法可以构建一个新的目标来实现相同的目标Package,但是允许我吐出一个完全自定义的清单?

Mer*_*ham 17

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

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

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

我的指南经历了以下整体步骤:

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

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

msbuild MyProject.csproj /t:Package /p:Configuration=Debug
Run Code Online (Sandbox Code Playgroud)

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

MyService.Deploy.cmd /Y /M:mywebserver -allowUntrusted
Run Code Online (Sandbox Code Playgroud)

最无证件的部分(我的指南除外)是创建自定义清单.这是我当前文件的转储(注意,它仍然是一个有点马车,但可固定-看到这个问题:MsDeploy远程执行清单的两倍 -并尽量保持使用直接批处理文件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>
Run Code Online (Sandbox Code Playgroud)