【问题标题】:web.config transformation does not work on build serverweb.config 转换在构建服务器上不起作用
【发布时间】:2016-05-26 03:09:53
【问题描述】:

我们正在与团队城市建立持续集成,在签到时进行构建。这很好用,但是它总是使用默认的 web.config 构建,它不会随着开发环境特定的 web 配置进行转换。 在 Visual Studio 中,我为开发创建了自定义构建配置。当我在本地发布并选择了development 配置时,web.development.config 会正确转换,但这不会在构建服务器上发生。 在团队城市中,作为 msbuild 步骤的一部分,我创建了一个构建参数 sys.configuration,并将 development 作为值传递。但是当它运行时,生成的 web.config 不会随开发设置进行转换。我还尝试将命令行参数设置为/p:configuration=development,但没有成功。

任何帮助将不胜感激。

【问题讨论】:

    标签: msbuild web-config teamcity


    【解决方案1】:

    我们一直在我们的 Web 应用程序项目中使用发布目标,并且已经看到构建过程正确地转换了我们的 web.config 文件,但是当我们将构建移动到我们的 CI 服务器时,没有应用转换。

    使用的 MSBuild 脚本是:

    MSBuild.exe PATH_TO_PROJ.csproj /p:DeployOnBuild=true /p:PublishProfile=PUBLISH_TARGET

    我们发现我们仍然需要将配置指定为 Debug 或 Release(尽管这似乎是发布目标的一个属性。)

    MSBuild.exe PATH_TO_PROJ.csproj /p:DeployOnBuild=true /p:PublishProfile=PUBLISH_TARGET /p:Configuration=Release

    【讨论】:

    • 奇怪的是添加/p:Configuration=Release 工作,即使我的发布配置文件也设置为!谢谢!
    • 附加 /p:Configuration=MyConfig 对我有用!谢谢!
    【解决方案2】:

    我相信,要在使用 MSBuild 而不是 Visual Studio 发布时发生配置转换,您需要将 TransformXml 任务添加到您的项目中。

    This answer 提供了您需要添加的指南,但要保存点击:

    <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
    <Target Name="BeforeBuild">
        <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
    </Target>
    

    您可能需要更改 .targets 文件的路径以与构建服务器上安装的任何版本保持一致。

    【讨论】:

    • 这对我有用!谢谢您的帮助。我所做的只是,卸载我的项目,将上面的代码复制到我的项目文件中。注意:默认情况下会注释“BeforeBuild”。
    • 此解决方案将更改原始 web.config,因此当您尝试在 VS 中构建项目时,它会要求您每次都覆盖 web.config。在我看来这是不可接受的。
    【解决方案3】:

    需要满足多个条件才能正确生成转换

    在 .csproj 中确保您拥有以下所有内容:

    <Project> ...
    
          <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
          <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets" />
          <Target Name="SetTransformAppConfigDestination" BeforeTargets="PrepareForBuild" Condition="exists('Web.$(Configuration).config')">
            <PropertyGroup>
              <!-- Force build process to use the transformed configuration file from now on. -->
              <AppConfig>$(IntermediateOutputPath)$(TargetFileName).config</AppConfig>
            </PropertyGroup>
            <Message Text="AppConfig transformation destination: = $(AppConfig)" />
          </Target>
          <Target Name="TransformAppConfig" AfterTargets="PrepareForBuild" Condition="exists('Web.$(Configuration).config')">
            <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="$(AppConfig)" />
          </Target>
        </Project>
    

    同时更新 .csproj 文件中的项目组。

    <ItemGroup> ...
    
    <Content Include="Web.config">
          <SubType>Designer</SubType>
        </Content>
        <Content Include="Web.Debug.config">
          <DependentUpon>Web.config</DependentUpon>
        </Content>
        <Content Include="Web.Release.config">
          <DependentUpon>Web.config</DependentUpon>
          <IsTransformFile>True</IsTransformFile>
        </Content>
        <Content Include="Views\Web.config" />
        <Content Include="Views\Home\Index.cshtml" />
      </ItemGroup>
    

    这将导致您的配置文件嵌套在资源管理器视图中

    然后确保在属性中正确设置路径 -> 构建

    然后为要转换的每个节点添加到 web.release.config 标记

    1. xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
    2. xdt:Transform="替换"

    看起来像这样:

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
       <appSettings xdt:Transform="Replace"> 
           <add key="VariableToBeTransformed" value="ValueToInsert"/> 
       </appSettings> 
       <system.web>
           <compilation xdt:Transform="RemoveAttributes(debug)" /> 
       </system.web>
    </configuration> 
    

    还要确保您的解决方案配置符合预期的调试/发布

    希望这对每个人都有帮助,您可以对 app.config 做同样的事情

    【讨论】:

      【解决方案4】:

      如果您使用的是 Visual Studio 2019(本示例中为社区版),则最新 msbuild.exe 的路径如下所示: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"

      您可以通过以下方式创建一个 msstrong textbuild 脚本:

      "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" MyProject.csproj /p:DeployOnBuild=true /p:PublishProfile=Release /p:VisualStudioVersion=16.0 
      

      其中参数DeployOnBuild在构建后触发多个目标,其中有一个转换web.config的目标。 "The DeployOnBuild=true property essentially means "I want to execute an additional target when build completes successfully."

      【讨论】:

        【解决方案5】:

        Visual studio 中的Publish 函数在您的项目中使用PublishProfiles/foo.pubxml 来确定您的配置,而MSBuild 则从project.sln 中确定您的配置。

        需要从Solution Explorer打开Configuration Manager→选择Active solution configuration→更改Project contexts下的Configuration

        【讨论】:

          猜你喜欢
          • 2013-01-03
          • 2013-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-06
          • 1970-01-01
          • 1970-01-01
          • 2015-05-05
          相关资源
          最近更新 更多