我需要能够使用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
显然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配置,但每个环境使用不同名称的转换。
这是我在这种情况下使用的:
<?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>
我有以下设置:
web.template.config
- web.template.debug.config
- web.template.production.config
- web.template.release.config etc
应该在没有额外插件的情况下工作。在你的场景中,你需要编辑内容来说app.
而不是web.
我认为令人困惑的是我们有能力进行编译时配置转换,然后我们有部署时配置转换。
通常,您使用编译时配置转换来更改本地默认的配置文件,以便它适用于DEBUG或RELEASE配置(或您定义的任何自定义配置)。对于web.config,工具是内置的。对于app.config,SlowCheetah Visual Studio extension为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-microsoft-web-deploy/
如果选择使用WIX工具集生成安装程序,请参阅Creating Multi-Environment Windows Installers with Visual Studio 2012 and Wix。
App.config转换
要测试变换是否有效,您必须使用真正的变换。使用appSettings块进行插入转换可能是最简单的。我测试了以下配置文件。
App.config中:
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="FirstName" value="Gunnar"/> </appSettings></configuration>
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>
变换后的配置文件:
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="FirstName" value="Gunnar"/> <add key="LastName" value="Peipman"/> </appSettings></configuration>
使App.config转换工作
让我们看看如何使用控制台应用程序来完成它。
<ProjectConfigFileName>App.Config</ProjectConfigFileName>
<ItemGroup>
(<None
Include="App.Config" />
)并在App.Config节点后添加以下块:
<None Include="App.Release.config">
<DependentUpon>App.Config</DependentUpon>
</None>
<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" />
<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>
再次加载项目时,Visual Studio可能会询问您对文件的一些修改,因此从Visual Studio 2010到当前的所有版本都可以使用您的项目文件而无需对其进行任何修改。同意它,因为那时您没有Visual Studio版本的依赖项。
您可以将app.config转换为与web.config相同的转换。使用这个位于https://github.com/acottais/msbuild.xdt的nuget包
还有其他几个Nuget软件包可以让你这样做。我前几天使用过这个,它起作用了。
安装后,它就像VS Window中的Add-Transform一样简单。我昨天用自定义配置做了这个。 我在网上看了一堆教程,让我卸载并更改项目文件。有些人接近了,但为此付出了很多努力。