Exclude files and folders from Web Deployment projects

Getting a lot of garbage when compiling web deployment projects? Don’t worry, there’s an easy remedy!

This is how to do it (using Visual Studio):

  1. Check out the web deployment project.
  2. Right-click the deployment project in the Solution Explorer and select “Open Project File”. The project file will be opened in the built-in Xml editor.
  3. Insert Xml code similar to the following last in the file, just before the end tag for the Project element:
    <project>
      ...
      <ItemGroup>
        <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\obj\**\*.*"/>
        <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\Properties\**\*.*"/>
        <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.csproj*"/>
        <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.resx"/>
        <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.Publish.xml"/>
      </ItemGroup>
    </project>
    
  4. After this change, all files matching the wildcard expressions given in ExcludeFromBuild elements will be excluded. Problem fixed!

Good luck deploying!

Update: The double asterisks means that the path matching is done recursively in the file system. For example, to exclude everything from the “obj\” folder we have to use “obj\**\*.*” since “obj\*.*” only matches files placed directly in the folder. Files in sub-folders would not be excluded if we used that pattern.

/Emil

7 thoughts on “Exclude files and folders from Web Deployment projects”

  1. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? – Thanks

  2. I’m sorry that I seem to be spamming you, but I can’t actually find a way of removing you from the notification list. As a matter of fact, I can’t even find such a list eventhough it obviously exists…

    If you want, you could mail me one of the notifications you get, and I’ll see if that makes it easier to Google a solution.

    Mail me at emil_astrom(at)hotmail.com.

    /Emil

  3. This did not seem to work for me. For a start I think you need capitalisations in there )(ItemGroup and ExcludeFromBuild). But even then in my case this seem to override some of the settings in the underlying build script – it cased whole project directory (source and everything) to be copied to the Release output folder!

  4. Hi!

    You’re right of course about the bad casing, must have been broken by my blog engine or by me at some point… The post is now corrected.

    It sounds strange about the whole project folder being copied. My guess is that there’s some problem with your paths. If you want, you could post them here and I’ll see if I can spot an error. I don’t work with these things very often though, so don’t get your hopes up too high 🙂

    /Emil

  5. Thanks for the article, it worked great for me. I finally was able to compile the project.
    Sad to see after so many years of programming and I still see things that could be added to the UI instead of “guessing” or knowing where to go or editing cryptic XML files with undocumented commands.
    Thanks again

  6. Hi,
    I tried writing the following code in my .proj file.

    ……………
    ……………

    but in my build drop directory I am still getting web.config and web.debug.config and web.release.config files.

    Can you please let me know where am I going wrong?

    Gautam

  7. Gautam :
    Hi,
    I tried writing the following code in my .proj file.
    ……………
    ……………

    but in my build drop directory I am still getting web.config and web.debug.config and web.release.config files.
    Can you please let me know where am I going wrong?
    Gautam

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.