Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 717
D3vtr0n Avatar asked Feb 25 '11 16:02

D3vtr0n


People also ask

How do I create a solution using MSBuild command line?

To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.

Can MSBuild build .NET core?

You can build your . NET Core projects just as easily as your regular . NET projects, using either MSBuild ( MSBuild::Build-Project ) or the newer . NET Core CLI, dotnet.exe ( DotNet::Build ).


2 Answers

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>
like image 173
Brian Walker Avatar answered Sep 20 '22 21:09

Brian Walker


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!!

like image 23
D3vtr0n Avatar answered Sep 20 '22 21:09

D3vtr0n