project.assets.json not found with msbuild

2019-07-25 09:40发布

问题:

I have an ASP.NET Core application that i wish to build on a jenkins machine with MSBuild 15.

When i try to build i get the following error:

C:\Program Files\dotnet\sdk\2.1.502\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(198, 5): error NETSDK1004: Assets file 'C:\sync\Src\Util\myUtil\ob j\project.assets.json' not found. Run a NuGet package restore to generate this file

I understand that i need to do nuget restore somehow, but i failed to make it work.

My build process: Running a batch filed with the following command:

call "%VS150COMNTOOLS%VsDevCmd.bat"
MSBuild DailyBuild.proj /t:DailyBuild /p:VersionNumber=%2 /l:FileLogger,Microsoft.Build.Engine;logfile=Build.log

The DailyBuild.proj file look like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <SourcesPath>$(MSBuildProjectDirectory)\..\..\</SourcesPath>
    <CSCleanProperties>BuildType=Clean;Configuration=Release;IsFormalBuild=true</CSCleanProperties>
    <CSBuildProperties>BuildType=ReBuild;Configuration=Release;PauseBuildOnError=false;PublishWebSites=true;VersionName=myProd-$(VersionNumber)</CSBuildProperties>
  </PropertyGroup>

  <Target Name="DailyBuildWithClean">
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Targets="Clean" Properties="$(CSCleanProperties)"/>
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Properties="$(CSCleanProperties)"/>
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Targets="FormalBuild" Properties="$(CSBuildProperties)"/>
  </Target>

  <Target Name="DailyBuild">
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Targets="SW;PreparePackFolder" Properties="$(CSBuildProperties)"/>
  </Target>

</Project>

The Make.proj is a proj file containing definitions for many applications to be built, one of them is my ASP.NET Core app.

How do i fix this problem? thank you.

SOLUTION EDIT: Thanks to solution by Martin Ullrich:

Added in the DailyBuild.proj the target Restore, also added in the Make.proj a target called restore as suggested (IE:

<Target Name="Restore">
  <MSBuild Projects="$(SourcesPath)\my.sln" Targets="Restore" />
</Target>

)

回答1:

Add -r (-restore//Restore) to your MSBuild command to trigger a restore before the main build.

The restore parameter will build the Restore target, clear internal caches and then run the rest of the build as specified.

Since you build a custom MSBuild project, you then need to add a Restore target to it:

<Target Name="Restore">
  <MSBuild Projects="$(SourcesPath)\my.sln" Targets="Restore" />
</Target>

(or alternatively add another Restore target on the make.proj file and forward it from there to the solution or individual projects that you need to be restored)