Freigeben über


MSBuild script for compiling all .cs files into a single assembly (DLL or EXE)

I love Visual Studio, but sometimes I build code using no graphical IDE - just the .NET Framework SDK.  I wrote an article not long ago describing how to build a WCF Service using .NET 3.5, and just the SDK. 

I've always been a makefile weenie, but msbuild is the new build tool - it really makes sense to me.  One of the things that took a little effort for me was figuring out the msbuild script I wanted.  Basically I wanted to just compile all the C# code contained in a directory, into a single assembly (EXE or DLL). As I build out an idea, I often will iterate rapidly on an app - introducing and removing C# files as it makes sense to me, and I want the build script to remain the same - just compile whatever files it finds.

Enclosed here is what I came up with, for my .NET Framework 3.5 SDK environment.  Maybe it will work for you, too.  

    1 <Project xmlns="https://schemas.microsoft.com/developer/msbuild/2003"

    2         DefaultTargets="CompileAll"

    3         ToolsVersion="3.5"

    4   >

    5 

    6   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

    7   <!-- Import Project="c:\.net3.5\Microsoft.Csharp.targets" /    -->

    8 

    9   <PropertyGroup>

   10     <!-- This AppName thing is the base name of your DLL or EXE -->

   11     <AppName>YourAppNameHere</AppName>

   12   </PropertyGroup>

   13 

   14   <!-- This build file compiles each .cs file into its own exe -->

   15   <PropertyGroup Condition="'$(Configuration)'==''">

   16     <Configuration>Debug</Configuration>

   17     <!-- Default -->

   18   </PropertyGroup>

   19 

   20   <PropertyGroup Condition="'$(Configuration)'=='Debug'">

   21     <Optimize>false</Optimize>

   22     <DebugSymbols>true</DebugSymbols>

   23     <!-- <OutputPath>.\bin</OutputPath>  -->

   24     <OutputPath>.\</OutputPath>

   25     <OutDir>.\</OutDir>

   26     <IntermediateOutputPath>.\</IntermediateOutputPath>

   27   </PropertyGroup>

   28 

   29 

   30   <!-- Specify the inputs by type and file name -->

   31   <ItemGroup>

   32     <CSFile Include="*.cs"/>

   33   </ItemGroup>

   34 

   35 

   36   <!-- specify reference assemblies for all builds in this project -->

   37   <ItemGroup>

   38     <Reference Include="mscorlib" />

   39     <Reference Include="System" />

   40     <Reference Include="System.Core" />

   41     <Reference Include="System.Data" />

   42     <Reference Include="System.Data.Linq" />

   43     <Reference Include="System.ServiceModel" />

   44     <Reference Include="System.ServiceModel.Web" />

   45     <Reference Include="System.Runtime.Serialization" />

   46     <!-- <Reference Include=".\ObjectDumper.dll" /> -->

   47   </ItemGroup>

   48 

   49 

   50   <Target Name="CompileAll"

   51           DependsOnTargets="ResolveAssemblyReferences"

   52     >

   53 

   54     <Message Text="Reference = @(Reference)" />

   55     <Message Text="ReferencePath = @(ReferencePath)" />

   56 

   57 

   58     <!-- Message Text="MS Build Tools path: $(MSBuildToolsPath)" / -->

   59 

   60     <!-- Run the Visual C# compilation on all the .cs files. -->

   61 

   62     <CSC

   63       Sources="@(CSFile)"

   64       References="@(ReferencePath)"

   65       OutputAssembly="$(OutputPath)\$(AppName).exe"

   66       EmitDebugInformation="$(DebugSymbols)"

   67       TargetType="exe"

   68       Toolpath="$(MSBuildToolsPath)"

   69       Nologo="true"

   70         />

   71   </Target>

   72 

   73   <!-- redefine the Clean target, from the Microsoft.csharp.targets file. (Last definition wins) -->

   74   <Target Name="Clean">

   75     <Delete Files="$(OutputPath)\$(AppName).exe"/>

   76     <Delete Files="$(OutputPath)\$(AppName).pdb"/>

   77     <Delete Files="%(CSFile.identity)~"/>

   78     <Delete Files="build.xml~"/>

   79   </Target>

   80 

   81 </Project>

To use it, run this:
    c:\windows\Microsoft.NET\Framework\v3.5\msbuild.exe build.xml

Comments

  • Anonymous
    August 24, 2008
    The comment has been removed

  • Anonymous
    August 25, 2008
    Yes, I believe what you need is an attribute like ToolsVersion="3.5" in the toplevel <Project> element in your .msbuild file.

  • Anonymous
    December 22, 2008
    is it possible to  run a .cs file using msbuild?

  • Anonymous
    January 09, 2009
    @vani - not sure what you mean by "run a .cs file", but the msbuild file in the post will actually compile .cs files, if that is what you mean.

  • Anonymous
    March 22, 2009
    The comment has been removed

  • Anonymous
    March 28, 2009
    The comment has been removed