Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CopyToPublishDirectory in .csproj

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--

like image 430
sir_thursday Avatar asked Sep 18 '17 15:09

sir_thursday


People also ask

Why is copy to output directory not working on my project?

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.

What is the use of copytopublishdirectory metadata in MSBuild?

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.

How to add metadata to a csproj file?

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:

What does “do not copy” mean in a build folder?

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.


1 Answers

The trick here is to:

  1. Ensure that the items are not yet included by default so there is no leftover metadata from previous glob pattern expansions. This can be done by adding the path to the DefaultItemExcludes property so the web sdk will ignore the files.
  2. Use the %(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>
like image 172
Martin Ullrich Avatar answered Sep 30 '22 11:09

Martin Ullrich