Excluding projects from dotnet pack task in Azure DevOps

随声附和 提交于 2021-02-08 10:59:22

问题


I have an VS solution file (.sln) which consists .net standard libraries and test projects. Now the project has an Integration test project that is targeted against .net Framework 4.7.2.

My current build pipeline does the following dotnet build dotnet pack dotnet push

With the dotnet pack, I am trying to excldude the project Integration.Tests.csproj.

My current folder structure looks like the following

CompanyName.Service\CompanyName.Service.csproj
CompanyName.Data\CompanyName.Data.csproj
Integration.Tests\Integration.Tests.csproj

According to the helper tooltip in the path to include/exclude we can do it like the following

**/*.csproj;-:**/*.Tests.csproj

However the dotnet pack task simply is not ignoring the Integration.Tests.csproj. It tries to pack it and it fails. I'm not sure what I am doing wrong or if this is an issue with the task. I haven't been able to locate any known issues on this so far.

Note: Prior to having that project in my solution, the pipeline was working fine with the projects being packed and published to nuget.


回答1:


If you are using the version (2.x) of the dotnet task in Azure Devops, you should use ! instead of -:.

Because the exclude pattern has changed. That these patterns were updated in version 2 of the NuGet task; if you have a pattern that contains -:, use ! instead:

Pack NuGet packages

As test, it works fine on my side.

My folder setup:

Note: If you still have issue with that task, please check the if the error comes from build not packing when you running that task. Or you can select the option Do not build:

But you should build the project before you packing it.




回答2:


Apart of including only the necessary files (one), you can modify the pipeline to build only the needed resources (two). The details:

One) This is the main answer to your question: the package operation will include the dependencies, so you only need to package the main project. In the image below, you can see in yellow the path and filename of the main project of the package (instead of guessing, click the button pointed by the read arrow to browse for it). As explained, the dependencies will be automatically included. You don't need to include them explicitly. This also includes the required Nuget packages, which are included as dependent packages.

Two) By default, the build configuration is defined in the $(BuildConfiguration) variable of the pipeline and it's usually Release (above, in green). If you want to save compute efforts and time in your pipeline, you can create a new Solution configuration inside Visual Studio. Below you can see a solution configuration already created, with the name Package. To create it:

  • in configuration manager, click <New...>(in green below)
  • specify a name for the configuration (Package)
  • choose the configuration of the projects (Release or Debug) or create a new one. Creating a new one is only useful if your project needs to do something special for packaging, for example, if you have configuration transforms. In the example, the main project has the Package configuration.
  • in the configuration specify the projects that need to be built. Include the main project and their dependencies. In this example the main project doesn't depend on others, so it's the only one checked in the Build column. You usually exclude the test projects, proof of concepts, and this kind of projects.
  • the last step is in the pipeline itself: specify the solution configuration (Package) directly in the green text box of the first snapshot, or as the value of that variable, in the Variables tab of the pipeline

If you do so, only the needed projects will be compiled, and all the dependencies will be included in the package.



来源:https://stackoverflow.com/questions/57503238/excluding-projects-from-dotnet-pack-task-in-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!