I'm using CopyToPublishDirectory
in my .csproj to copy over files/folders when publishing my dotnet app:
<None Update="Views\**\*; wwwroot\**\*">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
Is there a way to copy over a folder and change its name in the process? For example, I'd like to copy over a subset of my node_modules
folder, so I could create a new folder called node_modules_dev
with my subset of npm dependencies, and copy it over via CopyToPublishDirectory
as node_modules
. I'd imagine the syntax would work something like this:
<None Update="node_modules_dev/**/*" Rename="node_modules">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
Thanks--
When files are not located in a project folder Copy to Output Directory does not work as Copy to Output Directory only works on project files. For files, residing outside of the project folder working with Post build events provides the capability to copy files to the build folder of a project.
The CopyToPublishDirectory metadata on an MSBuild item controls when the item is copied to the publish directory. Allowable values are PreserveNewest, which only copies the item if it has changed, Always, which always copies the item, and Never, which never copies the item.
The easiest approach is setting the metadata ( CopyToOutputDirectory / CopyToPublishDirectory) items conditionally (assuming .txt being a None item instead of Content, if it doesn't work, try <Content> instead): If more control is required, the most versatile approach is to add custom targets that hook into the build process in the csproj file:
This also means any data in a file within the build folder will be overwritten. Do not Copy: As the option indicates, the file will not be copied. Note that changing from one of the other options will cause the file to be removed from the build folder.
The trick here is to:
DefaultItemExcludes
property so the web sdk will ignore the files.%(RecursiveDir)
metadata that is available for items expanded via glob patterns and represents the value of any expanded path. This will be defined for the Include="…"
syntax only hence 1.This will overwrite the default target path to a new directory using the Link
metadata:
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);node_modules_dev\**</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<Content Include="node_modules_dev\**\*" Link="node_nodules\%(RecursiveDir)%(FileName)%(Extension)" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
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