Build order with dotnet build ...?
Hi,
Im using VS code for my C# development. I have some MSBuild Tasks that create assets that are used by other projects. Now, when I run
dotnet build
at solution level, the asset producing projects always run late. So I have to run it twice. I created a script to build each project in order by calling dotnet build
on the project level. However, some commands like dotnet publish
or dotnet release
seem to work better and easier on the solution level.
So I wonder is there maybe a trick to define the order? Maybe in the *.sln file? Or can I define it in the debug|release section somewhere?22 Replies
In your csproj you can use BeforeTargets and AfterTargets to control when certain targets run.
The build system has no dependency on solution files (you don't need a sln file in order to build) so that's definitely a dead end.
Maybe my goal wasnt described clear enough. The projects should run in order, not the tasks within a project. Or can I apply those attributes also on the sln level?
If you make a dependency between the two projects they should build in dependency order.
True. Im currently running each csproj one by one. But i thought maybe the solution file, which is a container for multiple projects, provide some functionality to run them in order?
if you use project reference across your csproj files within sln then build order will align with it
how do you reference your projects eachother ?
Right, that's what I meant by dependency.
Ok, that's a workaround, but not so nice because it would be included in my build output, also in my packed nuget, right? 😄
Well i think i can somehow exclude it there again
Feels like you are doing some very non-standard stuff if you have this kind of issue. Maybe step back and re-evaluate your entire approach.
Two projects that don't have an actual dependency on each other but somehow need to build in an exact order is strange.
Well yea i do some kind of non-standard stuff. The project A is my core project which needs to be built first. Then project B, which is a console wrapper for project B, is running. So if B is built before A, it still uses the old code
the special thing though is that the code is auto-generated and not C# code. It's included at runtime
so project B references project A right ?
is that a project reference in csproj ?
Project A creates javascript and python files
Project B is a console project which is a CLI tool, so you can call project A by using the command line tool accepting parameters which are passed along
Project B uses the files that are generated by Project A
Maybe its easier this way? 😄 sorry for the confusion
okay so there is no direct reference between that two project
maybe it's just a bad project design?
yea exactly
i think i will stick to the script
and when i migrate to azure devops, i still can call the script in the pipeline when needed
You can put a custom target in project B that runs the build of project A
oh thats a good idea
so simply adding a exec tag and then running dotnet build -c $(Configuration)...?
That's a bit blunt force but it should work 🙂
I think there is a MSBuild task
But if you just want to get it working, sure use Exec.
I've done this before, but can't remember what repo that's in
Yea im new to MSBuild although ive been working with C# for years 😄
i tried different stuff already but exec seems to be reliable in what i want 😄
I have an entire book on MSBuild. With enough effort you can make it do anything, it's extremely customizable.
yea i just know people hate it.. but privately i try to use it a little bit to get a little familiar with it because i dont have that automation level at home 😄
gotta admit the hardest part is finding the information on internet. but im suprised about the features
thats maybe why i have that weird project setup, im going crazy 😄
do you mean this MSBuild task?
https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-task?view=vs-2019
MSBuild Task - MSBuild
Explore how the MSBuild task uses the same MSBuild process to build child projects from another MSBuild project and review applicable properties and metadata.
Well i think so
Yeah, that's the one.