Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild override output file name

I need to be able to overrire the name of output assemblies.

There is a file that contains the prefix, appid. I need to read the file to get this prefix and then to use it for assembly name. I.e., assembly name engine.dll and appid contains slot_113. Final output should be engine_slot113.dll.

I'm thinking about to create a separate .targets file, that I'm able to include to any .csproj file that requires that behaviour.

I found a task ReadFileLines to read file contains appid, but I'm not sure how to override output name.

like image 361
Alexander Beletsky Avatar asked Dec 07 '25 06:12

Alexander Beletsky


1 Answers

You can easily achieve this by using MSBuild Copy Task. Let's assume you already extracted suffix in the property $(AssemblyNameSuffix) and specify assembly files item group @(AssemblyDllFiles), so:

  1. Create RenameDlls.targets file (with content shown below)
  2. Create testfile.txt along with the targets file
  3. Execute msbuild.exe RenameDlls.targets
  4. File testfile.txt will be copied as testfile_SUFFIX.txt

Note: You can apply this action to multiple files as well


MSBuild targets:

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

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Default">

    <Target Name="Default" DependsOnTargets="CreateItems;CopyItems"/>

    <PropertyGroup>         
                <AssemblyNameSuffix>SUFFIX</AssemblyNameSuffix>
    </PropertyGroup>

    <Target Name="CreateItems">
        <ItemGroup>
            <AssemblyDllFiles Include="testfile.txt"/>
        </ItemGroup>
    </Target>

    <Target Name="CopyItems" 
            Inputs="@(AssemblyDllFiles)" 
            Outputs="@(AssemblyDllFiles->'.\%(RecursiveDir)%(Filename)_$(AssemblyNameSuffix)%(Extension)')">
           <Message Text="@(AssemblyDllFiles->'%(FullPath)', '%0a%0d')" 
                    Importance="low"/>

        <Copy SourceFiles="@(AssemblyDllFiles)" 
              DestinationFiles="@(AssemblyDllFiles->'.\%(RecursiveDir)%(Filename)_$(AssemblyNameSuffix)%(Extension)')" 
              SkipUnchangedFiles="true"/>
    </Target>
</Project>
like image 65
sll Avatar answered Dec 08 '25 20:12

sll