Compile two projects in one solution using MSBuild executable

时间:2016-10-20 19:20:11

标签: c# batch-file msbuild

Well, for some reason I can't compile two projects inside a solution, but one yes. In fact, now, when I start a batch file with following code it compiles one of two projects.

@echo off

::Use this in case, you haven't VS installed, or you wan't to open it!
::Maybe you need to install this, if you don't have VS 2015 or MSBuild packages: https://www.microsoft.com/en-us/download/details.aspx?id=48159

if "%MAIN_PATH%" == "" ( for %%a in ("%~dp0..") do set "MAIN_PATH=%%~fa" )
if "%REF_PATH%" == "" ( set "REF_PATH=%MAIN_PATH%\Assemblies" )

set MSBuildEmitSolution=1
msbuild "%MAIN_PATH%\Lerp2API.sln" /pp "/p:ReferencePath=%REF_PATH%" /p:Platform="Any CPU" /p:OutputPath=../Build /p:Configuration=Debug

I made this googling a little bit...

My folders:

main folder

This is the main folder where the solution is.

compile folder

This folder has the batch file I put before. And the executable of MSBuild (.NET 3.0), and its dependencies.

unity references folder

References folder, this folder contains references from Unity, needed in the compilation.

main project folder

This folder has the main project, this compiles it.

editor project folder

This folder has the part of the project that is in the editor, the folder is inside the Editor folder that you see before in the Main Project Folder.

And, when everything gets compiles this happens:

build folder

output path

This folder is the folder where the project points as you can see in the OutputPath.

The important generated files there are the Lerp2API.dll pdb and xml files. The other 3 files are only references that get copied when the compilation finishes (I want to know also how to avoid this!)

And this is the part builded from the Editor, the one that only gets compilated by using Visual Studio Build Option.

build editor path

So, my problems here, are two, the first is that I don't know how to avoid that the references get copied! And the second is that I don't know what I exaclty done before, so, if someone can explain me, what I did and how can I compile the two I one execution I will be so grateful.

Thanks!

1 个答案:

答案 0 :(得分:0)

我已经修好了!这是一个相对文件夹路径的简单问题,放置两个命令而不是一个修复编辑项目的路径我解决了它...

我没有意识到dll正在编译其他文件夹^^'

@echo off

::Use this in case, you haven't VS installed, or you wan't to open it!
::Maybe you need to install this, if you don't have VS 2015 or MSBuild packages: https://www.microsoft.com/en-us/download/details.aspx?id=48159

if "%MAIN_PATH%" == "" ( for %%a in ("%~dp0..") do set "MAIN_PATH=%%~fa" )
if "%REF_PATH%" == "" ( set "REF_PATH=%MAIN_PATH%\Assemblies" )

set MSBuildEmitSolution=1
"%~dp0\msbuild.exe" "%MAIN_PATH%\Lerp2API.sln" /pp /t:Lerp2API "/p:ReferencePath=%REF_PATH%" /p:Platform="Any CPU" /p:OutputPath=../Build /p:Configuration=Debug

::Fixing what @stijn said to me! (I only copied generated references, so, in theory there wouldn't be any problem, but I know only a case, that maybe can cause troubles, like for example, when the first project needs something from the second project, the problem there is the order of compilation, by this reason I will to catch this problems and solve it in the same code)

if exist "%MAIN_PATH%\Build\Lerp2API.dll" ( xcopy /s /z /y "%MAIN_PATH%\Build\Lerp2API.dll" "%MAIN_PATH%\Assemblies\Lerp2API.dll" )
if exist "%MAIN_PATH%\Build\Editor\Lerp2APIEditor.dll" ( xcopy /s /z /y "%MAIN_PATH%\Build\Editor\Lerp2APIEditor.dll" "%MAIN_PATH%\Assemblies\Lerp2APIEditor.dll" )

::Fixing the other thing I said by deleting unneeded files!

if exist "%MAIN_PATH%\Build\Lerp2APIEditor.dll" ( del "%MAIN_PATH%\Build\Lerp2APIEditor.dll" )
if exist "%MAIN_PATH%\Build\Lerp2APIEditor.pdb" ( del "%MAIN_PATH%\Build\Lerp2APIEditor.pdb" )
if exist "%MAIN_PATH%\Build\Lerp2APIEditor.xml" ( del "%MAIN_PATH%\Build\Lerp2APIEditor.xml" )
if exist "%MAIN_PATH%\Build\UnityEditor.dll" ( del "%MAIN_PATH%\Build\UnityEditor.dll" )
if exist "%MAIN_PATH%\Build\UnityEngine.dll" ( del "%MAIN_PATH%\Build\UnityEngine.dll" )
if exist "%MAIN_PATH%\Build\UnityEngine.UI.dll" ( del "%MAIN_PATH%\Build\UnityEngine.UI.dll" )

::Fixing the third problem, that was caused by the hierarchy of the folders, by separating the two compilation and editing the build path all the problems has been solved!

"%~dp0\msbuild.exe" "%MAIN_PATH%\Project\Editor\Editor.csproj" "/p:ReferencePath=%REF_PATH%" /p:Platform="Any CPU" /p:OutputPath=../../Build/Editor /p:Configuration=Debug

if exist "%MAIN_PATH%\Build\Editor\Lerp2API.dll" ( del "%MAIN_PATH%\Build\Editor\Lerp2API.dll" )
if exist "%MAIN_PATH%\Build\Editor\Lerp2API.pdb" ( del "%MAIN_PATH%\Build\Editor\Lerp2API.pdb" )
if exist "%MAIN_PATH%\Build\Editor\Lerp2API.xml" ( del "%MAIN_PATH%\Build\Editor\Lerp2API.xml" )
if exist "%MAIN_PATH%\Build\Editor\UnityEditor.dll" ( del "%MAIN_PATH%\Build\Editor\UnityEditor.dll" )
if exist "%MAIN_PATH%\Build\Editor\UnityEngine.dll" ( del "%MAIN_PATH%\Build\Editor\UnityEngine.dll" )
if exist "%MAIN_PATH%\Build\Editor\UnityEngine.UI.dll" ( del "%MAIN_PATH%\Build\Editor\UnityEngine.UI.dll" )

享受它!