MSBuild command line output for individual folders

I currently have the following MSBuild command:

msbuild /t:Build /p:Configuration=Release /p:OutputPath=C:\MySolutionOutput\ MySolution.sln 

In compilation, however, I have several projects in my solution. When I do the assembly in this way with the solution file, it copies all the outputs of the project to the same directory, and the output from Project2 overwrites the output of Project1 (or no matter what order it is built in).

Instead, I want MSBuild to put each project in a subfolder of the project name.

 msbuild /t:Build /p:Configuration=Release /p:OutputPath=C:\MySolutionOutput\$(ProjectName) MySolution.sln 

However, the syntax for $(ProjectName) does not work, because it literally creates a folder named $ (project_name).

How can I create an MSBuild solution for outputting each project to the OutputPath subdirectory, preferably without creating MSBuild xml files or for creating each project separately?

To illustrate this, my projects are as follows.

 + Project 1 + File1.txt + File2.txt + ReadMe.txt + Project 2 + File3.txt + File4.txt + ReadMe.txt 

What are my build outputs

 C:\MySolutionOutput\File1.txt C:\MySolutionOutput\File2.txt C:\MySolutionOutput\File3.txt C:\MySolutionOutput\File4.txt C:\MySolutionOutput\ReadMe.txt ^-- (this is the 2nd readme, project 1 readme gets overwritten) 

What I want

 C:\MySolutionOutput\Project 1\File1.txt C:\MySolutionOutput\Project 1\File2.txt C:\MySolutionOutput\Project 1\ReadMe.txt C:\MySolutionOutput\Project 2\File3.txt C:\MySolutionOutput\Project 2\File4.txt C:\MySolutionOutput\Project 2\ReadMe.txt 
+6
source share
1 answer

When creating a solution using msbuild, it internally converts the solution to the msbuild XML file, which builds all the projects and gives them properties such as OutputPath, and afaik cannot interfere with how it behaves. One possible solution requires you to write msbuild files yourself, but the modifications are really minor. You do not indicate what type of project you are using, but the principle is the same for any type, and here is a sample for C # projects:

  • create a shared file that allows you to redefine OutputPath the way you want, but does nothing unless specifically stated, to preserve the existing behavior if necessary
  • import this file into each project
  • specify a custom output directory on the command line

msbuild file, say outputpath.props :

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <OutputPath Condition="'$(OutputPathBaseDir)' != ''">$(OutputPathBaseDir)\$(MSBuildProjectName)\</OutputPath> </PropertyGroup> </Project> 

import in each project; here I use a relative path, but you can reference, for example, $ (SolutionDir), etc .; for C # projects, insert this line right before the shown

 <Import Project="..\outputpath.props" /> <!--the below is an existing line in every c# project--> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 

now build:

 msbuild mysolution.sln /p:OutputPathBaseDir=C:\MySolutionOutput 
+6
source

Source: https://habr.com/ru/post/973512/


All Articles