Set PackageId at package time? - msbuild

I'm trying to figure out how to specify a packageId at build or specifically at package time. I have the following in my csproj which works well for creating packages locally for the purposes of testing. We are using Azure DevOps build pipelines to build and pack our nuget packages and I would like to be able to set the packageId as a task or msbuild parameter within the build pipeline.
Does anyone have suggestions on how I could acheive this?
Thanks,
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);Dependencies</TargetsForTfmSpecificBuildOutput>
<Version>0.1.0.5</Version>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<AssemblyName>MyAssembly</AssemblyName>
<RootNamespace>MyAssembly</RootNamespace>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageId>MyAssembly.Test</PackageId> <----- set at package time
<FileVersion>1.0.0.0</FileVersion>
<PackageLicenseUrl>https://my.domain.xyz/license.pdf</PackageLicenseUrl>
<AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
<Description>A useful description.</Description>
<Company>XYZ</Company>
<Authors>UiPath</Authors>
<PackageIconUrl>http://my.domain.xyz/favicon.ico</PackageIconUrl>

Set PackageId at package time?
The answer is yes.
According to the document MSBuild pack target inputs, we could to know the PackageId is supported to be set at build time.
Note:
I saw that you set the property GeneratePackageOnBuild to true, so visual studio will generate the nuget package automatically.
So, if you are not use extra dotnet pack task to package your package, you add msbuild argument -p:PackageId=<PackageId> with your dotnet build task, like:
dotnet build -c Release -p:PackageId=<PackageId>
If you have another dotnet pack task to package your package, #Martin`s answer is correct.
Note2:
When we use the option -p:PackageId to change the package ID, but the AssemblyName is not changed. So the Package ID of the generated package is not consistent with its AssemblyName. We need to pay more attention when we use this nuget package. Or we could also change the AssemblyName to make it match the package id by the option -p:AssemblyName=<AssemblyName>
Hope this helps.

Did you try calling?:
dotnet pack -c Release -p:PackageId=The.Other.Packge.id
Or the equivalent in the .NET Core CLI task

You could modify the XML of the csproj after clone and before build using the File Transform task:
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/file-transform?view=azure-devops

Related

Deploying service fabric Visual studio build failing

I am trying to run the "Visual studio build task" in VSTS CI, but its failing with the following error:
Build error
I read it here bclbuildWhat does the Microsoft.Bcl.Build NuGet package do?
and passed the MSBuild arguments as /p:BclBuildImported=Ignore but still it complains that nuget restore didn't occur. What could be the problem?
Nuget restore task:
Nuget-restore task
Firstly make sure you can build the project locally with VS. Then add Nuget Restore task in the build definition to restore the packages.
UPDATE:
Just check your Nuget.config file, make sure you have set the correct feeds/package sources you want to consume and the package Microsoft.Bcl.Build.1.0.21 is included in the sources.
See Specifying sources in NuGet.config for details.
Besides, you can also try to create a new build definition with the Nuget restore task, then provide the nuget.config file and proper path to solution, also try with other Hosted agents.

Cannot create Nuget package that has xunit as dependency

Steps
Create a new .NETStandard 2.0 project e.g ClassLibrary1
Add xunit package as dependency
Open the command prompt and go to the location
Execute following two commands to create package
Please check dotnet pack for more details
dotnet build /p:SourceLinkCreate=true -v:n -c:Release -p:ci=true
dotnet pack -v:n -c=Release --no-build --include-source --include-symbols --output .\bin
Result: Package not created
Instead of xunit if you add any other dependency it works, for example NUnit and test framework, or Newtonsoft.Json. I have also tried with msbuild command, same result
Is there anything I am missing, or it's a bug?
This github issue says the behaviour is by design and your library needs the IsPackable setting in .csproj:
<Project Sdk="Microsoft.NET.Sdk.Web" ToolsVersion="15.0">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>true</IsPackable>
...

VS2017 msbuild / nuget pack

I am having a problem with nuget (version 4.3.0.4406) and msbuild (version 15.3.409.57025). I am using VS2017 to create class library. Using the pack capability of VS2017 i can successfully create a nuget package (that i can install in another solution). Now i want to add an install.ps1 script to the package in the tools folder that runs when the nuget is installed.
In the csproj file i am specifying multiple target frameworks:
<TargetFrameworks>net45;net452</TargetFrameworks>
I cannot figure out how to do this. I've created a nuspec file using the nuget -spec command which generates a simple nuspec file. When i use the msbuild command with the /t:pack and /p:Nuspecfile=path.to.nuspec I get the following errors:
NuGet.Build.Tasks.Pack.targets(141,5): error : Value cannot be null or an empty string.
I have nuspec files from other projects (from VS2015 solutions) that work without problem, and the structure of the one i am using now is basically the same. Can anyone let me know whether i am trying something that cannot be done?
You can pack any item by updating its metadata in the csproj file:
<ItemGroup>
<None Update="install.ps1" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="\tools" />
</ItemGroup>
Note that the ps1 file is only run for projects using packages.config to reference the NuGet package and you should investigate alternative ways to accomplish what you are trying to do with the script as PackageReference is now more likely to be used instead.

"Error MSB4057 missing target pack" when building .netstandard nuget package

I'm trying to create a .netstandard nuget package following these instructions, using VS2017 RC. It builds fine, but when I try to create the package using
msbuild /t:pack /p:Configuration=Release
I get an error, that the target pack is not available in my solution:
error MSB4057: The target "pack" does not exist in the project.
I'm not really sure what to do with this message or where I should be looking to fix it. Any suggestions?
Thanks to an answer on the MSDN forums I was able to get it working.
You'll have to specify your .csproj in the build command so it won't try to use the solution file (.sln).
msbuild "C:\Users\Administrator\Documents\visual studio 2017\Projects\AppLogger\AppLogger\AppLogger.csproj" /t:pack /p:Configuration=Release
Additionally I had to install the NuGet.Build.Tasks.Pack" package from NuGet.
The command msbuild /t:pack /p:Configuration=Release is specifying that MSBuild should run the pack target within the build script. The error indicates that MSBuild isn't able to find that target within the build script (or one of it's imports). Have you double checked your prerequisites? You're either using the wrong build script or it's missing an <import> tag.
You must import targets before using them. In project file before using targets write:
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />

Visual Studio Team Services Continuous Integration: NuGet Restore Task Failed

I am using Continuous Integration feature in Team Services (was Visual Studio Online). My build definition targets a specific project in a solution (not the whole solution), which is ClientUI MVC website.
The solution contains three projects:
ClientUI
AdminUI
Client Services
The Build Definition for ClientUI Project:
Repository:
Nuget Installer Step:
I have tried different params but not working.
Visual Studio Build
Before trying to target the a single project, my build definition was targeting the whole solution with the following parameters:
NuGet Installer -> Path to Solution: **\*.sln
Visual Studio Build -> Solution: **\*.sln ; MSBuild Arguments: /p:outdir=$(build.artifactstagingdirectory)
It was working. However now, it generates this error in the Nugget Restore Task:
2016-04-22T21:07:00.6716725Z Set workingFolder to default: C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\1.98.1\tasks\NuGetInstaller\0.1.25
2016-04-22T21:07:00.8163908Z Executing the powershell script: C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\1.98.1\tasks\NuGetInstaller\0.1.25\NuGetInstaller.ps1
2016-04-22T21:07:01.5283529Z ##[error]Cannot find path 'C:\a\1\s\packages.config' because it does not exist.
2016-04-22T21:07:01.5439897Z C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\1.98.1\agent\worker\tools\NuGet.exe restore "C:\a\1\s\packages.config" -NonInteractive
2016-04-22T21:07:03.0441507Z MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'.
2016-04-22T21:07:03.0597010Z ##[error]Cannot determine the packages folder to restore NuGet packages. Please specify either -PackagesDirectory or -SolutionDirectory.
2016-04-22T21:07:03.0909881Z ##[error]Unexpected exit code 1 returned from tool NuGet.exe
Try setting "Installation type" to "Install" for "Nuget Installer" task since you are using "packages.config" to install the packages.
For anyone curious, the source of the error about "Please specify either -PackagesDirectory or -SolutionDirectory" is that the build process is trying to issue a command similar to this:
C:\hostedtoolcache\windows\NuGet\4.4.1\x64\nuget.exe restore D:\a\1\s\MyProject\packages.config -PackagesDirectory packages -Verbosity Detailed -NonInteractive
The below screenshots should help if you want to build a project (rather than the solution) and your nuget "packages" folder is at the solution-level.
Additionally, you may need to specify this as the "MSBuild Argument" in the build task of your project: /p:SolutionDir="/"
I had the same thing sorted it out by changing the mapping - go to Repository tab, I had my mapping to another directory which means the nuget installer could not execute.