15

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

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

<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?

Tomas
  • 17,551
  • 43
  • 152
  • 257

3 Answers3

13

Add this Condition to both the Import and the Zip elements:

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

Community
  • 1
  • 1
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • You may need to add quotes in order to get things working correctly, like so: `Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')"` – Alexander Mar 11 '14 at 23:36
7

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.

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

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 will install it into your 'Packages' folder under the root of your Solution/Project Trunk.

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\'.

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.

justdan23
  • 560
  • 7
  • 9
3

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 ZIP could be your build with the post-build-event to zip your files, and Release could be an ordinary build that doesn't do anything special using the community tasks.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
  • I have two IMPORTS in my MSBuild script. How to move them to specified configuration for example Release Zip? – Tomas Jul 24 '12 at 11:04
  • 1
    You can `Condition` them, such as the `Target` element already has, or according to Preet Sangha's answer. =) – J. Steen Jul 24 '12 at 11:08
  • I'm not sure this fixes the reason why MSBuild.Community.Tasks.Targets is 'not found' when he compiles the code. The MSBuild.Community.Tasks.Targets is a Community Extension to Visual Studio 2010 and has to be added via NuGet. Once you add it via NuGet, it should automatically download and install when the user attempts to compile. – justdan23 Jul 08 '15 at 22:45