MSBuild error MSB3021: Unable to copy file. Could not find file 'obj\Release\myWebProject1.dll'

Alright, I figured it out. It's a "Configuration" mismatch. You have one project building with Configuration=MSBuildRelease and two other projects building with Configuration=Release. MSBuild then looks in the wrong place for the "intermediate" assemblies.

Change your code to this:

<?xml version="1.0" encoding="utf-8"?>
<Project
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
  ToolsVersion="4.0"
  DefaultTargets="Build">

  <PropertyGroup>
    <OutputDir>C:\MSBUILDRELEASE</OutputDir>
  </PropertyGroup>

  <ItemGroup>
    <ProjectToBuild Include="UtilityApp.sln" >
      <Properties>OutputPath=$(OutputDir);Configuration=MSBuildRelease;Platform=x86</Properties>
    </ProjectToBuild>
  </ItemGroup>

  <Target Name="Build">
    <MSBuild Projects="@(ProjectToBuild)"/>
            <CallTarget Targets="Publish WebProject1" />
            <CallTarget Targets="Publish WebProject2" />  
  </Target>

<Target Name="Publish WebProject1">
 <RemoveDir Directories="$(OutputFolder)"
       ContinueOnError="true" />
 <MSBuild Projects="WebProject1\WebProject1.csproj"
      Targets="ResolveReferences;_CopyWebApplication"
      Properties="WebProjectOutputDir=$(OutputDir)\WebProject1\;
      OutDir=$(OutputDir)\WebProject1\;Configuration=MSBuildRelease;Platform=AnyCPU" />
</Target>

<Target Name="Publish WebProject2">
 <RemoveDir Directories="$(OutputFolder)"
       ContinueOnError="true" />
 <MSBuild Projects="WebProject2\WebProject2.csproj"
      Targets="ResolveReferences;_CopyWebApplication"
      Properties="WebProjectOutputDir=$(OutputDir)\WebProject2\;
      OutDir=$(OutputDir)\WebProject2\;Configuration=MSBuildRelease;Platform=AnyCPU" />
</Target>

</Project>

After spending 3 hours on this bug, I started a new project and I imported each file from the old project one by one. I've been able to compile between each file until I added the last file.

I thought the problem was related to that last file but I realized by removing other files that this issue was happening only when I had a specific number of files included in my project.

I can't tell why it worked, but I solved this issue by adding / removing empty classes with random names to my project.

After a couple of Add / Compile / Remove / Compile again, VS started to work correctly.


Just a hunch, but I notice you are building the solution with Platform=x86, then calling the two WebProjects with Platform=AnyCPU. If those two projects are being built by the solution, the output location might be different for the build vs. the subsequent call to deploy.

Some other notes:

I generally avoid CallTarget, and would prefer this form in your case:

<Target Name="BuildProjects">
    <MSBuild Projects="@(ProjectToBuild)" />
</Target>
<Target Name="Build"
    DependsOnTargets="BuildProjects;Publish WebProject1;Publish WebProject2"
    />

The doubled slashes usually indicate one of two things:

$(OutDir)\$(Intervening)\bin

Either An intervening property was not evaluated, if $(Intervening) is empty, or one of the parts of the path already ends in a trailing slash, if the $(OutDir) property already has a trailing slash.

I never knew you could have spaces in a target name, I had to check it just to be sure and it worked!