Skip to content

How to import a targets file multiple times with different parameters? #6819

@Nefcanto

Description

@Nefcanto

I have an API that imports many modules. Here's the code snippet:

<Project  Sdk="Microsoft.NET.Sdk.Web">
    // rest of the MSBuild code in Api.csproj
    <Import Project="/CRM/Ticketing/Module.targets" />
    <Import Project="/CRM/Customers/Module.targets" />
    <Import Project="/CRM/Loyalty/Module.targets" />
    <Import Project="/CRM/Club/Module.targets" />
<Project>

And each of those Module.targets files define two properties and import a base Module.targets file:


/CRM/Customers/Module.targets

<Project>
    <PropertyGroup>
        <Project>CRM</Project>
        <Module>Customers</Module>
    </PropertyGroup>
    
    <Import Project="/Base/Module.targets" />
</Project>

/CRM/Club/Module.targets


<Project>
    <PropertyGroup>
        <Project>CRM</Project>
        <Module>Club</Module>
    </PropertyGroup>
    
    <Import Project="/Base/Module.targets" />
</Project>

And this is the base Module.targets:

<Project>
  
  <ItemGroup>
    <Reference Include="$(Project).$(Module).Models">
      <HintPath>/$(Project)/$(Module)/Packages/$(Project).$(Module).Models.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="$(Project).$(Module).DataAccess">
      <HintPath>/$(Project)/$(Module)/Packages/$(Project).$(Module).DataAccess.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="$(Project).$(Module).Business">
      <HintPath>/$(Project)/$(Module)/Packages/$(Project).$(Module).Business.dll</HintPath>
      <Private>True</Private>
    </Reference>
  </ItemGroup>
  
  <ItemGroup>
    <Content Include="/$(Project)/$(Module)/Database.json">
      <Link>Database\$(Module).json</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

</Project>

So, basically what I'm trying to do is to centralize a module's MSBuild configuration and only provide parameters.

The problem is that MSBuild complains that:

warning MSB4011: "/Base/Module.targets" cannot be imported again. It was already imported at "/CRM/Customers/Project.targets (8,3)". This is most likely a build authoring error. This subsequent import will be ignored. [/CRM/CustomerApi/Api.csproj]

How can I solve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageHave yet to determine what bucket this goes in.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions