I generate a nuget package from a project with this command in the post-build event. the variable %conf%
is set to the right configuration (debug or release) and %1
is the project name (e.g. "MyCompany.MyProject").
nuget pack -Prop Configuration=%conf% "%1.csproj" -exclude *.sql -IncludeReferencedProjects
This package is for our own usage only, it will never be published on nuget. It ends in our private repository.
In the project, there is a file that is set to generate action : content
and copy local : always
. (My Visual Studio is in French, so I'm not 100% sure of the translation). Let's name it importantfile.xml
.
In the generated package, I end up with this structure :
- content - importantfile.xml - lib -net45 (.NetFramework,Version=v4.5) -MyCompany.MyProject.dll
Which is fine, I want importantfile.xml
to be deployed in the package, because, well, this file is important!
When I install the package in another project, importantfile.xml
is deployed at the root of the project. That's OK. But it is not set to copy local : always
.
I need importantfile.xml
to be copy local : always
in this project where I install my package.
How can I achieve that?
Notes :
I can set copy local : always
on the file just after installing the package, that's no big deal. I would live with it if later updates of the package would let this property as-is, which is not the case. When updating the package, copy local
is reset to never
(as stated here).
There's a nuspec file in the project's folder, here it is :
<?xml version="1.0"?> <package > <metadata> <id>$id$</id> <version>$version$</version> <title>$title$</title> <authors>$author$</authors> <owners>$author$</owners> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>$description$</description> <copyright>Copyright 2014</copyright> <tags>some random tags</tags> </metadata> </package>
Instead of using a PowerShell script another approach is to use an MSBuild targets or props file with the same name as the package id:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <None Include="$(MSBuildThisFileDirectory)importantfile.xml"> <Link>importantfile.xml</Link> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> </ItemGroup> </Project>
In the nuspec file then, instead of adding the required files to the Content
directory, add them to the Build
directory along with the targets file.
If you require different content for different architectures then you can add architecture folders under Build
also each with their own targets file.
Benefits to using a targets file over the PowerShell script with NuGet Content
directory:
I know you guys got a working solution to this but it didn't work for me so I'm going to share what I pulled out of the NLog.config NuGet package install.ps1 (github source here).
NOTE: this is not my code, this is the content of the install.ps1 from the NLog.config nuget package just sharing the knowledge.
It seems a little more straight forward to me and just hoping to help others that will likely stumble upon this.
You can find the accepted int values for BuildAction here and the accepted values for CopyToOutputDirectory here.
if the link breaks again
Fields prjBuildActionCompile 1
The file is compiled.
prjBuildActionContent 2
The file is included in the Content project output group (see Deploying Applications, Services, and Components)
prjBuildActionEmbeddedResource 3
The file is included in the main generated assembly or in a satellite assembly as a resource.
prjBuildActionNone 0
No action is taken.
param($installPath, $toolsPath, $package, $project) $configItem = $project.ProjectItems.Item("NLog.config") # set 'Copy To Output Directory' to 'Copy if newer' $copyToOutput = $configItem.Properties.Item("CopyToOutputDirectory") # Copy Always Always copyToOutput.Value = 1 # Copy if Newer copyToOutput.Value = 2 $copyToOutput.Value = 2 # set 'Build Action' to 'Content' $buildAction = $configItem.Properties.Item("BuildAction") $buildAction.Value = 2
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