Dotnet pack - How to specify the package name?

I am trying to package a library as a NuGet package in powershell like:

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 uses the file project.json

and .xproj

(there is a file .csproj

for working on the project in .NET 4.5.1). The above command functions, but I ended up getting the NuGet package name MyProject.Core

and I need it MyProject

to match the legacy packages.

The project is a port and the most sensible MyProject.Core

thing to do is to name the folder after the Java package, which is , but I can't figure out how to get it to generate a NuGet package with a different name.

I tried using this command in the CLI:

dotnet pack "src\MyProject.Core\MyProject.csproj" --output NuGetPackages\ --configuration "Release"

      

but it gives error:

Unable to find a project.json in src\MyProject.Core\MyProject.csproj\project.json

      

According to this page :

Project

Project for packaging. It is either the path to the csproj file or a directory. If this parameter is omitted, the default is the current directory.

So why, if I give the path to csproj, it looks for the project.json file?

I managed to get around this issue for the assembly name by specifying:

"buildOptions": { "outputName": "MyProject" },

      

But the pack command completely ignores this. It is also not possible to specify a NuGet package name in the section packOptions

.

I looked at this old question , but it looks like they are talking about the nuget tool and not the dotnet tool.

Is my only option to rename the folder (which will most likely break a lot of other things), or is there another way to specify the NuGet package name for the command dotnet pack

?

On a side note, I've read in several places what is project.json

going away and we're back to .csproj

, but it's unclear when this will take effect. Should I aim to delete the file project.json

or is it too early for that?

+5


source to share


3 answers


Metadata is specified in csproj as stated in the documentation,

https://docs.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>

      

+4


source


Since Lex Lee pointed out that the toolkit still works, I came up with a hacky solution that didn't force me to resort to files again .nuspec

(and significantly rebuild the project) 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
}

      

+1


source


For those who still have this problem

dotnet pack -p:PackageID=my_id

      

+1


source







All Articles