justin.m.chase
justin.m.chase

Reputation: 13685

Relative paths for ItemGroup items in msbuild .targets files

I have a .dll and a .targets file in the same directory. Inside of that .targets file I would like to add the .dll to an ItemGroup item. However if I just add it similar to this:

<Example Include="Example.dll" />

The path to Example.dll appears to be resolving as relative to the .csproj that is including the .targets file. How can I add items to an ItemGroup in a .targets file with a relative path like this?

For example suppose I have:

C:\lib\Example.dll
C:\lib\Example.targets
C:\src\Example.csproj

When including the Example.dll from inside the .targets file the full path resolves to C:\src\Example.dll which is wrong, what I want is C:\lib\Example.dll. Does anyone have any suggestions?

Upvotes: 2

Views: 1668

Answers (1)

Brian Kretzler
Brian Kretzler

Reputation: 9938

Inside the .targets file, use this,

<Example Include="$(MSBuildThisFileDirectory)\Example.dll" />

...that reserved property will resolve to the directory in which the .targets file resides, not the project file importing the .targets file, which is the default for relative path resolution.

Upvotes: 7

Related Questions