Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lukesmith/1031245 to your computer and use it in GitHub Desktop.
Save lukesmith/1031245 to your computer and use it in GitHub Desktop.
VS 2010 RTM - MSBuild Project file for App.config XML transformations.
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- 20110617 : Luke Smith : Imports Microsoft.WebApplication.targets for SP1 support. Added exists condition for when the TransformedConfig doesn't exist -->
<!-- 20110224 : Ryan Milligan : Created OverrideAppConfigWithTargetPath target to fix ClickOnce deploy bug -->
<!-- 20100827 : João Angelo : Fixed bug when using Publish command within Visual Studio -->
<PropertyGroup>
<!-- Prevent circular dependency on Build target -->
<PipelineDependsOnBuild>false</PipelineDependsOnBuild>
<!-- Override project config file name (By default is set to Web.config) -->
<ProjectConfigFileName>App.config</ProjectConfigFileName>
</PropertyGroup>
<!-- The transformation target (TransformWebConfig) in this targets file-->
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- Removes the need to set config files Build Action as Content -->
<ItemGroup>
<FilesForPackagingFromProject Include="$(ProjectConfigFileName)">
<DestinationRelativePath>$(ProjectConfigFileName)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
<!-- Insert transformation targets in the build process -->
<PropertyGroup>
<BuildDependsOn>
TransformWebConfig;
OverrideAppConfigWithTargetPath;
$(BuildDependsOn);
CopyTransformedConfig
</BuildDependsOn>
</PropertyGroup>
<PropertyGroup>
<TransformedConfig>$(TransformWebConfigIntermediateLocation)\transformed\App.config</TransformedConfig>
</PropertyGroup>
<!-- Overrides AppConfigWithTargetPath allowing the transformed config to be used for manifest generation -->
<Target Name="OverrideAppConfigWithTargetPath">
<ItemGroup>
<AppConfigWithTargetPath Remove="@(AppConfigWithTargetPath)" />
<AppConfigWithTargetPath Include="$(TransformedConfig)" Condition="'$(TransformedConfig)'!='' AND Exists('$(TransformedConfig)')">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
<!-- Copy transformed file to output directory -->
<Target Name="CopyTransformedConfig" Condition="'$(TargetName)' != ''">
<Copy Condition="Exists('$(TransformedConfig)')"
SourceFiles="$(TransformedConfig)"
DestinationFiles="$(OutputPath)$(TargetName)$(TargetExt).config" />
<Copy Condition="Exists('$(TransformedConfig)') And '$(TargetExt)' == '.exe'"
SourceFiles="$(TransformedConfig)"
DestinationFiles="$(OutputPath)$(TargetName).vshost.exe.config" />
</Target>
<!--
Override After Publish to support ClickOnce
AfterPublish target replaces the untransformed config file copied to the
deployment directory with the transformed one
-->
<Target Name="AfterPublish">
<PropertyGroup>
<DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig>
</PropertyGroup>
<!-- Publish copies the unstransformed App.config to deployment directory so overwrite it -->
<Copy Condition="Exists('$(DeployedConfig)')"
SourceFiles="$(TransformedConfig)"
DestinationFiles="$(DeployedConfig)" />
</Target>
</Project>
@davidbitton
Copy link

I've add this to my project file as such

<Import Project="..\AppConfig.Transformation.targets" />

just before the UsingTask TaskName="TransformXml" and I'm getting an error on build:

Error   1   "obj\x86\Release\TransformWebConfig\transformed\App.config;obj\x86\Release\OsgExporter.Shell.exe.config" is an invalid value for the "ConfigFile" parameter of the "GenerateApplicationManifest" task. Multiple items cannot be passed into a parameter of type "Microsoft.Build.Framework.ITaskItem".  OsgExporter.Shell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment