0

Is it possible to call MSbuild publish during build or in pre-buildevent or in post-buildevent? I'm trying to publish two of the web projects from a solution. I'm using file system publishing.Requirement here is , building solution should take care of publish those two web projects. Can any one please suggest ?

Karthi
  • 1
  • 1

1 Answers1

0

I wouldn't put too much deploy logic in a post-build event. It becomes "fragile".

Create a separate .msbuild file, and do the "extra" logic in it, instead of messing too much with the .csproj file.

Below is a basic example. Place the xml below in an file call "MyBuildAndDeploy.msbuild", put it in the same folder as your .sln (or .csproj) file, and then use msbuild.exe "MyBuildAndDeploy.msbuild" from the command line.

So below is a basic example of building the primary solution and then copying the files somewhere.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapper">


    <PropertyGroup>
        <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
        <WorkingCheckout>.</WorkingCheckout>
        <BuildResultsRootFolder>$(WorkingCheckout)\..\BuildResults</BuildResultsRootFolder>
    </PropertyGroup>



    <Target Name="AllTargetsWrapper">

        <CallTarget Targets="BuildSolution" />
        <CallTarget Targets="CopyBuildOutputFiles" />

    </Target>


    <Target Name="BuildSolution">

        <MSBuild Projects="$(WorkingCheckout)\MySuperCoolSolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
            <Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"/>
        </MSBuild>
        <Message Text="BuildSolution completed" />

    </Target>   


    <Target Name="CopyBuildOutputFiles">


        <MakeDir Directories="$(BuildResultsRootFolder)\$(Configuration)" Condition="!Exists('$(BuildResultsRootFolder)\$(Configuration)\')"/>  

        <ItemGroup>
            <BuildOutputFilesExcludeFiles Include="$(WorkingCheckout)\**\*.doesnotexist" />
            <BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.dll" Exclude="@(BuildOutputFilesExcludeFiles)" />    
            <BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.exe" Exclude="@(BuildOutputFilesExcludeFiles)" />    
            <BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.config" Exclude="@(BuildOutputFilesExcludeFiles)" />
            <BuildOutputFilesIncludeFiles Include="$(WorkingCheckout)\**\*.pdb" Exclude="@(BuildOutputFilesExcludeFiles)" />
        </ItemGroup>  

        <Copy SourceFiles="@(BuildOutputFilesIncludeFiles)" 
            DestinationFolder="$(BuildResultsRootFolder)\$(Configuration)\"/>

    </Target>


</Project>
granadaCoder
  • 26,328
  • 10
  • 113
  • 146