1
votes

I have created a custom .targets file as below (Just added all the common tasks required in myproj.vcxproj file to .targets file)

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- *******************************************************************************************
        Common tasks
       ******************************************************************************************* -->

  <Target Name="H1">
    <Exec Command="del /F/Q @(S_PACK_H1)" />
    <RemoveDir Directories="@(D_PACK_H1)" />
  </Target>
  <Target Name="H2">
    <Exec Command="del /F/Q @(S_PACK_H2)" />
    <RemoveDir Directories="@(D_PACK_H2)" />
  </Target>
  <Target Name="H11">
    <Exec Command="del /F/Q @(S_PACK_H11)" />
    <RemoveDir Directories="@(D_PACK_H11)" />
  </Target>

</Project>

All the macros/arrays like S_PACK_H1, D_PACK_H11 are defined in myproj.vcxproj file after which I am importing this in myproj.vcxproj file as below

  <Import Project="C:\Program Files\MSBuild\MyCompany\Mycustom.targets" />

when I use the below cmd

msbuild myproj.vcxproj /t:H11

it gives an error "error MSB4057: The target "H11" does not exist in the project"

but If I have the same list of tasks in .vcxproj file instead of .targets file then it works fine.

  1. Can I define macros in .vcxproj file and use them in .targets file? Will MSBuild be able to get that definition/value? If not then how do I go about using/passing something defined in vxcproj file in .targets file?

  2. Why is msbuild not able to see my task when it is in .targets file Vs .proj file? what else do I need to do?

1

1 Answers

0
votes

There is no obvious reason for this not to work. Yes you can define targets in an imported file and they should be available, regardless of where the import occurs. If you are using MSBuild 4.0 (there is no ToolsVersion attribute on your .targets file above, so I'm not sure) then you can generate a fully preprocessed file, like this:

> msbuild mproj.vcxproj /pp

Look for the preprocessed file in the same folder. Open it up in a text editor and search for your imported content, it should all be there. If not, perhaps the preprocessed file can shed some light into what is going wrong.