I want to have a solution which contains:
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.
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?
Related: MSBuild build order
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.