【问题标题】:how to rename for web deploy?如何为 web 部署重命名?
【发布时间】:2014-04-19 09:48:13
【问题描述】:

我有一个网络应用项目:

如您所见,有 3 个 robots.txt 文件 - 每个环境一个。还有 3 个发布配置文件。

现在,对于每个 pubxml,我想选择正确的“robots.xxx.txt”文件并将其重命名为“robots.txt”。理想情况下,我想利用 MSBuild 并将配置保留在每个 pubxml 文件中。

3 个发布配置文件中的每一个都使用<WebPublishMethod>MSDeploy</WebPublishMethod>


编辑:

刚刚尝试Richard Szalay answer,但无济于事。所有 3 个文件仍被复制到输出目录。这就是我的发布配置文件现在的样子

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <SiteUrlToLaunchAfterPublish />
        <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
        <PrecompileBeforePublish>True</PrecompileBeforePublish>
        <EnableUpdateable>True</EnableUpdateable>
        <DebugSymbols>False</DebugSymbols>
        <WDPMergeOption>DonotMerge</WDPMergeOption>
        <ExcludeApp_Data>True</ExcludeApp_Data>
        <publishUrl>C:\Temp\myproject</publishUrl>
        <DeleteExistingFiles>False</DeleteExistingFiles>
    </PropertyGroup>

    <ItemGroup>
        <MsDeployReplaceRules Include="robots">
            <ObjectName>filePath</ObjectName>
            <Match>robots\.debug\.txt</Match>
            <Replace>robots.txt</Replace>
        </MsDeployReplaceRules>
    </ItemGroup>
</Project>

【问题讨论】:

    标签: msbuild msdeploy msbuild-4.0


    【解决方案1】:

    您需要的是替换规则。只需将以下内容添加到您的发布配置文件中:

    <ItemGroup>
      <MsDeployReplaceRules Include="robots">
        <ObjectName>filePath</ObjectName>
        <Match>robots\.debug\.txt</Match>
        <Replace>robots.txt</Replace>
      </MsDeployReplaceRules>
    </ItemGroup>
    

    如果文件命名约定与您的发布配置文件匹配,您也可以在 Web 应用程序的根目录中创建一个 Robots.wpp.targets 文件并使用以下内容:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <MsDeployReplaceRules Include="robots">
          <ObjectName>filePath</ObjectName>
          <Match>robots\.$(PublishProfileName)\.txt</Match>
          <Replace>robots.txt</Replace>
        </MsDeployReplaceRules>
      </ItemGroup>
    </Project>   
    

    【讨论】:

    • 感谢您的回复。我已经尝试了代码,但我认为我做错了什么。请您看一下更新后的问题。
    【解决方案2】:

    我不知道这在技术上是否正确,但它对我有用。

    在您的 pubxml 文件中,在 PropertyGroup 节点下添加此节点:

    <Target Name="MoveRobotsTxt" AfterTargets="GatherAllFilesToPublish">
      <Move Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " SourceFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.release.txt" DestinationFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.txt" />
      <Delete Files="obj\$(ConfigurationName)\Package\PackageTmp\robots.release.txt" />
    </Target>
    

    如果您不仅仅使用Release,则需要复制Move 命令并将Release|AnyCPU 更改为您的构建名称或使用特殊变量$(ConfigurationName)。

    由于您没有现有的 robots.txt,您只想在它是发布版本时替换它,您可能需要:

    <Target Name="MoveRobotsTxt" AfterTargets="GatherAllFilesToPublish">
      <Move SourceFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.$(ConfigurationName).txt" DestinationFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.txt" />
    </Target>
    

    对我来说最重要的是意识到pubxml 就像一个项目文件,您可以使用与 msbuild 相同的所有命令。

    在尝试解决此问题时,我发现 2010 年和 2012 年之间发生了一些变化,这可能解释了为什么 Richard 的答案不起作用。

    编辑:

    由于某种原因,我的原始答案不适用于 MSBuild,MSBuild 和 Visual Studio 之间的管道似乎不同。

    发布配置文件中的目标也没有运行。所以我编辑了我的 项目文件,在注释掉 BeforeBuild/AfterBuild 之后在最后添加:

    <Target Name="move_robots_in_msbuild"  AfterTargets="PipelinePreDeployCopyAllFilesToOneFolder">
      <Move Condition=" '$(Configuration)' == 'Live' " SourceFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.live.txt" DestinationFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.txt" />
      <Delete Files="obj\$(ConfigurationName)\Package\PackageTmp\robots.live.txt" />
    </Target>
    <Target Name="move_robots_in_visual_studio" AfterTargets="GatherAllFilesToPublish">
      <Move Condition=" '$(Configuration)' == 'Live' " SourceFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.live.txt" DestinationFiles="obj\$(ConfigurationName)\Package\PackageTmp\robots.txt" />
      <Delete Files="obj\$(ConfigurationName)\Package\PackageTmp\robots.live.txt" />
    </Target>
    

    【讨论】:

      【解决方案3】:

      今天早上我遇到了同样的问题,但上面提供的两个答案对我也不起作用。

      最终我关注了这篇文章 - http://www.asp.net/mvc/overview/deployment/visual-studio-web-deployment/deploying-extra-files

      然后在我的调试、发布和暂存 .pubxml 文件中的 &lt;/Project&gt; 标记之前添加此标记,并适当地重命名 robots.release.txt:

        <Target Name="CustomCollectFiles">
          <ItemGroup>
            <_CustomFiles Include="robots.release.txt" />
            <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
              <DestinationRelativePath>%(RecursiveDir)robots.txt</DestinationRelativePath>
            </FilesForPackagingFromProject>
          </ItemGroup>
        </Target>
        <PropertyGroup>
          <CopyAllFilesToSingleFolderForPackageDependsOn>
            CustomCollectFiles;
            $(CopyAllFilesToSingleFolderForPackageDependsOn);
          </CopyAllFilesToSingleFolderForPackageDependsOn>
      
          <CopyAllFilesToSingleFolderForMsdeployDependsOn>
            CustomCollectFiles;
            $(CopyAllFilesToSingleFolderForPackageDependsOn);
          </CopyAllFilesToSingleFolderForMsdeployDependsOn>
        </PropertyGroup>
      

      【讨论】:

      • 你应该得到+2。一个用来说明其他解决方案的都是废话。
      【解决方案4】:

      使用 .net 5 核心,我尝试使用提供的代码,并通过组合一些解决方案使其工作。 在我的上下文中,我基于配置,但我使用 Content Remove 进行一些清理。

      <Choose>
          <When Condition="'$(Configuration)' == 'Debug'">
              <ItemGroup>
                  <MsDeployReplaceRules Include="webconf">
                      <ObjectName>filePath</ObjectName>
                      <Match>Web\.Debug\.config</Match>
                      <Replace>Web\.config</Replace>
                 </MsDeployReplaceRules>
                 <Content Remove="Web.Staging.config;Web.Debug.config" />
              </ItemGroup>
          </When>
          <When Condition="'$(Configuration)' == 'Staging'">
              <ItemGroup>
                  <MsDeployReplaceRules Include="webconf">
                      <ObjectName>filePath</ObjectName>
                      <Match>Web\.Staging\.config</Match>
                      <Replace>Web\.config</Replace>
                  </MsDeployReplaceRules>
                  <Content Remove="Web.Debug.config;Web.Staging.config" />
              </ItemGroup>
          </When>
      </Choose>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        • 2011-05-22
        • 2011-02-22
        相关资源
        最近更新 更多