How do you include additional files using VS2010 web deployment packages?

Visual Studio-2010Visual StudioWeb DeploymentMsdeploy

Visual Studio-2010 Problem Overview


I am testing out using the new web packaging functionality in visual studio 2010 and came across a situation where I use a pre-build event to copy required .dll's into my bin folder that my app relies on for API calls. They cannot be included as a reference since they are not COM dlls that can be used with interop.

When I build my deployment package those files are excluded when I choose the option to only include the files required to run the app. Is there a way to configure the deployment settings to include these files? I have had no luck finding any good documentation on this.

Visual Studio-2010 Solutions


Solution 1 - Visual Studio-2010

Great question. I just posted a very detailed blog entry about this at [Web Deployment Tool (MSDeploy) : Build Package including extra files or excluding specific files][1].

Here is the synopsis. After including files, I show how to exclude files as well.

Including Extra Files

Including extra files into the package is a bit harder but still no bigee if you are comfortable with MSBuild, and if you are not then read this. In order to do this we need to hook into the part of the process that collects the files for packaging. The target we need to extend is called CopyAllFilesToSingleFolder. This target has a dependency property, PipelinePreDeployCopyAllFilesToOneFolderDependsOn, that we can tap into and inject our own target. So we will create a target named CustomCollectFiles and inject that into the process. We achieve this with the following (remember after the import statement).

<PropertyGroup>
  <CopyAllFilesToSingleFolderForPackageDependsOn>
    CustomCollectFiles;
    $(CopyAllFilesToSingleFolderForPackageDependsOn);
  </CopyAllFilesToSingleFolderForPackageDependsOn>

  <CopyAllFilesToSingleFolderForMsdeployDependsOn>
    CustomCollectFiles;
    $(CopyAllFilesToSingleFolderForMsdeployDependsOn);
  </CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>

This will add our target to the process, now we need to define the target itself. Let’s assume that you have a folder named Extra Files that sits 1 level above your web project. You want to include all of those files. Here is the CustomCollectFiles target and we discuss after that.

<Target Name="CustomCollectFiles">
  <ItemGroup>
    <_CustomFiles Include="..\Extra Files\**\*" />

    <FilesForPackagingFromProject  Include="%(_CustomFiles.Identity)">
      <DestinationRelativePath>Extra Files\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

Here what I did was create the item _CustomFiles and in the Include attribute told it to pick up all the files in that folder and any folder underneath it. If by any chance you need to exclude something from that list, add an Exclude attribute to _CustomFiles.

Then I use this item to populate the FilesForPackagingFromProject item. This is the item that MSDeploy actually uses to add extra files. Also notice that I declared the metadata DestinationRelativePath value. This will determine the relative path that it will be placed in the package. I used the statement Extra Files%(RecursiveDir)%(Filename)%(Extension) here. What that is saying is to place it in the same relative location in the package as it is under the Extra Files folder.

Excluding files

If you open the project file of a web application created with VS 2010 towards the bottom of it you will find a line with.

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />

BTW you can open the project file inside of VS. Right click on the project pick Unload Project. Then right click on the unloaded project and select Edit Project.

This statement will include all the targets and tasks that we need. Most of our customizations should be after that import, if you are not sure put if after! So if you have files to exclude there is an item name, ExcludeFromPackageFiles, that can be used to do so. For example let’s say that you have file named Sample.Debug.xml which included in your web application but you want that file to be excluded from the created packages. You can place the snippet below after that import statement.

<ItemGroup>
  <ExcludeFromPackageFiles Include="Sample.Debug.xml">
    <FromTarget>Project</FromTarget>
  </ExcludeFromPackageFiles>
</ItemGroup>

By declaring populating this item, the files will automatically be excluded. Note the usage of the FromTarget metadata here. I will not get into that here, but you should know to always specify that. [1]: http://sedodream.com/2010/05/01/WebDeploymentToolMSDeployBuildPackageIncludingExtraFilesOrExcludingSpecificFiles.aspx

Solution 2 - Visual Studio-2010

A simpler solution is to edit the csproj file to include the required dll in the bin folder and then create a beforebuild target to copy the item into the bin folder from the common library folder where we store our 3rd party dlls. Because the item exists in the solution file it is deployed by msbuild/msdeploy and nothing complicated is needed.

Tag used to include file without adding through VS (which will want to add it to your VCS usually)

<Content Include="Bin\3rdPartyNative.dll" ><Visible>false</Visible></Content>

This is the BeforeBuild target that worked for me:

<Target Name="BeforeBuild">
	<Message Text="Copy $(SolutionDir)Library\3rdPartyNative.dll to '$(TargetDir)'3rdPartyNative.dll" Importance="high"	/>
	<Copy SourceFiles="$(SolutionDir)Library\3rdPartyNative.dll" DestinationFiles="$(TargetDir)3rdPartyNative.dll" />
</Target>

Edited to include @tuespetre's suggestion to hide the entry thus removing the previous downside of a visible bin folder. Unverified by me.

Solution 3 - Visual Studio-2010

So Sayed's implementation did not work for me. I'm using VS2013 and using the Web Deploy package and needed to add some plugin DLLs from another folder into the bin of the deploy package. Here's how I managed to make it work (much easier):

At the bottom of your csproj file add:

<Target Name="AdditionalFilesForPackage" AfterTargets="CopyAllFilesToSingleFolderForMsdeploy">
    <ItemGroup>	
	    <Files Include="..\SomeOtherProject\bin\$(Configuration)\*.*"/>
    </ItemGroup>
    <Copy SourceFiles="@(Files)" DestinationFolder="$(_PackageTempDir)\bin\" />  
</Target>

Other mentionables in the csproj file:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <DeployOnBuild>true</DeployOnBuild>
    <DeployTarget>Package</DeployTarget>
    <DeployIisAppPath>Default Web Site/MyWebsite</DeployIisAppPath>
    <DesktopBuildPackageLocation>..\output\Service\Service\Service.Release.zip</DesktopBuildPackageLocation>
    <FilesToIncludeForPublish>OnlyFilesToRunTheApp</FilesToIncludeForPublish>
    <ExcludeGeneratedDebugSymbol>true</ExcludeGeneratedDebugSymbol>
    <PublishDatabases>false</PublishDatabases>
</PropertyGroup>

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\WebApplications\Microsoft.WebApplication.targets" />

Solution 4 - Visual Studio-2010

Just like @toxaq, but an even simpler solution is to:

In the solution explorer add the file as a link to the library/references folder, and then in the properties set it to be copied to output of the build.

Solution 5 - Visual Studio-2010

Wanted to comment to emphasize Emil Lerch's comment above. If you have installed an Azure SDK, then look for a different DependencyProperty.

Basically, you may need to use "CopyAllFilesToSingleFolderForMsdeployDependsOn instead of "CopyAllFilesToSingleFolderForPackageDependsOn". I'm not really an advanced MsBuild guy and I wasted hours pulling my hair out trying to determine why my targets were not getting called.

Here is another link if this does not work for you and you have installed an Azure SDK: http://forums.iis.net/t/1190714.aspx">http://forums.iis.net/t/1190714.aspx</a>

Solution 6 - Visual Studio-2010

As an addendum to Sayed's answer, I found that a static declaration of ExcludeFromPackageFiles items within my project was not enough. I needed to exclude certain DLLs that were only available after compile (Azure specific Ninject modules that are not needed when I deploy to IIS).

So I tried to hook in generating my ExcludeFromPackageFiles list using the CopyAllFilesToSingleFolderForPackageDependsOn trick Sayed posted above. However, this is too late as the packaging process has already removed the ExcludeFromPackageFiles items. So, I used the same technique, but a little earlier:

<PropertyGroup>
	<ExcludeFilesFromPackageDependsOn>
		$(ExcludeFilesFromPackageDependsOn);
		_ExcludeAzureDlls
	</ExcludeFilesFromPackageDependsOn>
</PropertyGroup>

<Target Name="_ExcludeAzureDlls">
	<ItemGroup>
		<FilesForPackagingFromProjectWithNoAzure Include="@(FilesForPackagingFromProject)"
							   Exclude="%(RootDir)%(Directory)*Azure*.dll" />
		<AzureFiles Include="@(FilesForPackagingFromProject)"
					Exclude="@(FilesForPackagingFromProjectWithNoAzure)" />
		<ExcludeFromPackageFiles Include="@(AzureFiles)">
			<FromTarget>_ExcludeAzureEnvironmentDlls</FromTarget>
		</ExcludeFromPackageFiles>
	</ItemGroup>
</Target>

Hope that helps someone...

Solution 7 - Visual Studio-2010

Also, it is possible to set the file as Content | Copy always

Also, it is possible to set the file as Content | Copy always

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJasonView Question on Stackoverflow
Solution 1 - Visual Studio-2010Sayed Ibrahim HashimiView Answer on Stackoverflow
Solution 2 - Visual Studio-2010toxaqView Answer on Stackoverflow
Solution 3 - Visual Studio-2010K0D4View Answer on Stackoverflow
Solution 4 - Visual Studio-2010eglasiusView Answer on Stackoverflow
Solution 5 - Visual Studio-2010Paul SchroederView Answer on Stackoverflow
Solution 6 - Visual Studio-2010Peter McEvoyView Answer on Stackoverflow
Solution 7 - Visual Studio-2010FabitoView Answer on Stackoverflow