开发者

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

When using TeamCity to compile my MSBuild XML task script, it fails with this:

[10:43:03]: myWebProject1\ myWebProject 1 .csproj (3s)
[10:43:07]: [ myWebProject1\ myWebProject1 .csproj] _CopyWebApplicationLegacy
[10:43:07]: [_CopyWebApplicationLegacy] Copy
[10:43:07]: [Copy] C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets(131, 5): error MSB3021: Unable to copy file "obj\Release\myWebProject1.dll" to "C:\MSBUILDRELEASE\myWebProject1\\bin\myWebProject1.dll". Could not find file 'obj\Release\myWebProject1.dll'.

When I run it locally, it works.

When I compare my local output to my build server output, there are files missing on my build server. Like the global.asax file is missing from my build server output directory (but not when I compile this locally). Why is that?

Here is my current MSBuildScript:

<?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=Release;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=Release;Platform=AnyCPU" />
</Target>

</Project>

I can run this script locally and it seems to work fine (no errors generated). When I run it on my build server, it fails with MSBuild error MSB3021.

Now when I compare my local build output files to my server build output files, the server output does not have as many files. For instance, the global.ASAX file is missing in the output on my buildserver. Why would it work local for me, but not on my TeamCity build server? What's the difference and how can I fix it?

I noticed the TeamCity build agent error message has a funny directory path: "C:\MSBUILDRELEASE\myWebProject1\bin\myWebProject1.dll"

^ There are two slashes before the bin folder. I do not specify that anywhere. What gives? I have a feeling I am not building my Web Projects correctly (maybe use a different task approach?). It seems to work locally but not on my build server.

Am I building my web projects correctly? These are simply web projects for Web Service (ASMX) deployment. Help?


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>


I experience this problem often with old projects, that I have not opened for long time

The solution that works for me is:

  1. Open the project folder and delete the bin folder

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

  2. Make sure the project config is set to Debug not Release

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

  3. Build the project - VS will make new bin folder

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

I think that this problem is releated to the release in some kind of a way.


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!


I Faced the same issue when connecting to MDF via entity framework. Tackled it by simply cleaning the solution and rebuilding it. Hope it helps..


My project build again after delete the 'obj' and 'bin' folder.


If your Build is being ruined by MSB3021, start with checking your antivirus settings. Mine has a feature called Real-time file system protection. Whenever a file is being created or modified, it grabs it for analysis, which results in the file being locked. As soon as I disabled this feature, MSB3021 disappeared.


I had this issue when trying to deploy to appharbor, for me, excluding the file then re including it fixed the issue


While I had the exact same error; in my case I had a Wix (v3.9) Deployment project (MSI - for a desktop application) that ran Heat.exe to harvest the file from the output folder. Turned out that VS and Heat dont play well if you dont have the

RunAsSeparateProcess="true"

attribute set on the Heat Directory pre-build task. Found this after many hours of frustration. see Wix HeatFile Task Locks Dll for details. HTH someone.


Step 1: Restart your Visual Studio.

Step 2: Build >> Reconfigure app.

I got it right this way.


I had the same error. Look the Target framework. For mistake i've changed the netcore from 2.1 to 2.0


My solution for this is open TaskManager/win or Monitor/mac And kill all visual studio and related processes.


Quick solution for those, who are using Github - simply clone your last project commited edit to different empty folder. I did this right now and it was working.


the problem was error MSB3021 it work with me like this :-

  1. i delete the bin and obj in the root of my project
  2. i run dotnet restore and bulid it tell me that some processes are already in use so i restart the laptop to close all the processes
  3. dontnet run and every thing worked


Upgrade .net version works for me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜