I have a config file in a .netstandard2.0 project, and I'd like to include it in the same directory when packing, I already have this in csprof file:
<ItemGroup>
<None Include="load.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
and when I build it with dotnet build, the config file indeed was copied to output directory, but when dotnet pack, the nugget only includes the .dll, not the .config file, how can I include this config file in nugget package?
Try to add Pack="true" property:
<ItemGroup>
<None Include="load.config" Pack="true">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
You may also need to specify PackagePath property as well. I have found sample in files section.
Related
NuGet nuspec files allow you to specify the target for <file> elements.
eg.
<files>
<file src="things/**/*" target="content/stuff/" />
</files>
Is there an equivalent if you're using a csproj (and dotnet pack), instead of a .nuspec file?
eg. Given
<ItemGroup>
<Content Include="things/**/*" />
</ItemGroup>
Can I add something to that to make those files end up in content/stuff in the .nupkg file?
According to the Microsoft docs, I'd say it should look like this:
<Content Include="things/**/*">
<Pack>true</Pack>
<PackagePath>content/stuff/</PackagePath>
</Content>
I have a .NET Core 3.1 API that I am attempting to deploy as a Guest Executable in Service Fabric using an Azure DevOps build pipeline. The platform for all projects/configurations is x64.
I have locally tested the API and also locally tested the API running as a guest executable within service fabric. I am able to build/rebuild the API and I am able to package the service fabric application.
In my build pipeline, I have the following steps shown in the picture below.
The Create Service Fabric Package is defined in the picture below
When the build pipeline runs, it always fails with the error:
##[error]AppraisalStatusUpdatesContainer\AppraisalStatusUpdatesContainer.sfproj(0,0): Error MSB4057: The target "Package" does not exist in the project.
I cannot find any documentation on what to do to solve this issue and have spent a couple of days trying. Does anyone know how to get this to work?
Edit 1
The sfproj is provided below
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.props" Condition="Exists('..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.props')" />
<PropertyGroup Label="Globals">
<ProjectGuid>ffefa7ed-cf72-4780-9910-816deed2ed4f</ProjectGuid>
<ProjectVersion>2.5</ProjectVersion>
<MinToolsVersion>1.5</MinToolsVersion>
<SupportedMSBuildNuGetPackageVersion>1.6.10</SupportedMSBuildNuGetPackageVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<None Include="ApplicationPackageRoot\ApplicationManifest.xml" />
<None Include="ApplicationParameters\Prod.xml" />
<None Include="ApplicationParameters\QA.xml" />
<None Include="ApplicationParameters\UAT.xml" />
<None Include="ApplicationParameters\Local.1Node.xml" />
<None Include="ApplicationParameters\Local.5Node.xml" />
<None Include="PublishProfiles\QA.xml" />
<None Include="PublishProfiles\UAT.xml" />
<None Include="PublishProfiles\Prod.xml" />
<None Include="PublishProfiles\Local.1Node.xml" />
<None Include="PublishProfiles\Local.5Node.xml" />
<None Include="Scripts\Deploy-FabricApplication.ps1" />
</ItemGroup>
<ItemGroup>
<Content Include="..\AppraisalStatusUpdates\bin\Release\netcoreapp3.1\publish\**\*.*">
<Link>ApplicationPackageRoot\AppraisalStatusUpdatesContainerPkg\Code\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Content>
<Content Include="ApplicationPackageRoot\AppraisalStatusUpdatesContainerPkg\Config\Settings.xml" />
<Content Include="ApplicationPackageRoot\AppraisalStatusUpdatesContainerPkg\ServiceManifest.xml" />
<Content Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />
<PropertyGroup>
<ApplicationProjectTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Service Fabric Tools\Microsoft.VisualStudio.Azure.Fabric.ApplicationProject.targets</ApplicationProjectTargetsPath>
</PropertyGroup>
<Import Project="$(ApplicationProjectTargetsPath)" Condition="Exists('$(ApplicationProjectTargetsPath)')" />
<Import Project="..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.targets" Condition="Exists('..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.targets')" />
<Target Name="ValidateMSBuildFiles" BeforeTargets="PrepareForBuild">
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.props')" Text="Unable to find the '..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.props' file. Please restore the 'Microsoft.VisualStudio.Azure.Fabric.MSBuild' Nuget package." />
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.targets')" Text="Unable to find the '..\packages\Microsoft.VisualStudio.Azure.Fabric.MSBuild.1.6.10\build\Microsoft.VisualStudio.Azure.Fabric.Application.targets' file. Please restore the 'Microsoft.VisualStudio.Azure.Fabric.MSBuild' Nuget package." />
</Target>
</Project>
Edit 2
#LeoLiu-MSFT, I have attempted the approach you mentioned. I am not getting the original error, but am now attempting to resolve the subsequent issues that resulted. Also, I am doing dotnet publish and dotnet test tasks before this step. It seems like the publish step is unnecessary except that it is needed for running the tests.
My msbuild step is now as follows
This results in the exception below:
##[error]C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(777,5): Error : The OutputPath property is not set for project 'AppraisalStatusUpdatesContainer.sfproj'. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='Release' Platform='x64'. You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn't exist for this project.
I have a custom Powershell script in my service fabric repositories for this.
It does a separate nuget restore for .sfproj.
The script restore-sf.ps1:
Push-Location $PSScriptRoot
$ProjectFolder = "..\src\YourProjectFolder"
$PackageFolder = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\Packages"))
Push-Location $ProjectFolder
nuget restore YourServiceFabricProject.sfproj -PackagesDirectory $PackageFolder -MSBuildVersion 15
Pop-Location
Pop-Location
My folder structure:
├───Root
├───packages
├───src
├───YourProjectFolder
├───YourServiceFabricProject.sfproj
├───scripts
├───restore-sf.ps1
Depending on your folder structure, you'll need to change the paths in the script.
In the pipeline one of the first steps is to call the script, to restore the Microsoft.VisualStudio.Azure.Fabric.MSBuild package.
The script assumes nuget is available in the path.
Azure DevOps build pipeline for Service Fabric Guest .Net Core 3.1 API exe fails on creating package
According to the error:
MSB4057: The target "Package" does not exist in the project.
When you are using MSBuild for a solution of projects (.sln) with target Package, but not all projects have the Package task defined. That may be one of the reasons why you get this error.
To resolve this issue, we could add following custom target in the .sfproj file:
<Target Name="ForcePackageTarget" AfterTargets="Build" Condition="'$(ForcePackageTarget)' =='true'">
<CallTarget Targets="Package"/>
</Target>
Then add /p:ForcePackageTarget=true as an argument to the msbuild build task.
Please check this thread and this post for some more details.
I maintain a few tiny Nuget Packages.
I have a Nuget README file in the git repository, and the .nupkg file is auto-built by VS, based on the "Package" config, stored in the .csproj file.
Whenever I need to release a new versio of the package, I upload the .nupkg file to nuget, in the web UI, and then it asks me for any documentation, at which point I have to manually upload the README file.
Is there any way to put that README file in the .nupkg so that I don't have to manually upload it every time?
NuGet have now added support to make this JustWork.
Announcement on the Issue thread: https://github.com/NuGet/Home/issues/6873#issuecomment-833829727
Announcement Blog post: https://devblogs.microsoft.com/nuget/add-a-readme-to-your-nuget-package/
All you need to do is reference the README.md file in the .csproj, using the PackageReadmeFile tag. If the README.md is in the git root (i.e. where GitHub auto-detects it) then it will look like this:
<PackageReadmeFile>README.md</PackageReadmeFile>
Currently there is not a way to do this, but it is WIP.
The new spec under review: https://github.com/NuGet/Home/wiki/Embedding-and-displaying-NuGet-READMEs
This issue for this feature: https://github.com/NuGet/Home/issues/6873
i do this via the csprj. file.
I created a .netstandard project and added a readme.txt.
add following to the x.csproj
<ItemGroup>
<None Include="readme.txt" pack="true" PackagePath="." />
</ItemGroup>
When installing the nuget the first time the file will be displayed in VS.
There is somewhere in the internet a artikel describing the solution, but i cannot find it now.
Here my complete csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net471;net35</TargetFrameworks>
.....
<FileVersion>1.0.1.11</FileVersion>
<AssemblyVersion>1.0.1.11</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.6" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="readme.txt" pack="true" PackagePath="." />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
I'm trying to add two text files to my nuget package so that when the user of the package builds the project, one file is copied to the root of the build directory, but another file is copied to a subfolder of the root like this
\bin\Debug\ - 1.TXT
- SubFolder\ - 2.TXT
This directory structure is a requirement of the assemblies in the package which will be looking for those files in those particular locations at runtime.
Based on this https://stackoverflow.com/a/30386836/1462656
and this https://stackoverflow.com/a/44752745/1462656
I am able to create a targets file that copies 1.txt to the root, but when I add the package to a project and build it, it just copies 2.txt to the root. I created a build folder as follows
And my targets file looks like this
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)1.txt">
<Link>1.txt</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)SubFolder\2.txt">
<Link>2.txt</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
How do you get it to create the SubFolder and copy the file to that folder?
Initially I was using the content folder to add my content files with subfolders to the nuget package, but I found that although the files were added with the correct directory structure to the solution itself, the files were added with attributes set to 'Do Not Copy' so they were not copied to the build directory - so I switched to the above approach instead. Is there a way to combine the two approaches?
I think I solved it using the following
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<RequiredFiles Include="$(MSBuildThisFileDirectory)**\*" />
<None Include="#(RequiredFiles)">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Based on a solution found here https://stackoverflow.com/a/30316946/1462656
.nuspec file has section <files>, what are the alternatives for include localization resources for new csproj file? How to add custom DLL files?
The first part of the trick is to get your third party DLL added to the nupkg. This does it:
<ItemGroup>
<Reference Include="ThirdParty">
<HintPath>..\DLLs\ThirdParty.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="$(OutputPath)\ThirdParty.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
If you install this package into an old-style csproj then a reference to ThirdParty.dll will be added.
However, if you install this package into a new-style csproj then a reference to ThirdParty.dll will not be added as a referece, irritatingly. Work in progress...