问题
I am trying to create a nuget using the csproj file. My files are copied, but my log4net.config (and others) are treated as a link when the nuget package is imported by another project. These files are also not copied to the target directory on build. (see the blue icon in the screenshot below)
My cs project contains the following code.
<ItemGroup>
<Content Include="contentFiles/**/*.*;log4net.config" copyToOutput="true">
<IncludeInPackage>true</IncludeInPackage>
<CopyToOutput>true</CopyToOutput>
<BuildAction>Content</BuildAction>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
What do I need to do so that the log4net.config:
1 - is not treated as a linked file
2 - is copied to outbut directory by default.
回答1:
What do I need to do so that the log4net.config: 1 - is not treated as a linked file
This is by design. contentFiles
are supposed to be added as a link. Copying files into the project's source directory is not supported and has been a discouraged practice for classic projects.
Check Martin`s answer here:
This is by design. NuGet packages are no longer supposed to modify the project's source but only add to its built output or build process (in addition to .NET libraries). The content folder for adding sources to the project is only continued to be supported for packages.config based projects since it would be a breaking change for existing projects, but projects using project.json or PackageReference (VS 2017) get the new behaviour.
Now, The contentFiles
section controls the msbuild items that are generated for these files into the obj\projectname.csproj.nuget.g.props
file.
2 - is copied to outbut directory by default.
You can set copyToOutput="true"
for the log4net.config
in the .nuspec file:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>xxxx</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<contentFiles>
<files include="any/any/Views/log4net.config" buildAction="content" flatten="true" copyToOutput="true"/>
</metadata>
<files>
<file src="contentFiles/any/any/log4net.config" target="contentFiles/any/any/log4net.config" />
</files>
You can check the document NuGet ContentFiles Demystified and this thread for more details.
来源:https://stackoverflow.com/questions/50677740/vs2017-nuget-package-files-treated-as-link-instead-of-actual-file