2
votes

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:

  1. Create an new Empty ASP.Net Web Application (.NET Framework 4.6.1) project in Visual studio Enterprise 2017 (version 15.9.7).
  2. Add the [ProjectName] .wpp.targets to the project which contains the earlier described data.
  3. Install the nuget package Newtonsoft.Json (version 12.0.1) (Used as an example)
  4. 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
  5. 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.
  6. 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
  7. 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
  8. 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.

2
Whether it works or not? please give me a feedback. And it could be better if you could share a simple sample by one-drive so that i can check it directly for you.LoLance
Something strange. But I use the wpp.targets example at my side with MSBuild version15.9.6 and it works well. Whether the Newtonsoft.* or *.pdb, they all work well to exclude certain files. (My project is ASP.net web application , 4.6.1 framework, has same folder structure like your)LoLance
Please update your vs or msbuild to latest version. And two points to make sure: 1.The [ProjectName].wpp.targets must be at ProjectDir instead of SolutionDir 2.Before publish, delete the content of publish folder manually or change the "DeleteExistingFiles" property in .pubxml to be 'True' ,which removes effects from previous publishmentLoLance
I tried it on my home computer today. First with msbuild 15.8.168 and then with the updated version 15.9.21. The results were the same again. Before I want to explain step by step what I have done, I first want to answer the two points that are asked by @LanceLi-MSFT. 1. Yes I also tested it with the wpp.target file in my project and not in the solution (although I got the same result) 2. I have manually cleared my publish folder, but I have also used the DeleteExistingFiles property. I added Update 2 to my Question which describes the steps I took. Could you try those steps as well?elzen
I followed your steps and encounter similar situation.Now I think i can completely understand what you mean. I will add then to update my answer.LoLance

2 Answers

1
votes

It seems to result from the we lack reference to wpp.targets file.

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.

MSBuild.exe xxx.csproj /p:DeployOnBuild=True /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=True /p:publishUrl=C:\test

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.

Description:This situation is strange. So i check the build log.And we can find the cause of this issue by pictures below:

Log for first publish in TestMe folder: enter image description here Log for second publish in new folder: enter image description here 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.

Alternative workaround:

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.

1
votes

What might also be helpfull to others is the following:

After you add your .targets file: close and restart visual studio. Visual studio caches the .target stuff when its running so changes dont have effect at all.