Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude files from web site publish in Visual Studio

Can I exclude a folder or files when I publish a web site in Visual Studio 2005? I have various resources that I want to keep at hand in the Solution Explorer, such as alternate config files for various environments, but I don't really want to publish them to the server. Is there some way to exclude them? When using other project types, such as a .dll assembly, I can set a file's Build Action property to "None" and its Copy to Output Directory property to "Do not copy". I cannot find any similar settings for files in a web site.

If the IDE does not offer this feature, does anyone have good technique for handling such files?

like image 672
Jim Snyder Avatar asked Mar 16 '09 15:03

Jim Snyder


People also ask

How do I get excluded files in Visual Studio?

In Visual Studio, right-click on the file to be excluded from build, choose Properties, Configuration Properties -> General -> Excluded From Build -> Yes -> OK. In Eclipse, right-click on the file to be excluded from build, choose Properties, C/C++ Build -> Excluded from build -> OK.

What does exclude from project do in Visual Studio?

Removing it temporarily from the active solution configuration. Creating a solution configuration that doesn't include the project.

How do I show excluded folders in Visual Studio?

Open the project in Visual Studio Team Edition for Database Professionals. Open the Project menu and click Show All Files. This will display excluded files in Solution Explorer. In Solution Explorer, click the folder that you had previously excluded.


2 Answers

Exclude files and folders by adding ExcludeFilesFromDeployment and ExcludeFoldersFromDeployment elements to your project file (.csproj, .vbproj, etc). You will need to edit the file in a text editor, or in Visual Studio by unloading the project and then editing it.

Add the tags anywhere within the appropriate PropertyGroup (Debug, Release, etc) as shown below:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">    ...    <ExcludeFilesFromDeployment>File1.aspx;Folder2\File2.aspx</ExcludeFilesFromDeployment>    <ExcludeFilesFromDeployment>**\.svn\**\*.*</ExcludeFilesFromDeployment>   <ExcludeFoldersFromDeployment>Folder1;Folder2\Folder2a</ExcludeFoldersFromDeployment>  </PropertyGroup> 

Wildcards are supported.

To explain the example above:

  • The 1st ExcludeFilesFromDeployment excludes File1.aspx (in root of project) and Folder2\File2.aspx (Folder2 is in the root of the project)
  • The 2nd ExcludeFilesFromDeployment excludes all files within any folder named .svn and any of its subfolders
  • The ExcludeFoldersFromDeployment excludes folders named Folder1 (in root of project) and Folder2\Folder2a (Folder2 is in the root of the project)

For more info see MSDN blog post Web Deployment: Excluding Files and Folders via the Web Application’s Project File

like image 153
Keith Avatar answered Sep 17 '22 14:09

Keith


Amazingly the answer for Visual Studio 2012 is not here:

  • The answer with green checkmark is not the answer.

  • The highest "upped" answer references an article from 2010 and says you have to edit your csproj project file which is now incorrect. I added the ExcludeFoldersFromDeployment XML element to my Visual Studio 2012 csproj file and it did nothing, the element was considered invalid, this is because ExcludeFoldersFromDeployment has been moved to the .pubxml file it looks like.

For Web Applications and Websites you edit the .pubxml file!

You can follow my answer or try this guide which I found later: http://www.leniel.net/2014/05/using-msdeploy-publish-profile-pubxml-to-create-an-empty-folder-structure-on-iis-and-skip-deleting-it-with-msdeployskiprules.html#sthash.MSsQD8U1.dpbs

Yes, you can do this not just for Website Projects but Websites too. I spent a long time on the internet looking for this elusive exclude ability with a Visual Studio Website (NOT Website project) and had previously concluded it was not possible but it looks like it is:

In your [mypublishwebsitename].pubxml file, found in ~/Properties/PublishProfiles for Web Application Projects and ~/App_Data/PublishProfiles for Websites, simply add:

  <ExcludeFilesFromDeployment>File1.aspx;Folder2\File2.aspx</ExcludeFilesFromDeployment>   <ExcludeFoldersFromDeployment>Folder1;Folder2\Folder2a</ExcludeFoldersFromDeployment> 

as children to the main <PropertyGroup> element in your .pubxml file. No need to add a new element not unless you are keying a specific build type, like release or debug.

BUT WAIT!!!

If you are removing files from your destination/target server with the following setting in your Publish configuration:

enter image description here

Then the Web Publish process will delete on your source/target server anything excluded, like an item you have delineated in your <ExcludeFoldersFromDeployment> and <ExcludeFilesFromDeployment>!

MsDeploy Skip Rules to the rescue:

First, Web Publish uses something other than MSBuild to publish (called Task IO or something like that) but it has a bug and will not recognize skip rules, so you must add to your .pubxml:

<PropertyGroup>     <WebPublishMethod>MSDeploy</WebPublishMethod> </PropertyGroup> 

I would keep <WebPublishMethod> in its own <PropertyGroup>, you would think you could just have one <PropertyGroup> element in your .pubxml but my Skip Rules were not being called until I moved <WebPublishMethod> to its own <PropertyGroup> element. Yes, crazy, but the fact you need to do all this for Web Publish to exclude and also not delete a folder/file on your server is crazy.

Now my actual SkipRules, ExcludeFolders and ExcludeFiles declarations in my .pubxml:

<ExcludeFoldersFromDeployment>Config</ExcludeFoldersFromDeployment> <ExcludeFoldersFromDeployment>Photos</ExcludeFoldersFromDeployment> <ExcludeFoldersFromDeployment>Temp</ExcludeFoldersFromDeployment> <ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment>  <AfterAddIisSettingAndFileContentsToSourceManifest>AddCustomSkipRules</AfterAddIisSettingAndFileContentsToSourceManifest> 

And now a the Skip Rules (<Target> is a child of <Project> in your .pubxml): (You may be able to leave <SkipAction> empty to Skip for all actions but I didn't test that and am not sure.

  <Target Name="AddCustomSkipRules">     <Message Text="Adding Custom Skip Rules" />     <ItemGroup>       <MsDeploySkipRules Include="SkipConfigFolder">         <SkipAction>Delete</SkipAction>         <ObjectName>dirPath</ObjectName>         <AbsolutePath>$(_DestinationContentPath)\\Config</AbsolutePath>         <XPath>         </XPath>       </MsDeploySkipRules>       <MsDeploySkipRules Include="SkipPhotosFolder">         <SkipAction>Delete</SkipAction>         <ObjectName>dirPath</ObjectName>         <AbsolutePath>$(_DestinationContentPath)\\Photos</AbsolutePath>         <XPath>         </XPath>       </MsDeploySkipRules>       <MsDeploySkipRules Include="SkipWebConfig">         <SkipAction>Delete</SkipAction>         <ObjectName>filePath</ObjectName>         <AbsolutePath>$(_DestinationContentPath)\\Web\.config</AbsolutePath>         <XPath>         </XPath>       </MsDeploySkipRules>       <MsDeploySkipRules Include="SkipWebConfig">         <SkipAction>Delete</SkipAction>         <ObjectName>dirPath</ObjectName>         <AbsolutePath>$(_DestinationContentPath)\\Temp</AbsolutePath>         <XPath>         </XPath>       </MsDeploySkipRules>     </ItemGroup>   </Target> 

And please, do not to forget to escape the . in a filePath Skip rule with a backslash.

like image 30
Brian Ogden Avatar answered Sep 19 '22 14:09

Brian Ogden