Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Publish (multiple) classic web application projects in multiple configurations to an output folder
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- CONFIGURE the configurations to publish for here -->
<PropertyGroup>
<ConfigurationsToPublish>Release;CD;UAT;Customer1;Customer2</ConfigurationsToPublish>
</PropertyGroup>
<!-- CONFIGURE the web projects to publish here. -->
<ItemGroup>
<ProjectToPublish Include="**\*.Web.csproj" />
</ItemGroup>
<!-- WARNING: Be careful with the rest of this file -->
<PropertyGroup>
<!-- Default artifact staging directory when built via VSTS / TFS agent -->
<PublishBaseDir Condition="'$(PublishBaseDir)' == '' and '$(BUILD_ARTIFACTSTAGINGDIRECTORY)' != '' ">$(BUILD_ARTIFACTSTAGINGDIRECTORY)</PublishBaseDir>
<!-- If not built on a known agent, use a "publish" subdir next to this file -->
<PublishBaseDir Condition="'$(PublishBaseDir)' == ''">$(MSBuildThisFileDirectory)publish\</PublishBaseDir>
<!-- Normalize directory if set manually or through ENV var -->
<PublishBaseDir Condition="!HasTrailingSlash('$(PublishBaseDir)')">$(PublishBaseDir)\</PublishBaseDir>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="_PublishProjectsOnInnerBuild">
<ItemGroup Condition="'$(Configuration)' == ''">
<InnerBuildConfiguration Include="$(ConfigurationsToPublish)" />
</ItemGroup>
<!-- Call inner build -->
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build" Properties="Configuration=%(InnerBuildConfiguration.Identity)" Condition="'$(Configuration)' == ''" />
</Target>
<!-- Preprare current configuration for publish (inner build). If Configuration isn't set, don't do anything (=> outer build) -->
<Target Name="_PublishProjectsOnInnerBuild" Condition="'$(Configuration)' != ''">
<ItemGroup>
<ResolvedProjectToPublish Include="@(ProjectToPublish)">
<PublishUrl>$(PublishBaseDir)%(Filename)\$(Configuration)\</PublishUrl>
<IntermediateOutputPath>%(RootDir)%(Directory)obj\$(Configuration)\</IntermediateOutputPath>
<PublishProfileUrl>%(ResolvedProjectToPublish.IntermediateOutputPath)GeneratedProfile.pubxml</PublishProfileUrl>
</ResolvedProjectToPublish>
</ItemGroup>
<MakeDir Directories="@(ResolvedProjectToPublish->'%(IntermediateOutputPath)')" />
<MSBuild Projects="$(MSBuildProjectFile)"
Targets="_PublishProject"
Properties="Configuration=$(Configuration);TargetProject=%(ResolvedProjectToPublish.Identity);PublishProfileUrl=%(ResolvedProjectToPublish.PublishProfileUrl);PublishUrl=%(ResolvedProjectToPublish.PublishUrl)" />
</Target>
<!-- expects $(Configuration), $(TargetProject), $(PublishProfileUrl) and $(PublishUrl) to be set (inner build) -->
<Target Name="_PublishProject">
<PropertyGroup>
<PublishProfile>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>$(Configuration)</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>$(PublishUrl)</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
</PropertyGroup>
</Project>
</PublishProfile>
<PublishBuildProperties>
Configuration=$(Configuration);
DeployOnBuild=true;
PublishProfile=$([System.IO.Path]::GetFullPath('$(PublishProfileUrl)'))
</PublishBuildProperties>
</PropertyGroup>
<WriteLinesToFile File="$(PublishProfileUrl)" Lines="$(PublishProfile)" Overwrite="true" />
<MSBuild Projects="$(TargetProject)" Targets="Build" Properties="$(PublishBuildProperties)" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment