MSBuild - How to build a .NET solution file (in an XML task script) from pre-written command line commands

32,133

Solution 1

Use the MSBuild task to build the solution passing the properties you need.

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

    <PropertyGroup>
        <OutputDir>c:\TESTMSBUILDOUTPUT</OutputDir>
    </PropertyGroup>

    <ItemGroup>
        <ProjectToBuild Include="MySecretApplication.sln">
            <Properties>OutputPath=$(OutputDir);Configuration=Release</Properties>
        </ProjectToBuild>
    </ItemGroup>

    <Target Name="Build">
        <MSBuild Projects="@(ProjectToBuild)"/>
    </Target>

</Project>

Solution 2

Here is my final MSBuild script:

<?xml version="1.0" encoding="utf-8"?>

<PropertyGroup>
    <OutputDir>C:\TESTMSBUILDOUTPUT</OutputDir>
</PropertyGroup>

<ItemGroup>
    <ProjectToBuild Include="MyTopSecretApplication.sln" >
        <Properties>OutputPath=$(OutputDir);Configuration=MSBuildRelease;Platform=x86</Properties>
    </ProjectToBuild>
</ItemGroup>

<Target Name="Build">
    <MSBuild Projects="@(ProjectToBuild)"/>
</Target>

As you can see, the main difference from Brian Walker's answer is the "Configuration" setting. Here it is set to "MSBuildRelease" instead of "Release", which I customized from within the .NET IDE. Follow the steps Brian suggested (right-click on the solution node and choose Configuration Manager in Visual Studio, add "NEW" configuration and remove/uncheck projects you want excluded.)

I have since uploaded this to my TEAMCITY server, and have automated my .NET build process with your help. Thanks so much to Brian Walker (a fellow Texan)...I'll buy you a beer!! CHEERS!!

Share:
32,133
D3vtr0n
Author by

D3vtr0n

Wouldn't you prefer a nice game of chess? Everything is a hack. SO, stop changing my picture!! After 13 years, why?

Updated on July 09, 2022

Comments

  • D3vtr0n
    D3vtr0n almost 2 years

    I have been studying MSBuild as I have the need to automate my development shop's builds. I was able to easily write a .BAT file that invokes the VS command prompt and passes my MSBuild commands to it. This works rather well and is kinda nifty.

    Here is the contents of my .BAT build file:

    call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\vcvars64.bat"
    cd  C:\Sandbox\Solution
    msbuild MyTopSecretApplication.sln /p:OutputPath=c:\TESTMSBUILDOUTPUT /p:Configuration=Release,Platform=x86
    pause
    

    ^ This works well but I now have the need to use the MSBuild task for TeamCity CI. I have tried to write a few MSBuild scripts but I cannot get them to work the same. What is the equivalent build script to the command I am using in my .BAT file? Any ideas?

    I have tried using something like this, but no success (I know this is wrong):

    <?xml version="1.0"?>
    <project name="Hello Build World" default="run" basedir=".">
    
    <target name="build">
        <mkdir dir="mybin" />
        <echo>Made mybin directory!</echo>
    
      <csc target="exe" output="c:\TESTMSBUILDOUTPUT">
        <sources>
          <include name="MyTopSecretApplication.sln"/>
        </sources>
      </csc>
      <echo>MyTopSecretApplication.exe was built!</echo>
    </target>
    
    <target name="clean">
        <delete dir="mybin" failonerror="false"/>
    </target>
    
    <target name="run" depends="build">
      <exec program="mybin\MyTopSecretApplication.exe"/>
    </target>
    

    What I simply need is an MSBuild XML build script that compiles a single solution for Release mode to a specified output directory. Any help?