How to exclude specified files and web Service projects when a web site and windows Service Project are released
When publishing the asp.net site and windows service projects, you may need to exclude the specified files when the msbuild is compiled and published to the specified directory on the server. For example, during jenkins build, do not want to overwrite the original Web. config and App. config, so how to exclude it?
Web Site Project
For example, if you do not want to publish the Web. config file, you can use the following three methods.
1. Change the item Content to None.
Before modification:
1 <Content Include="Web.config" />
After modification:
1 <None Include="Web.config" />
2. Use the Exclude attribute of the item
The following code:
1 <Content Include="Web.config" Exclude="Web.config" />
Note:
Although this method can be excluded during release, it is also excluded from the project, which is inconvenient for local development and debugging.
3. Use Target and ExcludeFromPackageFiles
Add a new Target node with the following code:
1 <! -- Exclude Web by ExcludeFromPackageFiles. config --> 2 <Target Name = "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, which can be used for release exclusion, and ensure that files exist in the project without affecting local development and debugging.
In addition:
Because msbuild is based on the. csproj project file, the above operations are to modify the. csproj file. The modification method is as follows:
1. Uninstall the project
For example:
2. Edit the csproj File
For example:
Windows Service Project
In windows service projects, App. config is of the None type, and the method of using Target and ExcludeFromPackageFiles is not applicable. Therefore, the Exclude attribute 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" />
References
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