0

Scenario

I want to have a solution which contains:

  • A command-line application (Utility.exe)
  • A class library (Library.dll)
  • A lot of other stuff which needs to be built

And I would like to build the entire solution using msbuild. Additionally, I need to execute the Utility in a pre-build event of the class library.

Problem

Setting Utility.exe as a project dependency of Library.dll is no good since the pre-build is invoked before dependent projects are built.

I am currently thinking of invoking msbuild for just Utility.exe before building the entire solution. Is there a better / cleaner solution?

Edit

Related: MSBuild build order

helb
  • 7,609
  • 8
  • 36
  • 58

2 Answers2

1

One option is to build utility.exe as separate application with it's own build process. This build can then package the binary as a nuget package and store it in a nuget repository (this could just be a file share)

Once you have the package available then you can add it as a nuget reference to library.dll and have an additional step in your build process which restores nuget packages before the build step. Then your prebuild event can call the from the nuget packages folder.

James Reed
  • 13,873
  • 51
  • 60
  • NuGet certainly helps storing the binary between the two build processes, thanks for mentioning. The fundamental problem to build everything in a single build process remains though. – helb Aug 08 '18 at 10:53
1

I need to execute the Utility in a pre-build event of the class library.

If you want to do that, you can just call MSBuild.exe to build the command-line application (Utility.exe) in the pre-build event of the class library, like:

"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" "$(ProjectDir)..\UtilityApplication\UtilityApplication.csproj" /p:Configuration=$(Configuration) /p:Platform=$(Platform)

"$(ProjectDir)..\UtilityApplication\bin\Debug\UtilityApplication.exe"

Note: Do Not use $(SolutionDir) instead of $(ProjectDir)..\ (The root cause of your issue is also related to this, let me explain it.).

Reason:

Solutions contain projects, but a project can be in multiple different solutions (or no solution at all), so we could not access solution information when building a project individually. That is the reason why we could not use $(SolutionDir) when we build the project UtilityApplication.csproj.

It's also the culprit that is causing your problem, since the project reference info exists only in solution information. So, if you build your solution file or build your project from Visual Studio, you will not have your problem, the pre-build is invoked after dependent projects are built.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Thank you for sharing your thoughts. I just don't see why msbuild does not use the solution information the same way VisualStudio does when invoking msbuild with the solution file. With msbuild, the pre-build event comes first, then dependencies are built. With VS, the dependencies are built first, then comes the pre-build event. – helb Aug 09 '18 at 13:02
  • @helb, I test it on my side with a simple sample, it works fine when I build the solution file with MSBuild. How did you add the project reference? Are you using project-to-project? If yes, your project may have some special settings. Anyway, the solution in my answer should be work. You could accept it as an answer. This can be beneficial to other community members reading this thread. If you are interested in the build order for your question, that should be another question, you can open a new thread with more info and provide a sample, some steps how did build it, I will keep follow. – Leo Liu Aug 10 '18 at 01:26
  • I will have a look into it. Meanwhile, I guesss your solution is the best approach. – helb Aug 10 '18 at 08:17
  • @LeoLiu-MSFT Problem with `$(ProjectDir)..` is, that if `$(SolutionDir)` is used in `UtilityApplication -> Properties -> Additional Include Directory`, it's not properly resolved. However, this problem does not occur when using `MSBuild $(SolutionDir)MySolution.sln /t:UtilityApplication`. – Agent49 Apr 07 '20 at 17:17