.net MSBuild 错误 MSB3021:无法复制文件。找不到文件“obj\Release\myWebProject1.dll”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5158313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MSBuild error MSB3021: Unable to copy file. Could not find file 'obj\Release\myWebProject1.dll'
提问by D3vtr0n
When using TeamCity to compile my MSBuild XML task script, it fails with this:
当使用 TeamCity 编译我的 MSBuild XML 任务脚本时,它失败了:
[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?
当我将本地输出与构建服务器输出进行比较时,构建服务器上缺少文件。就像我的构建服务器输出目录中缺少 global.asax 文件一样(但在我本地编译时没有)。这是为什么?
Here is my current MSBuildScript:
这是我当前的 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.
我可以在本地运行这个脚本,它似乎工作正常(没有产生错误)。当我在构建服务器上运行它时,它失败并显示 MSBuild 错误 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?
现在,当我将本地构建输出文件与服务器构建输出文件进行比较时,服务器输出没有那么多文件。例如,我的构建服务器上的输出中缺少 global.ASAX 文件。为什么它对我来说可以在本地运行,而不能在我的 TeamCity 构建服务器上运行?有什么区别,我该如何解决?
I noticed the TeamCity build agent error message has a funny directory path: "C:\MSBUILDRELEASE\myWebProject1\bin\myWebProject1.dll"
我注意到 TeamCity 构建代理错误消息有一个有趣的目录路径:“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.
^ bin 文件夹前有两个斜线。我没有在任何地方指定。是什么赋予了?我有一种感觉,我没有正确构建我的 Web 项目(也许使用不同的任务方法?)。它似乎在本地工作,但不在我的构建服务器上。
Am I building my web projects correctly? These are simply web projects for Web Service (ASMX) deployment. Help?
我是否正确构建了我的网络项目?这些只是用于 Web 服务 (ASMX) 部署的 Web 项目。帮助?
采纳答案by Carl
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.
好吧,我想通了。这是“配置”不匹配。您有一个使用 Configuration=MSBuildRelease 构建的项目和另外两个使用 Configuration=Release 构建的项目。然后,MSBuild 会在错误的位置查找“中间”程序集。
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>
回答by The_Black_Smurf
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.
在这个 bug 上花了 3 个小时后,我开始了一个新项目,我从旧项目中一个一个地导入了每个文件。我已经能够在每个文件之间进行编译,直到我添加了最后一个文件。
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.
再次添加/编译/删除/编译后,VS 开始正常工作。
回答by Brian Kretzler
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.
只是一个预感,但我注意到您正在使用 Platform=x86 构建解决方案,然后使用 Platform=AnyCPU 调用两个 WebProject。如果解决方案正在构建这两个项目,则构建与随后的部署调用的输出位置可能不同。
Some other notes:
其他一些注意事项:
I generally avoid CallTarget, and would prefer this form in your case:
我通常避免 CallTarget,并且在您的情况下更喜欢这种形式:
<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.
如果 $(Intervening) 为空,或者路径的一部分已经以尾部斜杠结尾,如果 $(OutDir) 属性已经有尾部斜杠,则未评估介入属性。
I never knew you could have spaces in a target name, I had to check it just to be sure and it worked!
我从不知道目标名称中可以有空格,我必须检查它以确保它有效!
回答by Jay
I had this issue when trying to deploy to appharbor, for me, excluding the file then re including it fixed the issue
我在尝试部署到 appharbor 时遇到了这个问题,对我来说,排除文件然后重新包含它解决了这个问题
回答by Shreyas Murali
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
虽然我有完全相同的错误;就我而言,我有一个 Wix (v3.9) 部署项目(MSI - 用于桌面应用程序),它运行 Heat.exe 从输出文件夹中收集文件。事实证明,如果你没有
RunAsSeparateProcess="true"
RunAsSeparateProcess="true"
attribute set on the Heat Directory pre-build task. Found this after many hours of frustration. see Wix HeatFile Task Locks Dllfor details. HTH someone.
在 Heat Directory 预构建任务上设置的属性。经过数小时的挫折后发现了这一点。有关详细信息,请参阅Wix HeatFile 任务锁定 Dll。HTH某人。
回答by Geomatik Mühendisi
Step 1: Restart your Visual Studio.
步骤 1:重新启动 Visual Studio。
Step 2: Build >> Reconfigure app.
第 2 步:构建 >> 重新配置应用程序。
I got it right this way.
我是这样理解的。
回答by IUN KHAN
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..
我在通过实体框架连接到 MDF 时遇到了同样的问题。通过简单地清洁溶液并重建它来解决它。希望能帮助到你..
回答by Jo?o Baganha
I had the same error. Look the Target framework. For mistake i've changed the netcore from 2.1 to 2.0
我有同样的错误。查看目标框架。由于错误,我已将 netcore 从 2.1 更改为 2.0
回答by martcs
My solution for this is open TaskManager/win or Monitor/mac And kill all visual studio and related processes.
我对此的解决方案是打开 TaskManager/win 或 Monitor/mac 并杀死所有 Visual Studio 和相关进程。
回答by Viree
My project build again after delete the 'obj' and 'bin' folder.
删除“obj”和“bin”文件夹后,我的项目再次构建。

