.NET Core Solution: Get licenses from used NuGet packages
Posted on May 17, 2024 (Last modified on August 20, 2024) • 3 min read • 610 wordsHow to get a txt or Excel file for all license of used Nuget package in your .NET Core Solution
ToDo: Get all licenses from used NuGet Packages in the Visual Studio Solution. Create a license text file or Excel file with all found license and save all license as a separate Text File.
Let’s try…
Best solution so far: jump to Solution 5 or Solution 6
Get-Package | Select-Object Id,LicenseUrl
Output Result (Nuget Package Name, License Url):
xunit.runner.visualstudio
https://raw.githubusercontent.com/xunit/xunit/master/license.txt
NSubstitute
https://github.com/nsubstitute/NSubstitute/raw/master/LICENSE.txt
Good… but the direct reference project NuGet package. Need also the License of all dlls.
Try out with the PowerShell script. Save the code as DownloadNugetLicense.ps1 in the VS solution director.
Run the PowerShell Script with ./DownloadNugetLicense.ps1 in the Visual Studio Package Manager Console.
PowerShell Code:
Split-Path -parent $dte.Solution.FileName | cd; New-Item -ItemType Directory -Force -Path ".\licenses";
@( Get-Project -All | ? { $_.ProjectName } | % {
Get-Package -ProjectName $_.ProjectName | ? { $_.LicenseUrl }
} ) | Sort-Object Id -Unique | % {
$pkg = $_;
Try {
if ($pkg.Id -notlike 'microsoft*' -and $pkg.LicenseUrl.StartsWith('http')) {
Write-Host ("Download license for nuget package " + $pkg.Id + " from " + $pkg.LicenseUrl);
#Write-Host (ConvertTo-Json ($pkg));
$licenseUrl = $pkg.LicenseUrl
if ($licenseUrl.contains('github.com')) {
$licenseUrl = $licenseUrl.replace("/blob/", "/raw/")
}
$extension = ".txt"
if ($licenseUrl.EndsWith(".md")) {
$extension = ".md"
}
(New-Object System.Net.WebClient).DownloadFile($licenseUrl, (Join-Path (pwd) 'licenses\') + $pkg.Id + $extension);
}
}
Catch [system.exception] {
Write-Host ("Could not download license for " + $pkg.Id)
}
}
Not perfect - I also need the license for the used Dlls inside any NuGet Package.
Abp.AspNetCore; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.AspNetCore.SignalR; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.AutoMapper; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.Castle.Log4Net; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.HangFire; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.RedisCache; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.TestBase; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.ZeroCore; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) Abp.ZeroCore.EntityFrameworkCore; https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/LICENSE; The MIT License (MIT) AspNet.Security.OpenIdConnect.Server; http://www.apache.org/licenses/LICENSE-2.0.html; Castle.Core; http://www.apache.org/licenses/LICENSE-2.0.html;
PowerShell Code
Write-Host("---------------------");
Write-Host("NuGet Package License");
Write-Host("---------------------");
Write-Host("Id | LicenseUrl | License");
Write-Host("---------------------");
@( Get-Project -All | ? { $_.ProjectName } | % {
Get-Package -ProjectName $_.ProjectName | ? { $_.LicenseUrl }
} ) | Sort-Object Id -Unique | % {
$pkg = $_;
$file =
Try {
if ($pkg.Id -notlike 'microsoft*' -and $pkg.LicenseUrl.StartsWith('http')) {
$licenseUrl = $pkg.LicenseUrl
if ($licenseUrl.contains('github.com')) {
$licenseUrl = $licenseUrl.replace("/blob/", "/raw/")
}
$extension = ".txt"
if ($licenseUrl.EndsWith(".md")) {
$extension = ".md"
}
$filePath = (Join-Path (pwd) 'licenses\') + $pkg.Id + $extension;
$textLicense = Get-Content $filePath | Select-Object -First 1
Write-Host($pkg.Id + "; " + $_.LicenseUrl + "; " + $textLicense);
}
}
Catch [system.exception] {
Write-Host ($error[0].Exception);
Write-Host ("Could not read license for " + $pkg.Id)
}
}
Visual Studio 2017 Extension: List license of all NuGet packages inside the ‘packages’ folder of any Visual Studio solution. Refer license from the Nuget ProjectUrl / LicenseUrl in the package metadata. Download license and license text from GitHub, spdx.org.
Visual Studio Marketplace: Package Licenses
Package Folder
Package Folder not exist for the solution?
NuGet.Config
Create Nuget.Config for the Visual Studio solution:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
</configuration>
Create your own license file output (Excel or TEXT) with the following .NET Core Console Project:
https://github.com/do-it-ger/DoitPackagesLicenses
A .net core tool to print the licenses of a project. This tool support .NET Core and .NET Standard Projects.