Freigeben über


MSBuild script for compiling each .cs file into an EXE

I previously wrote about using MSBuild to build all .cs files in a directory into a single exe. Here's a companion script that builds every .cs file into a separate exe.

This might be helpful for directories where you keep sets of small projects, where each source file stands alone, that sort of thing.

(I'm posting these things just as simple illustrations to help people along with their own MSBuild things)

 <Project xmlns="https://schemas.microsoft.com/developer/msbuild/2003"
         DefaultTargets="CompileEach"
         ToolsVersion="3.5"
         >

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

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- Import Project="c:\.net3.5\Microsoft.Csharp.targets" /    -->

  <PropertyGroup Condition="'$(Configuration)'==''">
    <Configuration>Debug</Configuration>   <!-- Default -->
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)'=='Debug'">
    <Optimize>false</Optimize>
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>.\bin</OutputPath>
  </PropertyGroup>

  <!-- Specify the inputs by type and file name -->
  <ItemGroup>
    <CSFile Include="*.cs" />
  </ItemGroup>

  <!-- specify reference assemblies for all builds in this project -->
  <ItemGroup>
    <Reference Include="mscorlib" />
    <Reference Include="System" />
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System.Printing" />
    <Reference Include="ReachFramework" />
  </ItemGroup>

  <Target Name="CompileEach"
          DependsOnTargets="ResolveAssemblyReferences"
          >

    <!-- ResolveAssemblyReferences resolves the short names of the assemblies -->
    <!-- named in the @(Reference) vector, into fully-qualified DLL names.    -->
    <!-- The task is defined in the Microsoft.Csharp.targets file.            -->
    <!-- The output of it is the @(ReferencePath) vector.                     -->

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

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

    <!-- Run the Visual C# compilation on EACH .cs file. -->
    <!-- see https://msdn2.microsoft.com/en-us/library/s5c8athz.aspx  for more info    -->
    <!-- By using the %(CSFile.identity) scalar, we run this task once for each       -->
    <!-- file.  The converse would be @(CSFile.identity).  That would run the compile -->
    <!-- once, for all files, compiling them all together into a single assembly.     -->
    <!-- Not what we want here.                                                       -->

    <CSC 
       Sources="%(CSFile.Identity)"
       References="@(ReferencePath)"
       OutputAssembly="$(OutputPath)\%(CSFile.filename).exe"
       EmitDebugInformation="$(DebugSymbols)"
       Toolpath="$(MSBuildToolsPath)"
       Nologo="true"
       >
    </CSC>
  </Target>

</Project>

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

Comments