Search code examples
c#.nettemplatesmsbuild.net-8.0

How to make a non-buildable .Net template?


I developed a .NET template, but I don't want it to be a buildable project. When I open it in Visual Studio Code or Visual Studio, it shows errors, and if I try to build it, I encounter many more errors. The build errors are due to the various conditions we provided for each option. The build errors are fine in the template, when we create a new project using the template they will not occur in the project.

Since I don't have a NuGet account, I need to share the code with my colleagues. To avoid giving a bad impression and to prevent follow-up questions about the build errors, I want to make the project non-buildable. I've attempted some changes, but they haven't worked. How can I achieve this?

I tried with these code - Core layer

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
   
  <Target Name="ValidateCommandLine" BeforeTargets="Build">
        <Error Text="Direct build is not allowed." />
  </Target>

  <ItemGroup>
     <PackageReference Include="Auth0.ManagementApi" Condition="'$(Auth0Enable)' == true" Version="7.26.2" />
     <PackageReference Include="SendGrid" Condition="'$(SendGridEnable)' == true" Version="9.29.3" />
  </ItemGroup>
</Project>

API layer

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
      <GenerateDocumentationFile>True</GenerateDocumentationFile>
  </PropertyGroup>
    
  <Target Name="ValidateCommandLine" BeforeTargets="Build">
        <Error Text="Direct build is not allowed." />
  </Target> 

  <ItemGroup>
    <PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
  </ItemGroup>

</Project>

Solution

  • Please see my comment on the question: even though the problem looks intricate, I don't think it should be solved. However, out of curiosity, I've quickly found a solution so you can try it.

    Add the file Directory.Build.targets (if you still don't have it). Its content can be:

    <Project>
      <Target Name="Build"/>
    </Project>
    

    That's all. After that, I added some bugs in the code and started the build using dotnet build at the location of the solution. The build shows:

    Determining projects to restore...
    <... List of projects... >
    
    Build succeeded.
    0 Warning(s)
    0 Error(s)
    

    The same thing happens if you build with dotnet build <project-filename>.

    This process does not even create the output directory, but it does create some intermediate files. You may want to redirect them somewhere in a single directory where you can easily discard them. To achieve that, you have to define the property <BaseIntermediateOutputPath> in the file Directory.Build.props or a project included in this file. It could be as simple as

    <BaseIntermediateOutputPath>$(MSBuildThisFileDirectory).intermediateOutput</BaseIntermediateOutputPath>