How to get Visual Studio 'Publish' functionality to include files from post build event?

后端 未结 6 2053
北海茫月
北海茫月 2020-12-02 05:32

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 cu

相关标签:
6条回答
  • 2020-12-02 05:44

    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.

    0 讨论(0)
  • 2020-12-02 05:44

    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?

    0 讨论(0)
  • 2020-12-02 05:45

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

    0 讨论(0)
  • 2020-12-02 05:54

    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

    0 讨论(0)
  • 2020-12-02 05:56

    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:

    0 讨论(0)
  • 2020-12-02 06:00

    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

    0 讨论(0)
提交回复
热议问题