【问题标题】:SlowCheetah not transforming web.config on buildSlowCheetah 没有在构建时转换 web.config
【发布时间】:2013-09-03 17:28:42
【问题描述】:

我通过 nuget 安装了 SlowCheetah 包,并基于构建配置为我的 web.config 添加了转换文件。 但是,在构建时,web.config 不会被转换。我检查了我的项目文件,确实看到了 SlowCheetah PropertyGroup 和 Import 元素的条目。我在项目文件中没有看到转换目标。 如果我添加一个 app.config,app.config 文件会被转换。 据我了解,安装 SlowCheetah 包应该会自动将 web.config 转换目标添加到项目的 MSBuild 文件中。我可以手动添加它,但我认为 SlowCheetah 是开箱即用的。 我是不是错过了什么。请务必让我知道。 我的要求是我的 web.config 文件应该根据构建配置进行转换,并且转换后的 web.config 文件应该位于输出目录中。 感谢并感谢所有帮助。

【问题讨论】:

    标签: msbuild slowcheetah


    【解决方案1】:

    您是否将转换文件的“复制到输出目录”属性设置为“不复制”? 请同时检查您的项目文件。

    在您的项目文件中应添加以下条目(取决于您安装的版本,在本例中为 2.5.7):

    <PropertyGroup Label="SlowCheetah">
    <SlowCheetah_EnableImportFromNuGet Condition=" '$(SC_EnableImportFromNuGet)'=='' ">true</SlowCheetah_EnableImportFromNuGet>
    <SlowCheetah_NuGetImportPath Condition=" '$(SlowCheetah_NuGetImportPath)'=='' ">$([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\..\packages\SlowCheetah.2.5.7\tools\SlowCheetah.Transforms.targets ))</SlowCheetah_NuGetImportPath>
    <SlowCheetahTargets Condition=" '$(SlowCheetah_EnableImportFromNuGet)'=='true' and Exists('$(SlowCheetah_NuGetImportPath)') ">$(SlowCheetah_NuGetImportPath)</SlowCheetahTargets>
    

    <Import Project="$(SlowCheetahTargets)" Condition="Exists('$(SlowCheetahTargets)')" Label="SlowCheetah" />
    

    【讨论】:

    • 对于任何观看的人来说,对于当前版本的 SlowCheetah,这是 100% 不正确的。
    【解决方案2】:

    仅当您使用发布功能部署项目时,Visual Studio 才会执行转换。要在构建时执行此操作,您需要调整 MSBuild 脚本。完整的解决方案是here。这里是要点:

    项目中的文件 创建一个名为 Web.Base.config 的文件,除了 现有的 Web.Debug.config 和 Web.Release.config。该文件将 等同于您的旧 Web.config,因为它将是 转型。你最终会得到这些文件:

    Web.config、Web.Base.config、Web.Debug.config、Web.Release.config 和 网络配置。将以下配置添加到您的底部 .csproj 文件,就在结束标记之前:

    <Target Name="BeforeBuild">
        <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
    </Target>
    

    更新:从 cmets 的提醒中,我意识到我也有一个 发布时 Visual Studio 两次转换 XML 的问题 一个专案。解决这个问题的方法是在 像这样的目标标签:

    <Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
    

    【讨论】:

    • 关于解决方案,对于 Visual Studio 2013,您不需要 因为 任务已经导入。
    • 感谢分享。
    • 耶耶耶耶耶!
    • 问题是关于SlowCheetah,它是一个扩展+NuGet 包,应该为你做这件事。
    • 要考虑的一个缺点:作为 Jacob Viau points out,这将破坏修改 Web.config 的 NuGet 包安装。
    【解决方案3】:

    为了详细说明 Philipp 的回答,我找到了一个可能更简单的解决方案: 不需要 Web.Base.Config,覆盖您的 web.config 也没有问题,这会导致源代码管理出现问题。

    BeforeBuild:将目标设为 TargetDir 和 TargetFileName。

    AfterBuild:在构建完成后将其复制到已发布的网站。

    <Target Name="BeforeBuild">
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="$(TargetDir)$(TargetFileName).config" />
    </Target>
    <Target Name="AfterBuild" Condition="'$(Configuration)' == 'Dev' Or '$(Configuration)' == 'Test' Or '$(Configuration)' == 'Prod'">
        <Delete Files="$(TargetDir)_PublishedWebsites\$(ProjectName)\Web.config" />
        <Copy SourceFiles="$(TargetDir)$(TargetFileName).config" DestinationFiles="$(TargetDir)_PublishedWebsites\$(ProjectName)\Web.config" />
    </Target>
    

    【讨论】:

    • 你能详细说明为什么构建不会替换在构建之前复制到那里的配置文件吗?因为我无法让它工作,我认为这是因为文件被覆盖了。
    猜你喜欢
    • 2012-11-10
    • 2017-07-27
    • 2013-11-16
    • 2012-12-24
    • 2014-10-12
    • 1970-01-01
    • 2023-04-02
    • 2015-02-07
    • 2015-12-28
    相关资源
    最近更新 更多