10

I currently have multiple projects being build using msbuild. I have a small customisation to the build that is handled by a .targets file. One solution is to add the snippet

<Import Project="MyTargets.targets"/>

to each project file. However, ideally I would like to not touch the project files, and be able to pass this information as a parameter to msbuild. That way I could easily control whether I run this customisation from the command line, and I don't have to touch the existing project files.

Is this possible?

4 Answers 4

9

You can do that easily with MSBuild 4.0 (check your version by top-level attribute ToolsVersion="4.0"):

There are multiple properties you can use to import your targets before and after Common.targets and or CSharp.targets loaded.

Simplest way is to use 2 sets of self explaining properties. First set is: $(CustomBeforeMicrosoftCommonTargets) $(CustomAfterMicrosoftCommonTargets)

and second one:

$(CustomBeforeMicrosoftCSharpTargets)
$(CustomAfterMicrosoftCSharpTargets)

Property names are pretty self-explained.

Just pass full file name to any of this properties via msbuild.exe e.g.

msbuild.exe /p:CustomBeforeMicrosoftCSharpTargets=c:\mytargets\custom.targets

You can use other "ImportByWildcard(Before|After)...." properties if you need to import multiple files. But in that case you need to pass more parameters to command-line.

4
  • Can you reference macros on the command line? E.g: /p:CustomBeforeMicrosoftCSharpTargets="$(SolutionDir)\custom.targets" ? This doesn't appear to work for me.
    – Rabid
    Dec 10, 2015 at 20:49
  • Doubt it'll work, this command line doesn't processed by msbuild engine, which unwraps these properties. You can specialize path relative to the path of build script itself Dec 10, 2015 at 21:36
  • Setting any of the above four properties, I can't get MSBuild to run any target of the imported file through /target:<target> (error: The target "<target>" does not exist in the project). If I set e.g. InitialTargets in the imported file, the target is executed. Is it not possible to externally refer to imported targets? Feb 18, 2019 at 9:07
  • Umm, what version of MSBuild you're currently using ? The answer above is 5 years old, can't recall all the specifics and things may change a bit since 2013 ;) Can you provide some more specific details or open a new question and reference it here? Feb 18, 2019 at 23:29
3

Starting from MSBuild 15.0, the following two files are auto-imported into your build in case they are found on the project path or in any parent folder on the path to the root directory:

  • Directory.Build.props
  • Directory.Build.targets

Remark: once the props or targets file is found, MSBuild will stop looking for a parent one.

Also see: https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

0

Lets say you have a project file called "Project.msbuild". You would add this conditional import:

<Import Project="$(TargetToImport)" Condition="'$(TargetToImport)' != ''" />

Then pass the name of the target file you want to import as an msbuild property:

msbuild.exe Project.msbuild /p:TargetToImport="TargetFile.Target"
1
  • This doesn't really answer the problem of not wanting to alter the project file (although it does mean that once I've made this alteration all other modifications can be done outside of the project file). The question is really asking whether I can effectively insert an <Import /> element via the command line.
    – Lawrence
    Aug 16, 2013 at 9:04
0

Make sure you use an absolute path to the target file and it works.
Source: Sayed Ibrahim Hashimi - MSBuild how to execute a target after CoreCompile part 2.

msbuild.exe /p:CustomBeforeMicrosoftCSharpTargets="c:\mytargets\custom.targets" /preprocess:out.xml

Use /preprocess[:filepath] to see the result of the imports.

You don't have to modify any csproj or vbproj files.
Of course, it only works where you can set MSBuild Properties.

1
  • For me, this does create an out.xml file, but it's always empty (zero bytes). Feb 18, 2019 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.