簡體   English   中英

使用MsBuild生成自定義的MsDeploy清單(包目標)

[英]Using MsBuild to generate customized MsDeploy manifest (Package target)

我正在使用Web Deploy為我的產品打包和部署網站。 特別是,我在我的解決方案中有兩個不同的項目,我使用此方法進行部署。

我在解決方案(Windows服務)中有第三個項目,也需要在Web服務器上安裝。

我知道我可以編寫自定義清單(用於dirPathfilePathrunCommand提供程序)並直接調用MsDeploy來部署它。 但是,如果可能的話,我想利用現有的MsBuild任務打包我的服務。

我看到可以通過msbuild目標對清單文件進行一些自定義:

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

特別是通過使用MsDeploySourceManifest項。

在瀏覽了相應的.targets文件之后,如果我使用Package目標,看起來要么將contentPathiisApp附加到我的清單。 理想情況下,我只想復制程序集(或目錄),可能設置ACL,並在服務上執行installutil.exe。

是否可以通過編輯我的csproj文件來完全自定義Package目標生成的清單?

如果沒有,是否有一種簡單的方法來構建一個與Package相同的新目標,但允許我吐出一個完全自定義的清單?

對於那些試圖了解其工作原理的人來說,我還沒有完整的寫作,但我現在已經寫了一篇關於如何至少實現這一目標的文章。

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

編輯:鏈接現在已經死了。如果您有興趣,請告訴我,我可以將其發布到其他地方 )。

我的指南經歷了以下整體步驟:

  • 確保服務在安裝時自行啟動(並不重要,但更容易處理)
  • 即使您沒有Web項目,也要將Microsoft.WebApplication.targets文件添加到項目中。 這將啟用Package MsBuild目標。
  • 將自定義.targets文件添加到構建自定義MsBuild包清單的項目中
  • 將一些批處理腳本添加到項目中以停止/卸載並安裝該服務
  • 添加一個Parameters.xml文件以支持更輕松地更改目標部署目錄
  • 使用SlowCheetah Visual Studio插件設置app.config轉換

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

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

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

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

最無證件的部分(我的指南除外)是創建自定義清單。 這是我當前文件的轉儲(注意,它仍然有點錯誤,但可以修復 - 請參閱此問題: 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM