When publishing an ASP. NET site and Windows Service project, there are times when you need to exclude specified files when you publish to a server-specified directory after MSBuild compiles, such as when you build with Jenkins and don't want to overwrite the original web. config and App. Config.
Web site project
For example, you do not want to publish the Web. config file in the following three ways.
1, change the item content to none
Before modification:
1 <content include="Web. config" />
After modification:
1 <none include="Web. config" />
2, using the Exclude property of the item
The following code:
1 <content include="Web. config " exclude="Web. config" />
Attention:
Although this can be achieved by the release of the exclusion, but also excluded from the project, for local development debugging inconvenient.
3, using Target and Excludefrompackagefiles
Add a new target node with the following code:
1 <!--exclude web.config-->2 <target name=" by Excludefrompackagefiles Customexcludefiles "beforetargets="excludefilesfrompackage">3 <ItemGroup>4 <excludefrompackagefiles include="web.config "></ExcludeFromPackageFiles>5 </ItemGroup>6 </Target>
View Code
Summary: The best practice is to use Method 3, both to achieve the release of the exclusion, but also to ensure that the file exists in the project, does not affect the local development and debugging.
Other than that:
Because MSBuild is based on the. csproj project file, the above actions are to modify the. csproj file and modify the method as follows:
1, uninstalling the project
Such as:
2, edit csproj file
Such as:
Windows Service Project
Because app. config is the none type in the Windows Service project, and the use of target and excludefrompackagefiles is not applicable, the Exclude property of the item is used for the time being.
Before modification:
1 <none include="app. config" />
After modification:
1 <none include="app. config" exclude="app. config" />
Resources
1,https://stackoverflow.com/questions/6358635/exclude-files-from-web-site-deployment-with-msbuild
2,msbuild Reference: Https://docs.microsoft.com/zh-cn/visualstudio/msbuild/msbuild
How to exclude specified files when Web sites and Windows service projects are published