2

Project1.csproj contains an import statement, which should (in ideal case) import Project2:

<Import Project="Project2.targets"/>

Project2.targets contains only a list of resource imports:

<Project DefaultTargets="BeforeBuild"  
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
<Target Name="BeforeBuild">   
  <ItemGroup>
    <Resource Include="resources\logo.png" />
  </ItemGroup>
  <ItemGroup>
    <Resource Include="resources\icon.ico" />
  </ItemGroup>
</Target>
</Project>  

In Project1, elements use these Project2-defined resources, such as the icon. Even though the build is successful, the resources are not included in the final executable.

What exactly am I doing wrong? Replacing the import statement in Project1.csproj with ItemGroup resource definitions results in working program.

My goal is to import an externally defined list of resources, which I thought could be done by importing another project.

Vilda
  • 1,675
  • 1
  • 20
  • 50
  • Possibly the resources are added to the Resource ItemGroup too late because they are in a target which runs after parsing the global ItemGroups and as a result some other component which refers to ItemGroup before BeforeBuild has ran doesn't see them.. Do they have to be in a Target? If not, that might just solve the problem. – stijn Aug 01 '17 at 14:29
  • Also, depending on where the `` is placed in the source file, the `BeforeBuild` target might be overwritten. However, I'd also suggest putting the items outside of the target. – Martin Ullrich Aug 01 '17 at 15:25
  • @stijn Moving items out of `Target` and placing `Import` at the end of Project1.csproj indeed solved the issue. I will accept either your or Martin Ullrich's answer, if you post it. – Vilda Aug 01 '17 at 16:43

1 Answers1

2

The Import isn't the problem here: importing a file is simlilar to copying it's content to the place where the Import occurs. The problem is the Resource items are being added within a target and either:

  • the target doesn't run at all (because it gets overriden as Martin commented, or because it is not even considered for execution)
  • the target runs, but by the time that happens another component which takes care of getting the Resources included in the executable has ran already, and as such only considered the first global ItemGroup definition without your items added to it

So moving the ItemGroup out of the Target fixes this, and you can instrument your code with some logging via the Message task if you want to dig deeper into finding the root cause.

stijn
  • 34,664
  • 13
  • 111
  • 163