【问题标题】:MSBuild condition to prevent app.config XML transform阻止 app.config XML 转换的 MSBuild 条件
【发布时间】:2017-06-07 18:13:48
【问题描述】:

我正在使用“SlowCheetah”VS 扩展来根据项目配置使用不同的值转换我们的 app.config。因此,“调试”配置会生成具有适合 Dev/Qa 用户的值的 app.config,而“发布”构建会生成具有生产值的 app.config。

.csproj 包含这样的部分:

<ItemGroup>
<None Include="App.config">
  <SubType>Designer</SubType>
  <TransformOnBuild>true</TransformOnBuild>
</None>
<None Include="App.Debug.config">
  <DependentUpon>App.config</DependentUpon>
  <IsTransformFile>True</IsTransformFile>
  <SubType>Designer</SubType>
</None>    
<None Include="App.Release.config">
  <DependentUpon>App.config</DependentUpon>
  <IsTransformFile >True</IsTransformFile>    
</None>
<None Include="packages.config" />
<None Include="Properties\SlowCheetah\SlowCheetah.Transforms.targets" />

msbuild 逻辑主要包含在“SlowCheetah.Transforms.targets”文件中。我的文件正在正确转换。

我想防止开发人员意外在 Visual Studio 中运行“发布”版本,并无意中使用生产配置文件运行我的应用程序。我的想法是使用 msbuild 条件,可能类似于:

Condition=" '$(BuildingInsideVisualStudio)'=='true' "

我已尝试在 .csproj 文件的多个位置使用此条件,但均未成功。我怀疑如果我修改“SlowCheetah.Transforms.targets”文件本身,我可以让它工作,但不应该根据顶部的 cmets 修改该文件。

理想情况下,我希望 Visual Studio 内部构建的所有配置都使用我的 Debug 配置文件,而 Visual Studio 外部的“发布”构建(例如在持续集成服务器上构建)以使用 Prod app.config,但我' d 满足于能够防止在 Visual Studio 中意外运行“发布”版本。任何关于是否/如何实现这一点的建议都值得赞赏。

【问题讨论】:

    标签: xml msbuild slowcheetah xdt-transform


    【解决方案1】:

    &lt;/Project&gt;之前添加这个:

      <Target Name="BeforeBuild">
        <Error Condition=" '$(BuildingInsideVisualStudio)'=='true' And '$(Configuration)'=='Release' "
               Text="JMc is so mad at you you trying to build using the Release configuration from Visual Studio." />
      </Target>
    

    【讨论】:

    • 做得很好。谢谢。
    • 使用相同条件的一个很好的替代方法是在 app.config 中指定 DEV 值,并为“发布”配置单独转换。然后将“条件”放在“SlowCheetah”属性组中。结果是只有在 VS 外部构建时才会发生转换。在 VS 中构建时,您总是会得到 DEV app.config 文件。
    • $([System .IO.Path]::GetFullPath($(MSBuildProjectDirectory)\..\packages\SlowCheetah.2.5.15\tools))..
    猜你喜欢
    • 2020-01-17
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多