1
votes

I've set up a web application project with an additional post-build step to zip some resulting files for release builds. To do this, I've added the MSBuild Community Tasks NuGet package. This works fine when I build within Visual Studio, and I've confirmed that the zip file is created. The relevant part of the csproj file is:

<PropertyGroup>
    <MSBuildCommunityTasksPath>$(SolutionDir)\.build</MSBuildCommunityTasksPath>
    <ZipFile>$(SolutionName).zip</ZipFile>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.targets" />

<Target Name="Zip" AfterTargets="Build" Condition="'$(Configuration)' == 'Release'">
    <ItemGroup>
        <ZipFiles Include="$(ProjectDir)\*.js; $(ProjectDir)\manifest.json" Exclude="$(ProjectDir)\*.map.js" />
    </ItemGroup>
    <Delete Files="$(ZipFile)" />
    <Zip Files="@(ZipFiles)" WorkingDirectory="$(ProjectDir)" ZipFileName="$(ZipFile)" ZipLevel="9" />
</Target>

I have some code in another project that attempts to automatically build the above project. Here is the code:

var projectInstance = new ProjectInstance(project.File.AbsolutePath);
projectInstance.SetProperty("Configuration", "Release");
projectInstance.SetProperty("SolutionDir", project.Solution.Directory.AbsolutePath);

projectInstance.Build("Build", Enumerable.Empty<ILogger>());

The above code fails with the following error message:

The "Zip" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with in the project file, or in the *.tasks files located in the "C:\Windows\Microsoft.NET\Framework\v4.0.30319" directory.

If I remove the Zip task, it works flawlessly. Why does this fail when I build programmatically, but not when I build within VS? How can I fix it?

1
Have you tried the same from Visual Studio Command prompt, using msbuild command. If not, can you try that once and post the result so?RinoTom
@RinoTom, I just tried that, and it produced the same result. It also included an error number: error MSB4036.Sam

1 Answers

-1
votes

This will be resolved if you give the option for package restore from Visual Studio's TOOLS Menu >> Options >> Package Manager >> General as shown in image below:

Enable Package Restore

This will restore the Nuget packages even when you are building the project using MSBuild. Otherwise it will restore only in Visual Studio builds and not in MSBuild builds. By that reason MSBuild.Community.Tasks Nuget was not getting restored for you in MSBuild but happens fine in Visual Studio.

So, try this and hope this should fix the issue.