Create a web deployment package using the MSBuild command line, Team Build, or Visual Studio 2010. Copy the web package to the destination web server. Use the Import Application Package Wizard in IIS Manager to install the web package and provide values for variables like connection strings and service endpoints.
To deploy your web application, select your web application from Solution Explorer and right-click on your web application and click on the Publish button. At the next screen, click on Web Server (IIS) and click Next button. Select Web Deploy to publish your code and click Next.
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.
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.
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.
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.
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" />
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
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With