When I publish my solution that contains an ASP.NET Web Application for the first time the .wpp.targets file is ignored. The publish folder contains also the files that shouldn't be published.
After removing all the files in my publish folder and publish again everything is fine. But when I do a clean solution from visual studio the .wpp.targets file is ignored again.
Hopefully someone can solve this problem or explain why this happens. I would like to use it on our build server where the build always starts with a clean solution.
What I have used:
MSBuild version 15.8.166 "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\msbuild.exe" WebApplication1.sln /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:publishUrl=C:\test
MSBuild version 16.0.360 "C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\msbuild.exe" WebApplication1.sln /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:publishUrl=C:\test
I ran the builds with a wpp.target file which contained the name of my project, but I tried it also with the following MSBuild parameter: /p:WebPublishPipelineCustomizeTargetFile=C:\Users\username\source\repos\Test\WebApplication1\test.wpp.targets
Update
When building a project for the first time or after the build is cleaned the wildcards in the *.wpp.targets file dont work. Here is an example of my *.wwp.targets file:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ExcludeFromPackageFolders Include="bin\roslyn">
<FromTarget>Roslyn</FromTarget>
</ExcludeFromPackageFolders>
<ExcludeFromPackageFiles Include="bin\Newtonsoft.*">
<FromTarget>Newtonsoft</FromTarget>
</ExcludeFromPackageFiles>
</ItemGroup>
</Project>
Roslyn is excluded as expected. But Newtonsoft.* are not excluded, because there is a wildcard in the name.
When the obj folder is manual deleted or the build is executed before than the wildcards work as well.
What I have used: MSBuild version 15.8.166 & MSBuild version 16.0.360
Example of an used msbuild comando: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\msbuild.exe" WebApplicationTestForMsBuild.sln /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:publishUrl=C:\test
In other projects we have noticed that some wildcard seem to work for the most files but not for all of them. An example of this is: "<ExcludeFromPackageFiles Include="bin\*.pdb">" the most pdb files or exclude but not all of them.
I would like to use it on our build server where the build always starts with a clean solution. This problem seems to be a bug in msbuild, but if you know a solution please share it with me.
Update 2
The steps of one of the tests that I performed:
- Create an new Empty ASP.Net Web Application (.NET Framework 4.6.1) project in Visual studio Enterprise 2017 (version 15.9.7).
- Add the [ProjectName] .wpp.targets to the project which contains the earlier described data.
- Install the nuget package Newtonsoft.Json (version 12.0.1) (Used as an example)
- I run the following command at the command prompt: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\msbuild.exe" [projectName].csproj /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:publishUrl=C:\test /p:DeleteExistingFiles=True
- I looked into the C:\test\bin folder and I found the following files:
- Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll
- Newtonsoft.Json.dll
- Newtonsoft.Json.pdb
- [projectName].dll
- [projectName].pdb But the Newtonsoft.Json.dll and the Newtonsoft.Json.pdb files should not be published.
- I run the msbuild command again and it works as expected. I only got the following files in the publish folder:
- Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll
- [projectName].dll
- [projectName].pdb
- I did a clean solution in my visual studio instance or by the msbuild command: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\msbuild.exe" WebApplication7wpp.csproj -target:Clean
- Run the msbuild build command again and the Newtonsoft files are added again.
The wpp.targets works as expected when I don't use the wildcards.
For a clean solution, open the xxx.csproj file and check if there exists
<Content Include="test.wpp.targets" />in<ItemGroup>tag. If not, manually add this sentence into the tag.Every time before we publish it by msbuild command, we should make sure we have reference to wpp.targets file so that it can be found and deployed to publish folder.
In addition:
1.We can add a /p:DeleteExistingFiles option. The command below works well at my side.
2.Make sure the wpp.targets is in the Project folder instead of Solution folder Hope it helps. Any update please share here.
Update 1
Since I've reproduced this issue and have some tests with it, now I update some new discoveries.
Log for first publish in TestMe folder:
Log for second publish in new folder:
As what shows above.The first publish copy the files we exclude to obj, and second publish has a deleting action which is the reason why it works well for the second time as you mentioned above in Update2. So far, the reason why the exclude doesn't work is unknown.It can result from the call order in running time or something about msbuild itself.
I know you want a clean publish.
So, before we use the publish command, we can use a simple build command or rebuild command locally.It works at my side, and since it is a build locally which won't affect publish process before we publish it, you can get a clean and successful publish you want. Hope it works.