简体   繁体   中英

.csproj file doesn't properly import other project

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM