Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active June 28, 2019 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/ca740578776db7fca21c844daf89bb4a to your computer and use it in GitHub Desktop.
Save guitarrapc/ca740578776db7fca21c844daf89bb4a to your computer and use it in GitHub Desktop.
Information about msbuild with dotnet core

MSBuild Github Project (dotnet cli using this msbuild)

https://github.com/Microsoft/msbuild

You can pass build parameter to msbuild through dotnet build or dotnet publish

Can't pass msbuild params in dotnet run #7229

MSBuild (Core) detect property

Use $((MSBuildRuntimeType). This will return Full | Core | Mono.

https://github.com/aspnet/BuildTools/blob/9ea72bcf88063cee9afbe53835681702e2efd720/src/Internal.AspNetCore.BuildTools.Tasks/build/Internal.AspNetCore.BuildTools.Tasks.props#L2-L6

.NET Core MSBuild cannot load tasks built against MSBuild 4.0 #2111

MSBuild Conditions

https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions

MSBuild Reserved and Well-Known Properties

https://docs.microsoft.com/ja-jp/visualstudio/msbuild/msbuild-reserved-and-well-known-properties

Common MSBuild Project Properties

https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties

DependsOn vs. BeforeTargets/AfterTargets

BeforeTargets and AfterTargets are much prefer because it can handle like Condition. Hey, what's MSBuild 4? We are living in MSBuild 15.0!

https://stackoverflow.com/questions/2855713/what-is-the-difference-between-dependsontargets-and-aftertargets

dotnet and msbuild command relationship

https://stackoverflow.com/questions/43422488/relationship-between-the-dotnet-cli-and-the-new-vs2017-msbuild

dotnet publish will use $(PublishDir), not $(OutputPath) or $(OutDir

https://stackoverflow.com/questions/48298741/dotnet-publish-o-dist-does-not-set-outdir-or-outpath-in-msbuild

New .csproj glob format

You cannot use [abc]* to pickup "ahoge", "bhoge", "choge". It doesn't work at all.

https://docs.microsoft.com/en-us/dotnet/core/tools/csproj#default-compilation-includes-in-net-core-projects

File Naming rule

Better using . separator for DependentUpon like items.

Docker? Dockerfile, Dockerfile.dev, Dockerfile.release, .dockerignore will be sum up automatically with this naming rule.

image

dotnet publish never clean

dotnet/sdk#377

Make your own RemoveDir MSBuild Task like following. It should run After Build & Before Publish

    <!-- Clean PublishDir task -->
    <Target Name="CleanPublishArtifact" AfterTargets="Build">
      <PropertyGroup>
        <CleanPath>$([System.IO.Path]::Combine($(MSBuildThisFileDirectory),$(PublishDir)))</CleanPath>
      </PropertyGroup>
      <RemoveDir Directories="$(CleanPath)" />
    </Target>

Call same target multiple time

Use <MSBuild Projects="$(MsBuildThisFile)" Targets="SwapConfig" Properties="YourUniqueProperty=AndValueXxx"/> to make your call unique!

https://stackoverflow.com/questions/25347562/calling-subtarget-twice-results-in-this-target-being-skipped-how-to-prevent

MSBuild Task to SwapConfig hook with BranchName property passed on Build or Publish?

    <PropertyGroup>
      <!--app-->
      <SwapFileName></SwapFileName>
      <!--config-->
      <SwapExtension></SwapExtension>
      <!--$(MSBuildThisFileDirectory)-->
      <SwapDestinationDir></SwapDestinationDir>
    </PropertyGroup>

    <Target Name="SwapConfig">
      <PropertyGroup>
        <CopyConfigMessage>BranchName build parameter missing! Skipping $(SwapFileName).$(SwapExtension) swap task.</CopyConfigMessage>
        <CopyConfigDestination>$([System.IO.Path]::Combine($(SwapDestinationDir), $(SwapFileName).$(SwapExtension)))</CopyConfigDestination>
      </PropertyGroup>
      <PropertyGroup Condition="'$(BranchName)' != '' AND Exists('$(MSBuildThisFileDirectory)$(SwapFileName).$(BranchName).$(SwapExtension)')">
        <!-- For "dotnet build|publish -c Debug /p:BranchName=Xxxx" where xxx.BranchName.config exists -->
        <CopyConfigMessage>Detected BranchName parameter, Copy $(SwapFileName).$(BranchName).$(SwapExtension) to $(SwapFileName).$(SwapExtension)</CopyConfigMessage>
        <CopyConfigSource>$(MSBuildThisFileDirectory)$(SwapFileName).$(BranchName).$(SwapExtension)</CopyConfigSource>
      </PropertyGroup>
      <PropertyGroup Condition="'$(BranchName)' != '' AND !Exists('$(MSBuildThisFileDirectory)$(SwapFileName).$(BranchName).$(SwapExtension)')">
        <!-- For "dotnet build|publish -c Debug /p:BranchName=Xxxx" where xxx.BranchName.config NOT exists -->
        <Fallback>Development</Fallback>
        <CopyConfigMessage>Detected BranchName parameter, $(SwapFileName).$(BranchName).$(SwapExtension) missing! Copy $(SwapFileName).$(Fallback).$(SwapExtension) to $(SwapFileName).$(SwapExtension)</CopyConfigMessage>
        <CopyConfigSource>$(MSBuildThisFileDirectory)$(SwapFileName).$(Fallback).$(SwapExtension)</CopyConfigSource>
      </PropertyGroup>
      <PropertyGroup Condition="'$(BranchName)' == '' AND Exists('$(MSBuildThisFileDirectory)$(SwapFileName).$(Configuration).$(SwapExtension)')">
        <!-- For dotnet build|publish without /p:BranchName=Xxxx -->
        <CopyConfigMessage>Missing BranchName parameter! Copy $(SwapFileName).$(Configuration).$(SwapExtension) to $(SwapFileName).$(SwapExtension)</CopyConfigMessage>
        <CopyConfigSource>$(MSBuildThisFileDirectory)$(SwapFileName).$(Configuration).$(SwapExtension)</CopyConfigSource>
      </PropertyGroup>
      <Message Importance="High" Text="@SwapConfig@ [$(SwapFileName).$(SwapExtension)] $(CopyConfigMessage)" />
      <Message Importance="High" Text="* CopyConfigSource           : $(CopyConfigSource)" />
      <Message Importance="High" Text="* CopyConfigDestination      : $(CopyConfigDestination)" />
      <Copy SourceFiles="$(CopyConfigSource)" DestinationFiles="$(CopyConfigDestination)" />
    </Target>

Then just call it from any Target you want. CallTargets or MSBuild task is simple enough. If you want to pass parameter or call multiple time, then use MSBuild.

    <Target Name="BeforeBuildHeader" BeforeTargets="Build">
      <Message Importance="High" Text="======================================================" />
      <Message Importance="High" Text="[Before Build Custom Task]" />
      <Message Importance="High" Text="* MSBuildRuntimeType         : $(MSBuildRuntimeType)" />
      <Message Importance="High" Text="* MSBuildThisFileDirectory   : $(MSBuildThisFileDirectory)" />
      <Message Importance="High" Text="* Configuration              : $(Configuration)" />
      <Message Importance="High" Text="* BranchName                 : $(BranchName)" />
      <MSBuild Projects="$(MsBuildThisFile)" Targets="SwapConfig" Properties="SwapFileName=app;SwapExtension=config;SwapDestinationDir=$(MSBuildThisFileDirectory)"/>
    </Target>
@guitarrapc
Copy link
Author

Picture store

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment