将dirPath Provider与WebDeploy一起使用

时间:2012-10-11 22:54:39

标签: msbuild msdeploy webdeploy

我在iis中托管了一个wcf应用程序,我正在尝试使用webdeploy打包。使用visual studio工具一切都很好,但我还需要创建一个日志文件夹并设置权限。为此,我在我的web项目中创建了一个ProjectName.wpp.target文件。 该文件看起来像这样

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Target Name="CreateLogsDirectory" AfterTargets="AddIisSettingAndFileContentsToSourceManifest">   
    <!-- This must be declared inside of a target because the property 
    $(_MSDeployDirPath_FullPath) will not be defined at that time. -->
    <ItemGroup>
      <MsDeploySourceManifest Include="dirPath">
        <Path>$(_MSDeployDirPath_FullPath)\logs</Path>
        <enableRule>DoNotDeleteRule</enableRule>
      </MsDeploySourceManifest>
    </ItemGroup>
  </Target>

  <Target Name="DeclareCustomParameters" AfterTargets="AddIisAndContentDeclareParametersItems">
    <!-- This must be declared inside of a target because the property 
    $(_EscapeRegEx_MSDeployDirPath) will not be defined at that time. -->
    <ItemGroup>
      <MsDeployDeclareParameters Include="LogsDirectoryPath">
        <Kind>ProviderPath</Kind>
        <Scope>dirPath</Scope>
        <Match>^$(_EscapeRegEx_MSDeployDirPath)\\logs$</Match>
        <Value>$(_DestinationContentPath)/log</Value>
        <ExcludeFromSetParameter>True</ExcludeFromSetParameter>
      </MsDeployDeclareParameters>
    </ItemGroup>
  </Target>
</Project>

我可以看到dirPath提供程序已添加到sourcemanifest文件中,但是当我部署该程序包时,它会尝试创建源文件路径。本质上,LogsDirectoryPAth项不替换路径。谁能指出我需要做什么?谢谢!

1 个答案:

答案 0 :(得分:5)

考虑到您的其他目录位于您的Web应用程序中,并不一定要包含另一个dirPath提供程序,这样做只会导致更多麻烦(其他参数声明等)。

以下是我用来帮助解决这类问题的一些帮助。您的应用程序特定值可以在wpp.targets文件中声明:

<!-- Items specific to your application (these should be in your wpp.targets) -->
<ItemGroup>
  <SkipDeleteFiles Include="logs" />

  <EmptyDirectoriesToDeploy Include="logs" />

  <AdditionalAcls Include="logs">
    <AclAccess>Write</AclAccess>
  </AdditionalAcls>
</ItemGroup>

以下基于约定的定义既可以放在wpp.targets中,也可以放在可导入wpp.targets的公共目标文件中:

<!-- 
  Empty directories
-->

<PropertyGroup>
  <BeforeAddContentPathToSourceManifest>
    $(BeforeAddContentPathToSourceManifest);
    CreateEmptyDirectories;
  </BeforeAddContentPathToSourceManifest>
</PropertyGroup>

<Target Name="CreateEmptyDirectories">
  <MakeDir Directories="$(_MSDeployDirPath_FullPath)\%(EmptyDirectoriesToDeploy.Identity)" 
           Condition="'@(EmptyDirectoriesToDeploy)' != ''" />
</Target>

<!-- 
  Additional ACLs 
-->

<ItemDefinitionGroup>
  <AdditionalAcls>
    <AclAccess>Write</AclAccess>
    <ResourceType>Directory</ResourceType>
  </AdditionalAcls>
</ItemDefinitionGroup>

<PropertyGroup>
  <AfterAddIisSettingAndFileContentsToSourceManifest>
    $(AfterAddIisSettingAndFileContentsToSourceManifest);
    AddAdditionalAclsToSourceManifest;
  </AfterAddIisSettingAndFileContentsToSourceManifest>
  <AfterAddIisAndContentDeclareParametersItems>
    $(AfterAddIisAndContentDeclareParametersItems);
    AddAdditionalAclsDeclareParameterItems
  </AfterAddIisAndContentDeclareParametersItems>
</PropertyGroup>

<Target Name="AddAdditionalAclsToSourceManifest">
  <ItemGroup Condition="'@(AdditionalAcls)' != ''">
    <MsDeploySourceManifest Include="setAcl">
      <Path>$(_MSDeployDirPath_FullPath)\%(AdditionalAcls.Identity)</Path>
      <setAclResourceType Condition="'%(AdditionalAcls.ResourceType)' != ''">%(AdditionalAcls.ResourceType)</setAclResourceType>
      <setAclAccess>%(AdditionalAcls.AclAccess)</setAclAccess>
      <AdditionalProviderSettings>setAclResourceType;setAclAccess</AdditionalProviderSettings>
    </MsDeploySourceManifest>
  </ItemGroup>
</Target>

<Target Name="AddAdditionalAclsDeclareParameterItems">
  <ItemGroup Condition="'@(AdditionalAcls)' != ''">
    <MsDeployDeclareParameters Include="Add %(AdditionalAcls.AclAccess) permission to %(AdditionalAcls.Identity) Folder">
      <Kind>ProviderPath</Kind>
      <Scope>setAcl</Scope>
      <Match>^$(_EscapeRegEx_MSDeployDirPath)\\@(AdditionalAcls)$</Match>
      <Description>Add %(AdditionalAcls.AclAccess) permission to %(AdditionalAcls.Identity) Folder</Description>
      <DefaultValue>{$(_MsDeployParameterNameForContentPath)}/@(AdditionalAcls)</DefaultValue>
      <DestinationContentPath>$(_DestinationContentPath)/@(AdditionalAcls)</DestinationContentPath>
      <Tags>Hidden</Tags>
      <ExcludeFromSetParameter>True</ExcludeFromSetParameter>
      <Priority>$(VsSetAclPriority)</Priority>
    </MsDeployDeclareParameters>
  </ItemGroup>
</Target>

<!-- 
  Skip delete files and directories
-->

<PropertyGroup>
  <ImportPublishingParameterValuesDependsOn>
    $(ImportPublishingParameterValuesDependsOn);
    AddSkipDirectives;
  </ImportPublishingParameterValuesDependsOn>
</PropertyGroup>

<ItemGroup>
  <SkipDeleteItems Include="@(SkipDeleteFiles)" 
                   Condition="'@(SkipDeleteFiles)' != ''">
    <Provider>filePath</Provider>
  </SkipDeleteItems>

  <SkipDeleteItems Include="@(SkipDeleteDirectories)" 
                   Condition="'@(SkipDeleteDirectories)' != ''">
    <Provider>dirPath</Provider>
  </SkipDeleteItems>
</ItemGroup>

<!-- Uses MSBuild trickery to add an escaped version of the skip path to as 
     "EscapedPath" metadata -->
<Target Name="AddRegexEscapedPathMetadata" Outputs="%(SkipDeleteItems.EscapedPath)">
  <EscapeTextForRegularExpressions Text="%(SkipDeleteItems.Identity)">
    <Output TaskParameter="Result" 
            PropertyName="_Temp_EscapeRegEx_SkipDeleteItemPath" />
  </EscapeTextForRegularExpressions>

  <ItemGroup>
    <SkipDeleteItems Condition="'%(SkipDeleteItems.Identity)' == '%(Identity)'" >
      <EscapedPath>$(_Temp_EscapeRegEx_SkipDeleteItemPath)</EscapedPath>
    </SkipDeleteItems>
  </ItemGroup>

  <PropertyGroup>
    <!-- Clear value -->
    <_Temp_EscapeRegEx_SkipDeleteItemPath></_Temp_EscapeRegEx_SkipDeleteItemPath>
  </PropertyGroup>

</Target>

<Target Name="AddSkipDirectives" DependsOnTargets="AddRegexEscapedPathMetadata">

  <ItemGroup>
    <MsDeploySkipRules Include="%(SkipDeleteItems.Identity)">
      <SkipAction>Delete</SkipAction>
      <ObjectName>%(SkipDeleteItems.Provider)</ObjectName>
      <AbsolutePath>%(SkipDeleteItems.EscapedPath)</AbsolutePath>
    </MsDeploySkipRules>
  </ItemGroup>

</Target>

NB 如果您需要额外努力将打包流程与部署流程分开,那么从技术上讲,SkipDeleteFiles应该在pubxml而不是wpp.targets。 1}}。