如何让 Visual Studio 的“发布"功能包含来自后期构建事件的文件? [英] How to get Visual Studio 'Publish' functionality to include files from post build event?

查看:39
本文介绍了如何让 Visual Studio 的“发布"功能包含来自后期构建事件的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently attempting to use Visual Studio 2010 'Publish' and MSDeploy functionality to handle my web deployment needs but have run into a roadblock with regards to customizing the package depending on my build configuration.

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

I develop in a 32bit environment but need to create a release package for a 64bit environment, so in the 'Release' configuration I have a post build event that copies the 64bit version of a third-party dll into the bin directory overwriting the 32bit version. When I use the 'Publish' functionality, even though the correct 64bit dll is being copied to the bin directory, it doesn't get included in the package.

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

Is there a way to get the 'Publish' to include files that have been copied into the bin directory during a post build event?

推荐答案

我在 如何使用 VS2010 Web 部署包包含其他文件?.

在您使用构建后事件的场景中,我建议您删除构建后事件并使用您自己的 MSBuild 目标而不是构建后事件来实施您的操作.您会在下面找到另一个答案的文本.

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.

来自:怎么做您使用 VS2010 Web 部署包包含其他文件吗?

好问题.我刚刚在 Web 部署工具 (MSDeploy) 上发布了一个非常详细的博客条目:构建包包括额外文件或排除特定文件.

这是概要.包含文件后,我还会展示如何排除文件.

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

包括额外文件

在包中包含额外的文件有点困难,但如果您对 MSBuild 感到满意,那么仍然没有什么大不了的,如果您不熟悉,请阅读本文.为了做到这一点,我们需要挂钩收集文件以进行打包的流程部分.我们需要扩展的目标称为 CopyAllFilesToSingleFolder.这个目标有一个依赖属性 PipelinePreDeployCopyAllFilesToOneFolderDependsOn,我们可以利用它并注入我们自己的目标.因此,我们将创建一个名为 CustomCollectFiles 的目标并将其注入到进程中.我们通过以下方式实现这一点(记住在 import 语句之后).

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>

这会将我们的目标添加到流程中,现在我们需要定义目标本身.假设您有一个名为 Extra Files 的文件夹,它位于您的 Web 项目上方 1 级.您希望包含所有这些文件.这是 CustomCollectFiles 目标,我们将在此之后讨论.

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>

在这里我所做的是创建项目 _CustomFiles 并在 Include 属性中告诉它选择该文件夹中的所有文件及其下的任何文件夹.然后我使用这个项目来填充 FilesForPackagingFromProject 项目.这是 MSDeploy 实际用来添加额外文件的项目.另请注意,我声明了元数据 DestinationRelativePath 值.这将确定它将放置在包中的相对路径.我在这里使用了 Extra Files%(RecursiveDir)%(Filename)%(Extension) 语句.也就是说,将它放在包中与 Extra Files 文件夹下相同的相对位置.

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.

排除文件

如果您打开使用 VS 2010 创建的 Web 应用程序的项目文件,您会在其底部找到一行.

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)MicrosoftVisualStudiov10.0WebApplicationsMicrosoft.WebApplication.targets" />

顺便说一句,您可以在 VS 中打开项目文件.右键单击项目选择卸载项目.然后右键单击卸载的项目并选择编辑项目".

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.

该声明将包括我们需要的所有目标和任务.我们的大部分定制都应该在导入之后,如果您不确定是否在之后!因此,如果您有要排除的文件,则可以使用一个项目名称 ExcludeFromPackageFiles.例如,假设您的 Web 应用程序中包含名为 Sample.Debug.js 的文件,但您希望从创建的包中排除该文件.您可以将下面的代码片段放在该导入语句之后.

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>

通过声明填充此项,文件将被自动排除.请注意此处 FromTarget 元数据的使用.我不会在这里讨论这个,但你应该知道总是指定.

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.

这篇关于如何让 Visual Studio 的“发布"功能包含来自后期构建事件的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆