visual-studio 如何使用 VS2010 Web 部署包包含其他文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2747081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:54:16  来源:igfitidea点击:

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

visual-studio-2010visual-studioweb-deploymentmsdeploy

提问by Jason

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.

我正在 Visual Studio 2010 中使用新的 Web 打包功能进行测试,并遇到了这样一种情况:我使用预构建事件将所需的 .dll 复制到我的应用程序依赖于 API 调用的 bin 文件夹中。它们不能作为参考包含在内,因为它们不是可用于互操作的 COM dll。

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.

当我构建部署包时,当我选择仅包含运行应用程序所需的文件的选项时,这些文件将被排除。有没有办法配置部署设置以包含这些文件?我没有找到任何关于此的好的文档。

回答by Sayed Ibrahim Hashimi

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.

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

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).

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

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

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

<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 excludesomething from that list, add an Excludeattribute to _CustomFiles.

在这里,我所做的是创建项目 _CustomFiles 并在 Include 属性中告诉它选择该文件夹中的所有文件及其下的任何文件夹。如果有任何机会您需要从该列表中排除某些内容,请将Exclude属性添加到_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.

然后我使用这个项目来填充 FilesForPackagingFromProject 项目。这是 MSDeploy 实际用来添加额外文件的项目。另请注意,我声明了元数据 DestinationRelativePath 值。这将确定它将放置在包中的相对路径。我在这里使用了 Extra Files%(RecursiveDir)%(Filename)%(Extension) 语句。这就是说将它放在包中与 Extra Files 文件夹下相同的相对位置。

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.

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

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

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

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.

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

<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 FromTargetmetadata here. I will not get into that here, but you should know to always specify that.

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

回答by toxaq

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.

一个更简单的解决方案是编辑 csproj 文件以将所需的 dll 包含在 bin 文件夹中,然后创建一个 beforebuild 目标,将项目从我们存储 3rd 方 dll 的公共库文件夹复制到 bin 文件夹中。因为该项目存在于解决方案文件中,所以它是由 msbuild/msdeploy 部署的,不需要任何复杂的东西。

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

标签用于在不通过 VS 添加的情况下包含文件(通常希望将其添加到您的 VCS)

<Content Include="BinrdPartyNative.dll" ><Visible>false</Visible></Content>

This is the BeforeBuild target that worked for me:

这是对我有用的 BeforeBuild 目标:

<Target Name="BeforeBuild">
    <Message Text="Copy $(SolutionDir)LibraryrdPartyNative.dll to '$(TargetDir)'3rdPartyNative.dll" Importance="high" />
    <Copy SourceFiles="$(SolutionDir)LibraryrdPartyNative.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.

编辑以包含@tuespetre 的隐藏条目的建议,从而消除了可见 bin 文件夹的先前缺点。未经本人核实。

回答by eglasius

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

就像@toxaq 一样,但更简单的解决方案是:

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.

在解决方案资源管理器中,将该文件添加为 library/references 文件夹的链接,然后在属性设置中将其复制到构建的输出。

回答by K0D4

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):

所以赛义德的实施对我不起作用。我正在使用 VS2013 并使用 Web 部署包,需要将另一个文件夹中的一些插件 DLL 添加到部署包的 bin 中。这是我设法让它工作的方法(更容易):

At the bottom of your csproj file add:

在您的 csproj 文件底部添加:

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

csproj 文件中的其他可提及内容:

<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" />

回答by Paul Schroeder

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

想发表评论以强调上面 Emil Lerch 的评论。如果已安装 Azure SDK,则查找不同的 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.

基本上,您可能需要使用“CopyAllFilesToSingleFolderForMsdeployDependsOn 而不是“CopyAllFilesToSingleFolderForPackageDependsOn”。我不是一个真正的高级 MsBuild 人,我浪费了几个小时试图确定为什么我的目标没有被调用。

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

如果这对您不起作用并且您已经安装了 Azure SDK,这里是另一个链接: http://forums.iis.net/t/1190714.aspx

回答by Peter McEvoy

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 aftercompile (Azure specific Ninject modules that are not needed when I deploy to IIS).

作为赛义德回答的附录,我发现我的项目中 ExcludeFromPackageFiles 项目的静态声明是不够的。我需要排除某些仅编译可用的 DLL (部署到 IIS 时不需要的特定Azure 的 Ninject 模块)。

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:

所以我尝试使用上面发布的 CopyAllFilesToSingleFolderForPackageDependsOn 技巧 Sayed 来生成我的 ExcludeFromPackageFiles 列表。但是,这为时已晚,因为打包过程已经删除了 ExcludeFromPackageFiles 项。所以,我使用了相同的技术,但更早一点:

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

希望能帮助某人...

回答by Fabito

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

此外,可以将文件设置为 Content | 始终复制

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

此外,可以将文件设置为 Content |  始终复制