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 i开发者_Go百科t 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.
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:
- Create
RenameDlls.targets
file (with content shown below) - Create
testfile.txt
along with the targets file - Execute
msbuild.exe RenameDlls.targets
- File
testfile.txt
will be copied astestfile_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>
I don't think there is a way to override it, but in a post build operation renaming it should be very possible. Use the PostBuildEvent.
精彩评论