Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

msbuild - dotnet core publish: include/exclude dir in output

Given aspnet project with folders:

/
  /sql
  /WebApp
    /Client
    /wwwroot
    /Views

On project.json I used

"publishOptions": {
    "include": [
      "..\sql",
      "wwwroot",
      "Views",
      "web.config"
    ]
  }

And after dotnet publish there was sql, wwwroot and Views folders.

After migration to csproj (Microsoft.NET.Sdk.Web), I got

<None Update="..sql***;wwwroot***;Views***">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>

After msbuild15 /t:publish (cli not works for me) there is wwwroot, Views AND Client with only json files inside. Files from sql copied but flattened.

I also try to change it to:

<Content Include="..sql**" CopyToPublishDirectory="PreserveNewest" />
<Content Remove="Client*" CopyToPublishDirectory="Never" />

and got same output.

How to remove Client and preserve folder structure for sql?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update:

When using VS 2017 >= 15.3 or a .NET CLI version >= 2.0, there is a new feature that automatically adds Link metadata for a few known item types (including Content). This can be used like:

<Content Include="..sql**" LinkBase="sql" />

Original:

You can use this:

  <ItemGroup>
    <Content Remove="Client**" />
    <Content Include="..sql**" CopyToPublishDirectory="PreserveNewest" Link="sql\%(RecursiveDir)\%(Filename)%(Extension)" />
  </ItemGroup>

The content include item's link metadata is a bit of hack to make MSBuild use the item's relative path as target path. This is because items outside of the "project cone" aren't considered in AssignTargetPath if they have no Link metadata (source).

Alternative to <Content Remove="..." /> you can also do this to still have the files inside VS:

<Content Update="Client**" CopyToPublishDirectory="Never" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...