I am trying to pack a library as a NuGet package in powershell, like so:
function Create-NuGetPackages($projects) {
if (!(Test-Path $NuGetPackageDirectory)) {
New-Item $NuGetPackageDirectory -ItemType Directory
}
foreach ($project in $projects) {
pushd $project.DirectoryName
& dotnet.exe pack --configuration $Configuration --output $NuGetPackageDirectory --no-build
popd
}
return $NuGetPackageDirectory
}
The project is using a project.json
and a .xproj
file (there is also a .csproj
file for working on the project in .NET 4.5.1). The above command functions, but I end up with a NuGet package name MyProject.Core
and I need it to be MyProject
to match the legacy packages.
The project is a port and the most sensible thing to do is name the folder after the Java package, which is MyProject.Core
, but I can't seem to figure out how to make it generate a NuGet package with a different name.
I have tried using this command on the CLI:
dotnet pack "src\MyProject.Core\MyProject.csproj" --output NuGetPackages\ --configuration "Release"
but it gives the error:
Unable to find a project.json in src\MyProject.Core\MyProject.csproj\project.json
According to this page:
PROJECT
The project to pack. It's either a path to a csproj file or to a directory. If omitted, it defaults to the current directory.
So why if I specify the path of the csproj does it look for a project.json file?
I was able to work around this issue for the name of the assembly by specifying:
"buildOptions": { "outputName": "MyProject" },
But the pack command totally ignores this. There also doesn't seem to be an option to specify the name of the NuGet package in the packOptions
section.
I had a look at this old question but it looks like they are talking about the nuget tool, not the dotnet tool.
Is my only option to rename the folder (which will likely cause a lot of other stuff to break now), or is there another way to specify the NuGet package name for the dotnet pack
command?
On a side note, I have read in several places that project.json
is going away and we are going back to .csproj
, but it is unclear when that will take effect. Should I be aiming to eliminate the project.json
file or is it too early for that?
For whoever still has this issue
dotnet pack -p:PackageID=my_id
Metadata are specified in csproj, as the documentation says,
https://learn.microsoft.com/en-us/nuget/guides/create-net-standard-packages-vs2017#edit-metadata-in-the--csproj-file
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<PackageId>AppLogger.YOUR_NAME</PackageId>
<PackageVersion>1.0.0</PackageVersion>
<Authors>YOUR_NAME</Authors>
<Description>Awesome application logging utility</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>First release</PackageReleaseNotes>
<Copyright>Copyright 2016 (c) Contoso Corporation. All rights reserved.</Copyright>
<PackageTags>logger logging logs</PackageTags>
</PropertyGroup>
Since Lex Li pointed out that the tooling is still a work in progress, I came up with a hackish solution that doesn't force me to have to resort to .nuspec
files again (and much rearranging of the project) just to get it working.
# Hack because dotnet pack doesn't provide a way to override the directory
# name for the NuGet package name when using project.json.
# So, we copy MyProject.Core to a new directory
# MyProject to work around this.
function Copy-MyProject() {
Copy-Item -Recurse -Force "$root\src\MyProject.Core" "$ReleaseDirectory\MyProject"
}
function Delete-MyProject-Copy() {
Remove-Item "$ReleaseDirectory\MyProject" -Force -Recurse -ErrorAction SilentlyContinue
}
function Create-NuGetPackages($projects) {
try
{
Copy-MyProject
$projects = $projects += Get-ChildItem -Path "$ReleaseDirectory\MyProject\project.json"
$projects = $projects | ? { !$_.Directory.Name.Equals("MyProject.Core") }
if (!(Test-Path $NuGetPackageDirectory)) {
New-Item $NuGetPackageDirectory -ItemType Directory
}
foreach ($project in $projects) {
pushd $project.DirectoryName
Write-Host "Creating NuGet package for $project..." -ForegroundColor Magenta
& dotnet.exe pack --configuration $Configuration --output $NuGetPackageDirectory --no-build
popd
}
} finally {
Delete-MyProject-Copy
}
return $NuGetPackageDirectory
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With