3
votes

I work with .NET Core SDK version 2.1.302. My solution has two type of projects: libraries and web. All libraries are targeted to .NET Standard 2.0: <TargetFramework>netstandard2.0</TargetFramework> and web projects have multiple targets: <TargetFrameworks>net462;netcoreapp2.0</TargetFrameworks>

I have two CI builds: for Windows which uses net462 and build in docker based on linux with netcoreapp2.0.

In the docker build to build my solution I use the following line of code:

RUN dotnet build ./MySolution.sln --configuration Release --framework netcoreapp2.0

And build fails with the rrors like this:

Assets file '/app/MyLibraryProject/obj/project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.0'. Ensure that restore has run and that you have included 'netcoreapp2.0' in the TargetFrameworks for your project. [/app/MyLibraryProject.csproj]

It happens because as I mentioned before my library projects are targeted only one framework - netstandard2.0

So, my question is how to deal with this situation? How should I specify that projects with only one target framework should ignore --framework param?

1
There are 2 options for this I think: build the main project instead of the solution, or use msbuild to define the target framework based on the environment (you can use $(OS), or pass some custom property in the dotnet build command). - José Pedro
I do not want to use separate project builds, because I also have test projects which I should build separately too. After adding new project I should change my build scripts again. Looks like a lot of work. What about custom parameter: it looks possible to use it, but maybe I just miss a more simple solution with built-in keys like --framework - Ceridan
The second option is basically using a custom parameter, for example RUN dotnet build ./MySolution.sln --configuration Release /p:BuildCoreOnly=True. You just need to add conditional properties to the project, like <TargetFramework Condition="'$(BuildCoreOnly)' == 'True'">netcoreapp2.0</TargetFramework>. - José Pedro

1 Answers

3
votes

In the interests of making sure the answer is noticed, I found that the best way around this was @José Pedro's solution from the above comment stream.

In the csproj file, I put a condition on the TargetFramework element. Now it looks as follows:

<TargetFrameworks Condition="'$(CoreOnly)' != 'True'">net472;netcoreapp2.1</TargetFrameworks>
<TargetFramework Condition="'$(CoreOnly)' == 'True'">netcoreapp2.1</TargetFramework>

It will then by default compile both, but you can pass a CoreOnly parameter to only compile the .NET Core framework.

dotnet build MySolution.sln /p:CoreOnly=True