Reputation: 6085
We use the standard NuGet.targets file in our solutions and projects to do package update and package creation on build.
Now the problem is: When we build our solution like this:
msbuild MySolution.sln /P:Configuration=Release;BuildPackage=true;PackageOutputDir="..\CreatedPackages"
then the NuGet.targets file gets along and builds packages for all projects in our solution. So we actually end up with Nuget packages for our Unit- and Integration tests projects too, which we do not want.
Setting the Property <BuildPackage>false</BuildPackage>
unconditionally in these projects does not help, as the property is already set by commandline and cannot be changed afterwards. I do not want to change the nuget.targets
file.
So the question is: How can I prevent package creation for specific projects in the solution, while other projects should build their packages?
Upvotes: 2
Views: 2545
Reputation: 2880
By providing <IsPackable>
property value to false
in your .csproj
file you can ignore that project to do not pack as nuget package
<IsPackable>true</IsPackable>
- create package
<IsPackable>false</IsPackable>
- don't create package
Following sample copy of your .csproj
file
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishRepositoryUrl>false</PublishRepositoryUrl>
<IsPackable>false</IsPackable>
</PropertyGroup>
Upvotes: 6
Reputation: 6085
It seems it is not possible to solve this without modification to the Nuget.targets
file.
What we did is introduce a new property, 'SkipBuildPackage', which by default (Conditional, if not already set to another value) is set to false
in the nuget.targets
file.
The BuildPackage
target then gets an additional conditional, that evaluates this property and does not execute the target when SkipBuildPackage
is set to true
.
We set this property to true
in the project files of the the projects where we do not want to build the packages by default.
It is, however, still possible to build packages for these projects too, by specifying /P:SkipBuildPackage=false
on the command line. This will override the property in the project files and the default in the nuget.targets
file and prevent all packages to be skipped.
Upvotes: 2
Reputation: 336
The problem is that the nuget.targets file is connected to all the projects config file(.csproj) in the solution, and the BuildPackage target is in the nuget.targets file also. Two ideas to solve it:
For projects that don't need the nuget.targets, meaning they don't need to restore packages in the build process, you can remove the import to nuget.target file from their .csproj config file.
Copy the BuildPackage target to the projects config file(.csproj), only to the project you want to pack, and remove it from the nuget.targets. This will isolate the BuilPackage target, only to the relevant projects.
Upvotes: 0