MSBuild: Build and Publish using a Build.Targets File

237 Views Asked by At

I have a solution in Visual Studio that contains two projects: ProjectA.csproj ProjectB.csproj

At the solution level I have a Buid.Targets file that describes the steps for the build process. ProjectB is special and needs to be published as an application for both Window and Linux. To do that I have set up the Publish profiles in the project for both Windows and Linux.

My question is it possible to use the MSBuild .targets file at the solution level to generate a build that does the following: Build ProjectA Publish ProjectB for both Windows and Linux

I'm new to MSBuild, but would like to try and define these steps in the .targets file I have at the solution level if that is even possible?

Thanks

I have set up the publish profiles but am not seeing a way to "call" to those in the Build.Targets file that I have at the solution level.

1

There are 1 best solutions below

0
On

For example, on my side, I have a solution with project A and project B:

enter image description here

You can see that there has a Build.targets file.

This is the content of the Build.targets file on my side:

<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  
  <PropertyGroup>
    <WindowsPublishProfile>Properties\PublishProfiles\Windows.pubxml</WindowsPublishProfile>
    <LinuxPublishProfile>Properties\PublishProfiles\Linux.pubxml</LinuxPublishProfile>
  </PropertyGroup>

  <Target Name="BuildAndPublish">
    
    <!-- Build ProjectA -->
    <MSBuild Projects="ProjectA\ProjectA.csproj" Targets="Build" />

    <!-- Publish ProjectB -->
    <!-- For Windows -->
    <MSBuild Projects="ProjectB\ProjectB.csproj" 
             Targets="Publish" 
             Properties="PublishProfile=$(WindowsPublishProfile)" />

    <!-- For Linux -->
    <MSBuild Projects="ProjectB\ProjectB.csproj" 
             Targets="Publish" 
             Properties="PublishProfile=$(LinuxPublishProfile)" />
  </Target>

</Project>

And here are the content of the Windows.pubxml and Linux.pubxml on myside:

Windows.pubxml:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->

<Project>
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>bin\Release\net6.0\publish\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <PublishDir>bin\Release\net6.0\windows-x64\publish\</PublishDir>
  </PropertyGroup>
</Project>

Linux.pubxml:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>bin\Release\net6.0\publish\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <PublishDir>bin\Release\net6.0\linux-x64\publish\</PublishDir>
  </PropertyGroup>
</Project>

This is the structure on my side:

enter image description here

After the above steps, you can run the msbuild command:

msbuild /t:BuildAndPublish Build.targets

You will get the result:

enter image description here

You can see whether the above is what you want, if I misunderstand something, please feel free to let me know.