Import .targets file from command line in msbuild

后端 未结 4 1679
心在旅途
心在旅途 2021-02-15 02:44

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

相关标签:
4条回答
  • 2021-02-15 03:06

    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.

    0 讨论(0)
  • 2021-02-15 03:15

    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.

    0 讨论(0)
  • 2021-02-15 03:15

    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://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

    0 讨论(0)
  • 2021-02-15 03:21

    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"
    
    0 讨论(0)
提交回复
热议问题