【问题标题】:web.config file transform from command line从命令行转换 web.config 文件
【发布时间】:2012-05-17 09:22:59
【问题描述】:

我有两个构建环境可以定位;发布和分期。 Web.config 如下所示:

<system.web>     
    <authentication mode="Windows">
    </authentication>

    <authorization>
        <deny users="?" />
    </authorization>
</system.web>

我想通过构建到暂存配置来转换它:Web.Staging.config

<system.web>     
    <authentication mode="Windows">
    </authentication>

    <authorization xdt:Transform="Replace">
        <deny users="?" />
        <allow roles="StagingRoles" />
        <deny users="*" />
    </authorization>
</system.web>

我是这样从命令行构建的:

msbuild buildscript.build /p:Configuration=Staging

构建后,我没有看到构建工件文件夹中转换的 web.config 文件。这里有什么问题吗?

谢谢

【问题讨论】:

    标签: msbuild


    【解决方案1】:

    如果您将以下 xml 添加到 Web 应用程序的 .csproj 文件的底部,您将确保配置转换发生在每次构建之前:

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

    编辑:响应您的评论,您应该能够使用 Web.config 作为 TransformXml 任务中的源参数(参见步骤 #2)。如果您只想在构建脚本中执行配置转换,请按照以下说明操作:

    1) 在构建脚本中导入 WebApplication.targets,如下所示:

    <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
    

    2) 在构建脚本目标中执行 TransformXml 构建任务:

    <Target Name="MyBuildScriptTarget">
        <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
        ...other build tasks...
    </Target>
    

    【讨论】:

    • 感谢您的快速回复。问题是,我需要维护另一个配置文件:Web.base.config。当我用 VS 构建解决方案时,它会转换不必要的 Web.config。我想要在运行构建脚本时进行转换。
    • 这可行,但有一个问题。当转换发生时,我得到一个有点像这样的错误:Web.config 文件正在被另一个进程使用。我在编译目标之后调用了这个。
    • 如果您将 Web.config 移动到另一个目录,您可以将 TransfromXml 的 Source 参数更改为 Source="ConfigDir\Web.config"。如果这样做,您可能希望在 BeforeBuild 目标中执行配置转换,以便始终将对 ConfigDir\Web.config 所做的更改复制到根目录。另一种选择是创建一个您必须维护的 Web.Base.config 文件。
    【解决方案2】:

    乔纳森的回答很好。我稍微调整了一下以允许保留原始的 Web.config 文件。这些是我添加到 .csproj 文件底部的行:

      <!-- the step to copy the config file first avoids a 'File is being used by another process' error -->
      <Target Name="BeforeBuild">
        <Copy SourceFiles="Web.config" DestinationFiles="Web.temp.config" OverwriteReadOnlyFiles="True" />
        <TransformXml Source="Web.temp.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
      </Target>
      <Target Name="AfterBuild">
        <Copy SourceFiles="Web.temp.config" DestinationFiles="Web.config" OverwriteReadOnlyFiles="True" />
        <Delete Files="Web.temp.config" />
      </Target>
    

    我可以看到 web.config 文件通过在 Visual Studio 中运行构建(或从命令行)进行了转换。

    【讨论】:

    • 这个答案比我在测试时更好的 b/c 更改了我在 VS 项目中的原始 web.config 文件
    • XML 中有错误。在 TransformXml 中,您应该交换 Source 和 Destination。否则不应用转换。无论如何,你的回答非常感谢!经过几天的搜索,这似乎是网络上唯一正确的答案
    • 优秀。谢谢。真的感觉虽然我们不应该需要这样做(以前也不需要这样做)但是嘿,它有效......
    【解决方案3】:

    对乔纳森的回答有微小的改进:

    使用下面的这一行来导入 Web 目标将允许与任何 Visual Studio 版本兼容。请注意,这与 v10.0 版本无关

    <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
    

    【讨论】:

      【解决方案4】:

      使用 Jenkins 构建转换,我还看到 web.config 不会转换,但是,实际转换发生在您进行部署时。我使用一体式 msbuild 命令进行构建和部署。

      MSBuild MyProj.csproj /P:Configuration=Release /P:DeployOnBuild=True /P:DeployTarget=MsDeployPublish /P:MsDeployServiceUrl=https://your server/msdeploy.axd /P:AllowUntrustedCertificate=True /P:MSDeployPublishMethod=WMSvc /P:CreatePackageOnPublish=True /P:UserName=username /P:Password=password1 /P:DeployIISAppPath="Default Web Site or name of your website"

      运行后,您可以在服务器上验证是否发生了转换。

      【讨论】:

      • 感谢@Sandeep 详细解释转换发生在您进行部署时,这有助于我理解和解决我的问题。
      【解决方案5】:

      如果您想继续使用命令行,请将其添加到命令中:

      /p:UseWPP_CopyWebApplication=true /p:PipelineDependsOnBuild=false
      

      Source

      【讨论】:

        猜你喜欢
        • 2011-06-18
        • 2019-04-10
        • 2018-06-15
        • 2019-07-14
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 2021-05-22
        • 2012-06-16
        相关资源
        最近更新 更多