如何让 VisualStudio“发布”功能包含后期生成事件中的文件?

我目前正在尝试使用 Visual Studio 2010“发布”和 MSDeploy 功能来处理我的 Web 部署需求,但是在根据我的构建配置定制包方面遇到了障碍。

我在32位环境中开发,但是需要为64位环境创建一个发布包,所以在“发布”配置中,我有一个 post build 事件,它将第三方 dll 的64位版本复制到 bin 目录中,覆盖了32位版本。当我使用“发布”功能时,即使正确的64位 dll 被复制到 bin 目录,它也不会包含在包中。

有没有办法让“发布”包含在后期构建事件中复制到 bin 目录中的文件?

92327 次浏览

I found a workaround for the problem by using the ExcludeFilesFromDeployment element within the project file. I got the idea from Web Deployment: Excluding Files and Folders

So if you need to package project files as they exist in your project directory after a successful build and associated post build steps then do the following.

  1. Edit "Package/Publish Web" project settings and
    select Items to deploy to be "All files in this project folder"

  2. Unload the project

  3. Right click on the unloaded project and select to edit the project config

  4. Locate the PropertyGroup element associated to the configuration setting e.g. "Release"

  5. Within the PropertyGroup element add in the following elements and exclude files and folders you don't want in the package

    <ExcludeFilesFromDeployment>*.cs;**\.svn\**\*.*;Web.*.config;*.csproj*</ExcludeFilesFromDeployment>
    <ExcludeFoldersFromDeployment>.svn;Controllers;BootstrapperTasks;Properties</ExcludeFoldersFromDeployment>
    
  6. Save and reload your project

This solves my problem for the time being but if there is a better solution then please let me know, as this is not ideal due to the hackery involved, but then again perhaps this is an uncommon deployment scenario?

I answered a similar but different question at How do you include additional files using VS2010 web deployment packages?.

In your scenario you are using post build event, I would recommend dropping the post build event and implement your actions using your own MSBuild targets instead of post build event. Below you'll find the text of the other answer.


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


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>
</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. 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.js 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.

Select your files or folders and Change Build action as Content from Properties Window.

Adding the bin folder (and it's contents) to the project caused the files to be copied to the publish output directory.

For me, my issue was that I needed to place a proprietary software license file in the bin/ folder, but did not want to copy it manually each deployment.

This was using Visual Studio 2015 Professional

I know its a old question but none of these worked for me .

In 2017 VS I just right clicked on the extra folder to be published and select publish it worked.

Example:

enter image description here

I know this is an old conversation but I came upon it while trying to do the same thing and I thought it would be helpful to add what I found here.

Nearly all the articles about including extra files in your publication use this method of adding the CopyAllFilesToSingleFolderForPackageDependsOn or CopyAllFilesToSingleFolderForMSDeployDependsOn items in the PropertyGroup and they all same something like "I added this to the end of the file ..."

This is what I did and spent an afternoon trying to find why nothing was happening until I realised there was already a PropertyGroup section at the top of the file. When I put my CopyAllFilesToSingleFolderForPackageDependsOn into that section it worked fine.

Hope this saves someone time some day