cml*_*ano 12 .net c# msbuild slowcheetah xdt-transform
我需要能够使用msbuild转换我的app.config文件.我可以转换文件,如果它被称为app.DEBUG.config或app.Release.config,但我不能添加一个名为app.PROD.config的文件.
使用常规XDT转换如果我选择不同的PublishProfile,msbuild会识别不同的web.config文件
msbuild path.to.project.csproj Configuration=Release PublishProfile=DEV
Run Code Online (Sandbox Code Playgroud)
显然app.config不能使用相同的设置.我总是可以为DEV.config设置创建一个特定的构建配置,但对一个应用程序进行单独的构建确认似乎没用.一个hacky方法是只为每个环境POST-BUILD复制正确的app.config.
我试图使用SlowCheetah插件,但这似乎只是转换默认的DEBUG和RELEASE配置文件,这是不合适的,因为我有两个以上的环境.如果我确实使用了这个错误,请让我知道我应该将哪个参数传递给msbuild来选择我的app.DEV.config.
预期的结果是msbuild将根据自定义的变换app.DEV.config或为app.PROD.config自定义的变换来变换app.config.我希望有一个参数可以传递给msbuild,这将允许我使用Release配置,但每个环境使用不同名称的转换.
我认为令人困惑的是我们有能力进行编译时配置转换,然后我们有部署时配置转换.
通常,您使用编译时配置转换来更改本地默认的配置文件,以便它适用于DEBUG或RELEASE配置(或您定义的任何自定义配置).对于web.config,工具是内置的.对于app.config,SlowCheetah Visual Studio扩展为web.config提供了与app.config相同的功能.RELEASE配置的配置转换示例是删除system.web编译中的debug属性.
部署时配置转换是在部署到特定环境(例如QA,PROD)时对配置文件的操作.数据库连接字符串需要更改,服务端点更改等...对于web.config,MSDEPLOY是首选的IIS工具.对于app.config,似乎我们需要依赖安装程序技术.有不同的工具,例如WIX.
无论如何,我希望这个对编译时和部署时配置转换之间区别的简短解释有助于解释为什么工具集是碎片化的.有关更深入的分析,您可以参考我就此主题撰写的博文:http://philippetruche.wordpress.com/2012/07/11/deploying-web-applications-to-multiple-environments-using-微软的Web部署/
如果选择使用WIX工具集生成安装程序,请参阅使用Visual Studio 2012和Wix创建多环境Windows安装程序.
App.config 转换
要测试转换是否有效,您必须使用真正的转换。使用 appSettings 块插入转换可能是最简单的一种。我使用以下配置文件进行了测试。
应用程序配置:
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="FirstName" value="Gunnar"/> </appSettings></configuration>
Run Code Online (Sandbox Code Playgroud)
App.Release.config
<?xml version="1.0"?><configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="LastName" value="Peipman" xdt:Transform="Insert"/> </appSettings></configuration>
Run Code Online (Sandbox Code Playgroud)
改造后的配置文件:
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="FirstName" value="Gunnar"/> <add key="LastName" value="Peipman"/> </appSettings></configuration>
Run Code Online (Sandbox Code Playgroud)
使 App.config 转换工作
让我们看看如何使用控制台应用程序来做到这一点。
在第一个属性组的结束标记之前添加以下行:
<ProjectConfigFileName>App.Config</ProjectConfigFileName>
Run Code Online (Sandbox Code Playgroud)找到<ItemGroup>App.Config 的定义位置 ( <None
Include="App.Config" />) 并在 App.Config 节点后添加以下块:
<None Include="App.Release.config">
<DependentUpon>App.Config</DependentUpon>
</None>
Run Code Online (Sandbox Code Playgroud)找到第一个<Import Project=节点并将以下导入作为最后一个添加到列表中:
<Import Project="$(VSToolsPath)\Web\Microsoft.Web.Publishing.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" Condition="false" />
Run Code Online (Sandbox Code Playgroud)在文件末尾,就在标记之前,粘贴以下代码块:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetName).vshost$(TargetExt).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
Run Code Online (Sandbox Code Playgroud)保存项目文件,关闭它并重新加载它。
再次加载项目时,Visual Studio 可能会询问您对文件的一些修改,因此从 Visual Studio 2010 到当前的所有版本都可以使用您的项目文件而无需对其进行修改。同意它,因为那样你就没有对 Visual Studio 版本的依赖。
这就是我在这个场景中使用的:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This target will run right before you run your app in Visual Studio -->
<Target Name="UpdateWebConfigBeforeRun" BeforeTargets="Build">
<Message Text="Configuration: $(Configuration) update from web.template.$(Configuration).config"/>
<TransformXml Source="web.template.config"
Transform="web.template.$(Configuration).config"
Destination="web.config" />
</Target>
<!-- Exclude the config template files from the created package -->
<Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
<ItemGroup>
<ExcludeFromPackageFiles Include="web.template.config;web.template.*.config"/>
</ItemGroup>
<Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
</Target>
</Project>
Run Code Online (Sandbox Code Playgroud)
我有以下设置:
web.template.config
- web.template.debug.config
- web.template.production.config
- web.template.release.config etc
Run Code Online (Sandbox Code Playgroud)
应该可以跨电脑工作,不需要额外的插件等。在你的场景中,你需要编辑内容来app.代替web.
| 归档时间: |
|
| 查看次数: |
25430 次 |
| 最近记录: |