C
C#•2mo ago
Davaaron

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
mtreit
mtreit•2mo ago
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.
Davaaron
Davaaron•2mo ago
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?
mtreit
mtreit•2mo ago
If you make a dependency between the two projects they should build in dependency order.
Davaaron
Davaaron•2mo ago
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?
Cracker
Cracker•2mo ago
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 ?
mtreit
mtreit•2mo ago
Right, that's what I meant by dependency.
Davaaron
Davaaron•2mo ago
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
mtreit
mtreit•2mo ago
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.
Davaaron
Davaaron•2mo ago
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
Cracker
Cracker•2mo ago
so project B references project A right ? is that a project reference in csproj ?
Davaaron
Davaaron•2mo ago
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
Cracker
Cracker•2mo ago
okay so there is no direct reference between that two project
Davaaron
Davaaron•2mo ago
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
mtreit
mtreit•2mo ago
You can put a custom target in project B that runs the build of project A
Davaaron
Davaaron•2mo ago
oh thats a good idea so simply adding a exec tag and then running dotnet build -c $(Configuration)...?
mtreit
mtreit•2mo ago
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
Davaaron
Davaaron•2mo ago
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 😄
mtreit
mtreit•2mo ago
I have an entire book on MSBuild. With enough effort you can make it do anything, it's extremely customizable.
Davaaron
Davaaron•2mo ago
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 😄
Davaaron
Davaaron•2mo ago
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.
Davaaron
Davaaron•2mo ago
Well i think so
mtreit
mtreit•2mo ago
Yeah, that's the one.
Want results from more Discord servers?
Add your server