ILRepack for .Net Core Projects with MSBuild

2019-06-14 09:18发布

问题:

I want to integrate ILRepack in my MSBuild pipeline for a .Net Core project to merge all required dlls into a single exe/dll.

The useful NuGet-Package ILRepack.MSBuild.Task seems well fitted for that, however the example in the GitHub readme does not quite work for .Net Core projects and I can't figure out how I have to change this to be compatible with a .Net Core project:

<!-- ILRepack -->
<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'">

   <ItemGroup>
    <InputAssemblies Include="$(OutputPath)\ExampleAssemblyToMerge1.dll" />
    <InputAssemblies Include="$(OutputPath)\ExampleAssemblyToMerge2.dll" />
    <InputAssemblies Include="$(OutputPath)\ExampleAssemblyToMerge3.dll" />
   </ItemGroup>

   <ItemGroup>
    <!-- Must be a fully qualified name -->
    <DoNotInternalizeAssemblies Include="ExampleAssemblyToMerge3" />
   </ItemGroup>

   <ILRepack 
    Parallel="true"
    Internalize="true"
    InternalizeExclude="@(DoNotInternalizeAssemblies)"
    InputAssemblies="@(InputAssemblies)"
    TargetKind="Dll"
    OutputFile="$(OutputPath)\$(AssemblyName).dll"
   />

</Target>
<!-- /ILRepack -->

Clarification:

I just want to use the .csproj-Format introduced with .Net-Core but actually using net461 as TargetPlatform.

回答1:

Use this for .Net Core projects:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net461</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="ILRepack" Version="2.0.15" />
        <PackageReference Include="ILRepack.MSBuild.Task" Version="1.0.9" />
    </ItemGroup>

    <!-- ILRepack -->
    <Target Name="ILRepack" AfterTargets="Build">

        <ItemGroup>
            <InputAssemblies Include="$(OutputPath)\ExampleAssemblyToMerge1.dll" />
            <InputAssemblies Include="$(OutputPath)\ExampleAssemblyToMerge2.dll" />
            <InputAssemblies Include="$(OutputPath)\ExampleAssemblyToMerge3.dll" />
        </ItemGroup>

        <ItemGroup>
            <!-- Must be a fully qualified name -->
            <DoNotInternalizeAssemblies Include="ExampleAssemblyToMerge3" />
        </ItemGroup>

        <ILRepack
            Parallel="true"
            Internalize="true"
            InternalizeExclude="@(DoNotInternalizeAssemblies)"
            InputAssemblies="@(InputAssemblies)"
            TargetKind="Dll"
            OutputFile="$(OutputPath)\$(AssemblyName).dll" />

    </Target>
    <!-- /ILRepack -->

</Project>

Note

You can also use one <InputAssemblies Include="$(OutputPath)\*.dll" /> to merge all dll-files in the output folder