Configure .nupkg version and other metadata in JetBrains Rider - nuget-package

I've been trying to figure out all day how JetBrains's Rider sets the .nupkg version and other metadata.
I can't seem to find any configuration window on the whole IDE to do this, or if I have to have a special file with the data on my project.
The .nupkg data always defaults to the following (where MyProjectName is the name of my project) whenever I use Rider to "Pack Solution" or "Pack Selected Projects":
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>MyProjectName</id>
<version>1.0.0</version>
<authors>MyProjectName</authors>
<owners>MyProjectName</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<dependencies>
...
</dependencies>
</metadata>
</package>
I've looked at Microsoft's Docs for Packages and JetBrains Rider Help, but none gave me any hint at what must be done.
I'm using Rider 2017.3.1 on my Ubuntu 14.04.

After googling around to no avail, I found out that I should modify the .csproj file. Also, as #xtmq pointed out, the Microsoft .Net Core docs specify which are the available tags recognized as NuGet metadata properties.
So for example, we could add the values inside the PropertyGroup section so that Rider generates the .nupkg file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>
...
</TargetFrameworks>
<Authors>cavpollo</Authors>
<Version>1.0.1</Version>
<Description>My project Description</Description>
</PropertyGroup>
<ItemGroup>
...
</ItemGroup>
</Project>

Related

Automatically update .nupec file with dependencies

I've created a nuget package that is used by another project.
My package references Microsoft.Identity.Web version 1.23.1. When I pull this package into the project where I want to you use it, I don't get told that my package has this dependency, I understand I can add the following to the .nuspec file to fix this:
<dependencies>
<dependency id="Microsoft.Identity.Web" version="1.23.1" />
</dependencies>
Now I'll get the message about also downloading this package when I install my own package.
I would like this to happen automatically, I don't want to have to go into here and update the version numbers and/or add any new dependencies when I update my package.
I'm sure there is a very basic answer to this but I just can't seem to find the answer.
My .csproj for my package:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web" Version="1.23.1" />
</ItemGroup>
<ItemGroup>
<None Update=".nuspec">
<SubType>Component</SubType>
</None>
</ItemGroup>
</Project>
The right way is to not use a nuspec at all - they're a bit legacy, and since you're using PackageReference dotnet can get all the info it needs from your project file.
See https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#pack-target for how to use your project file to specify any other values you've currently got in your nuspec

Nuget does not import .targets

I want to add my .targets file into project via nuget.
I have next .nuspec file:
<package >
<metadata>
...
</metadata>
<files>
<file src="..\..\Rcs\Rcs\bin\Release\Rcs.targets" target="build\Rcs.targets" />
<file src="..\..\Rcs\Rcs\bin\Release\Rcs.dll" target="lib\Xamarin.iOS10\Rcs.dll" />
<file src="..\..\Rcs\Rcs\bin\Release\*.dll" />
<file src="..\..\Rcs\Rcs\bin\Release\*.config" />
</files>
</package>
And .targets file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<RcsDirectory Condition="$(RcsDirectory) == '' Or $(RcsDirectory) == '*Undefined*'">$(MSBuildThisFileDirectory)..\..\</RcsDirectory>
</PropertyGroup>
<UsingTask
TaskName="Rcs.RcsBuildTask"
AssemblyFile="$(RcsDirectory)Rcs.dll" />
<Target AfterTargets="Build" Name="RcsBuildTask">
<RcsBuildTask
ProjectPath="$(MSBuildProjectFullPath)"
RootNamespace="$(RootNamespace)"
/>
</Target>
</Project>
New doesn't added to project after installing this nuget.
Any help will be appreciated
Are you sure the .targets file is really not imported into your Nuget package?
Along with a .nuspec file you should use the Nuget CLI to pack your files. Don't use the dotnet CLI
Once you created your .nupkg file using the nuget pack command, you can open your package using Nuget Package Explorer or simply WinRar (I believe this should work too with 7-zip).
At this point if you see your .targets file or your Rcs.dll are missing it means you have an issue with your .nuspec file.
Ensure your .nuspec file has the name ProjectName.nuspec
OR
Specify the .nuspec file in your .csproj
<PropertyGroup>
[...]
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<NuspecFile>ProjectName.nuspec</NuspecFile>
[...]
</PropertyGroup>
If your .targets file is in your .nupkg then it means its execution is simply doing not what you expect.
When you install a Nuget package, the files are stored to the Nuget cache. On Windows this is located at %userprofile%\.nuget\packages. Your .targets file and your Rcs.dll should be there too
First of all you can include this in your .targets file
<Target Name="TestMessage" AfterTargets="Build" >
<Message Text="***********************************************************" Importance="high"/>
<Message Text="$(MSBuildThisFileDirectory)" Importance="high"/>
<Message Text="***********************************************************" Importance="high"/>
</Target>
When building your project, you will see the current folder is actually your cache (not your project's directory)
If I understand, what you want is to copy your Rcs.dll into your bin folder. To achieve that you can write a similar task in your .targets file
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)\..\lib\Xamarin.iOS10\Rcs.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
This is a normal behavior that the .targets file is not copied to your project referencing your Nuget package. The .targets file should stay in the Nuget cache, but from there, execute a MsBuild command to copy the Rcs.dll file.
I hope you understand my answer and it solves your problem.
I found that by using NuGet Package Explorer that the targets file had to be named correctly. By default, NPE uses .targtets. It appears that other names may not work.
My Id is "Handy.TargetFrameworkVersionCumulativePreprocessorSymbols", but I had named my target file "HandyTargetFrameworkVersionCumulativePreprocessorSymbols.targets".
Once I used "Handy.TargetFrameworkVersionCumulativePreprocessorSymbols" everywhere it just worked as expected.

How to suppress warnings for missing Mac Server (Warning VSX1000)

We have a number of Xamarin iOS projects that are part of our main solution since we need to ensure that they compile as part of the gated check-in. However most of our developers are not using iOS and hence do not configure a connection to a Mac build agent.
During build locally and on our servers, we see this warning:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Xamarin\iOS\Xamarin.iOS.Windows.After.targets(63,5): Warning VSX1000: No Address and User has been specified in order to establish a connection to a Mac Server, so only the main assembly was compiled for project 'MyProject.iOS'. Connect to a Mac Server and try again to build the full application.
Is there some way of configuring whether this should be a warning, so that we can remove it from the Error List in Visual Studio and the build log from the server? Preferably it should be done in the projects so it could be set once for everyone.
We are using latest Visual Studio 2017 and TFS 2017 Update 2 and build vNext.
A dirty workaround is to override the targets that produce the warning - in my case that's fine as I don't need them.
In our iOS project files I conditionally (if a server address is defined) import a target file, AvoidMacBuildWarning.target, that replaces a number of targets.
Parts of the project file:
...
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets"/>
<Import Project="AvoidMacBuildWarning.target" Condition=" '$(ServerAddress)' == '' " />
<ItemGroup>
...
AvoidMacBuildWarning.target:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="_SayHello">
<Message Text="Warning (demoted to message) VSX1000: No Address and User has been specified in order to establish a connection to a Mac Server, so only the main assembly was compiled for project 'MediumService.iOS'. Connect to a Mac Server and try again to build the full application." />
</Target>
<Target Name="_SayGoodbye">
</Target>
<Target Name="_DetectSdkLocations">
</Target>
<Target Name="_CollectBundleResources">
</Target>
<Target Name="_PackLibraryResources">
</Target>
<Target Name="CopyFilesToMacOutputDirectory">
</Target>
</Project>
We do nothing special to change the warning behavior in VSTS/TFS build comparing with local build through visual studio.
As far as I known, suppressing warnings with MSB prefix is still not possible. Refer to: Supress/Disable/Solve Visual Studio Build Warning
You could give a try with /property:WarningLevel=0through MSBuild argument. Not sure if it will work with this kind of warning above. If not, afraid there is no way to bypass it.
You should use /nowarn:VSX1000 per msbuild documentation
I'd like to add a small variation of Tore Østergaard's answer in case you converted your CSPROJ to an SDK-style project (which iOS projects at this time are usually not, but you can make it work).
In an SDK-style project the "system" targets and props are imported via an SDK attribute at the top of the CSPROJ, like this:
<Project Sdk="MSBuild.Sdk.Extras">
... various project settings ...
</Project>
But if you try to use Tore Østergaard's answer, it won't work, because that answer's target overrides will be themselves overwritten by the SDK's targets (which are always imported last).
The workaround is to manually import the SDK targets and props so that you can control their order:
<Project>
<!--
The SDK is imported manually so that certain targets can be overridden (see bottom of file).
Otherwise we could use Project Sdk="MSBuild.Sdk.Extras"
-->
<Import Project="Sdk.props" Sdk="MSBuild.Sdk.Extras" />
... various project settings ...
<!-- See comment at top of file about manually importing SDK -->
<Import Project="Sdk.targets" Sdk="MSBuild.Sdk.Extras" />
<!--
These targets must be imported last so that they override the SDK-provided targets.
These override the Mac build agent command because they are not needed on CI.
-->
<Import Project="AvoidMacBuildWarning.targets" Condition=" '$(SkipMacBuild)' == 'true' " />
</Project>
Note: I also changed the condition to be a specific condition SkipMacBuild, but you can use whatever condition you want that makes sense for your build.
I also had to add an additional "empty target" to AvoidMacBuildWarning.targets to ensure they were also quieted. My full AvoidMacBuildWarning.targets looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Inspired by https://stackoverflow.com/a/47182083 from Tore Østergaard -->
<Target Name="_SayHello">
<Message Text="INFO: This would have been MSBuild warning VSX1000, but it has been ignored by importing this targets file." />
</Target>
<Target Name="_SayGoodbye">
</Target>
<Target Name="_DetectSdkLocations">
</Target>
<Target Name="_CollectBundleResources">
</Target>
<Target Name="_PackLibraryResources">
</Target>
<Target Name="CopyFilesToMacOutputDirectory">
</Target>
<Target Name="_VerifyBuildSignature">
</Target>
<Target Name="_VerifyXcodeVersion">
</Target>
</Project>

Create a nuspec file that uses all the default behaviour but adding one additional file

I want to have a nuspec file that automatically reads all the info from packages.config and so on, building up all the dependencies, but also includes just one single additional file.
Is there a way to do this?
For example, here is a nuspec file that does almost what I want:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<id>My.Project</id>
<version>1.0.0.0</version>
<owners>Me</owners>
<authors>Me</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<title>My.Project</title>
<description>My.Project</description>
<frameworkAssemblies />
<copyright>Copyright © 2015</copyright>
<tags></tags>
</metadata>
</package>
If my project references ten nuget packages, the above nuspec file will detect that and add them as dependencies when I pack the package together.
But, the moment I add a files element then all the auto-linking to my other nuget dependencies stops:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<id>My.Project</id>
<version>1.0.0.0</version>
<owners>Me</owners>
<authors>Me</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<title>My.Project</title>
<description>My.Project</description>
<frameworkAssemblies />
<copyright>Copyright © 2015</copyright>
<tags></tags>
</metadata>
<files>
<file src="myfile.dll" target="lib\net45" />
</files>
</package>
Without the auto-linking I effectively have to maintain two copies of all the dependencies for my project: one in the csproj file and one here.
There must be a way to do this?! I want to just add a single extra file to my package, via a nuspec.
If you only need to include an additional file in a package packed from a project, simply add the file to the project and set its Build Action to Content.
It will be added to the package under Content directory and on installing will be added to the consuming project.
Are you using Octopus Deploy?
If using Octopus Deploy this can be resolved by adding /p:OctoPackEnforceAddingFiles=true, which tells OctoPack to include the normal files on top of what's defined in <files>..</files>
Read this for more information
https://octopus.com/docs/packaging-applications/nuget-packages/using-octopack/octopack-to-include-buildevent-files

(Solution-Wide pre/post events) MSBuild target executes in command line but not in VS

Basically, I am trying to implement some pre and post build events for an entire solution instead of just standalone projects. I have seen this question around here before, but not addressing the same problem. I have created two .targets files by the name of after.TestSolution.sln.targets and before.TestSolution.sln.targets. Within each:
before
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CreateFile" BeforeTargets="Build">
<Message Text="Creating a file" Importance="high" />
<Exec Command="C:\users\me\Desktop\CreateFiles.bat" />
</Target>
</Project>
after
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CopyFile" AfterTargets="Build">
<Message Text="Copying a File" Importance="high" />
<Exec Command="C:\users\me\Desktop\CopyFiles.bat" />
</Target>
</Project>
These are just simple test batches to see if the events work. I then build the solution through MSBuild from command line: this works fully. MSBuild executes the code within the "before" before the solution builds and the same for the "after" afterwards. HOWEVER, the problem is when I go to build the solution from VS, the batches are never run. So, I am not sure as to why this is. I am fairly new to MSBuild tasks.
This is a known VS feature/bug. As mentioned, VS does not build in the same way as msbuild. Msbuild on the commandline generates an msbuild file from the solution (if you set the MSBUILDEMITSOLUTION environment variable to 1, you'll see a .metaproj file generated for your solution in which the before/after targets are imported). It's my understanding VS does not do that but instead invokes msbuild programatically with no extension points for the solution.

Resources