C# 未找到 MSBuild 社区任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11627304/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:33:37  来源:igfitidea点击:

MSBuild Community Tasks not found

c#visual-studio-2010msbuild

提问by Tomas

I have MyLiblibrary project along with several examples. The library and examples are in the same solution MySolution.

我有MyLib图书馆项目和几个例子。库和示例在同一个解决方案中MySolution

In MyLiblibrary project I have included MSBuild code to zip the whole solution and copy to another directory for internet publishing.

MyLib库项目中,我包含了 MSBuild 代码来压缩整个解决方案并复制到另一个目录以进行 Internet 发布。

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
  <Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'">
    <PropertyGroup>
      <ReleasePath>C:\Users\Administrator\Projects\CA\Libraries\Api-DotNet\</ReleasePath>
      <ZipFile>C:\Users\Administrator\Projects\CA\WebProject\libraries\Api-DotNet.zip</ZipFile>
    </PropertyGroup>
    <ItemGroup>
      <LibraryFiles Include="$(ReleasePath)\**\*.*" Exclude="$(ReleasePath)\**\*.user;$(ReleasePath)\**\*.suo;$(ReleasePath)\Api.*;$(ReleasePath)\**\packages\**;$(ReleasePath)\**\Lib.Test\**;$(ReleasePath)\**\*.nuspec;$(ReleasePath)\**\*.nupkg;$(ReleasePath)\**\*nuget*;$(ReleasePath)\**\*internal*;$(ReleasePath)\**\*ReSharper*\**;$(ReleasePath)\**\.svn\**;$(ReleasePath)\**\obj\**;$(ReleasePath)\lib\bin\Debug\**;$(ReleasePath)\lib\bin\Publish\**;$(ReleasePath)\Example\**\bin\**;" />
    </ItemGroup>
    <Zip Files="@(LibraryFiles)" WorkingDirectory="$(ReleasePath)" ZipFileName="$(ZipFile)" ZipLevel="9" />
  </Target>  
</Project>

The problem is that when user download library and run on another computer the compiler show error that import library not found MSBuild.Community.Tasks.Targets. I would like to exclude ZipAndCopy code from project file when building the solution. How to do that?

问题是当用户下载库并在另一台计算机上运行时,编译器显示未找到导入库的错误MSBuild.Community.Tasks.Targets。我想在构建解决方案时从项目文件中排除 ZipAndCopy 代码。怎么做?

采纳答案by J. Steen

Sounds like you want multiple build configurations. I would suggest setting up one specifically for building and zipping the artifacts and a separate for your users.

听起来您想要多个构建配置。我建议设置一个专门用于构建和压缩工件,并为您的用户单独设置。

Release ZIPcould be your build with the post-build-event to zip your files, and Releasecould be an ordinary build that doesn't do anything special using the community tasks.

Release ZIP可能是您的构建后构建事件来压缩您的文件,也Release可能是一个普通的构建,它不会使用社区任务做任何特殊的事情。

回答by Preet Sangha

Add this Conditionto both the Importand the Zipelements:

将此添加Condition到元素ImportZip元素中:

Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Commu??nity.Tasks.Targets')"

For example:

例如:

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"
        Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Commu??nity.Tasks.Targets')" />

Similar to this: C# Checking if a Property 'Starts/Ends With' in a csproj

与此类似:C# 检查 csproj 中的属性是否“开始/结束”

回答by justdan23

The above solution hides the project file load error, but Tomas appears to be trying to use a Task from the MSBuild.Community.Tasks extension.

上述解决方案隐藏了项目文件加载错误,但 Tomas 似乎试图使用 MSBuild.Community.Tasks 扩展中的任务。

This should be installable by using NuGet. Here's a linkto the source site showing we can install it via NuGet's Package Command Line:

这应该可以使用 NuGet 安装。这是源站点的链接,显示我们可以通过 NuGet 的包命令行安装它:

PM> Install-Package MSBuildTasks

Their documentation isn't great. You'll need to also define the path using:

他们的文档不是很好。您还需要使用以下方法定义路径:

<Import Project="..\Packages\MSBuildTasks.1.4.0.88\tools\MSBuild.Community.Tasks.Targets"/>
<UsingTask AssemblyFile="..\Packages\MSBuildTasks.1.4.0.88\tools\MSBuild.Community.Tasks.Targets.dll"
         TaskName="MSBuild.Community.Tasks.Zip" />

...where you need to replace the Version with the Version you are using from NuGet. It's not perfect, but I managed to get mine working.

...您需要将版本替换为您从 NuGet 使用的版本。这并不完美,但我设法让我的工作。

NuGet will install it into your 'Packages' folder under the root of your Solution/Project Trunk.

NuGet 会将其安装到解决方案/项目主干根目录下的“Packages”文件夹中。

I ran into issues where Visual Studio may still be fighting to look for the files in a specific location. If this happens, copy the files from '.\Packages\MSBuildTasks.1.4.0.88\tools*'to 'C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\'.

我遇到了 Visual Studio 可能仍在努力寻找特定位置中的文件的问题。如果发生这种情况,请将文件从'.\Packages\MSBuildTasks.1.4.0.88\tools*'复制到'C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\'

This isn't the most elegant, but I was able to successfully get the new Tags to work. If I find a way to fix this last part, I'll update my posting.

这不是最优雅的,但我能够成功地让新标签工作。如果我找到解决这最后一部分的方法,我会更新我的帖子。