msbuildmsbuild-4.0

MSBuild dynamic AssemblyName


How can I change AssemblyName dynamically, based on content of some file?

For instance, I have file named "AssemblyBaseName.txt" which contains "Abcd" string I want my assembly DLL/EXE name to be Abcd.Common.dll, where "Common" is constant.

I've tried to use ReadLinesFromFile like this

  <Target Name="Build">
    <ItemGroup>
      <AssemblyBaseNameFile Include="Test.txt"/>
    </ItemGroup>
    <ReadLinesFromFile File="@(AssemblyBaseNameFile)">
      <Output TaskParameter="Lines" ItemName="AssemblyBaseName" />
    </ReadLinesFromFile>
    <Message Text="AssemblyBaseName: $(AssemblyBaseName)" />
  </Target>
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <ProductVersion>9.0.30729</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{FEE469DB-44BD-4CD9-BA08-91F1DFDE9679}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Common</RootNamespace>
    <AssemblyName Condition=" '$(AssemblyName)' == '' ">$(AssemblyBaseName).Common</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>

but did not succeed.


Solution

  • Since static properties are processed before any targets are executed, your target changed the value of $(AssemblyBaseName), but only after the value of the statically declared $(AssemblyName) was set – using the original value of $(AssemblyBaseName).

    You need to redefine $(AssemblyName) dynamically (inside your target) as well.

    Also, you've overridden the "Build" target. I'm not sure how this assembly will build, you need to use a differently named target, and be sure that it executes prior to any use of $(AssemblyName) by the normal build pipeline.