Execute bat file in the build event got the error

2019-08-23 06:17发布

问题:

In our solution we have more than 150 projects, I need build one of project before others, but I did not want to set project reference or project dependencies for more than 100 projects one by one. This is a waste of time. So I ask following question before this one:

Set build order without using project reference

And as @Leo Liu suggested that I could use batch file to build the project first, it is should be a valid workaround. As optimization, I want set this bat file in the build event of that project so that I only need to build that project, do not need to manually run the bat file from outside.

However, when I build that project with below build event:

call "$(ProjectDir)\MyTest.bat"

I got this error:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\Microsoft.Common.CurrentVersion.targets(5074,5): error MSB3073: The command "call "C:\Users\Myname\Source\repos\MysolutionName\GenerateFile\MyTest.bat"" exited with code 1. [C:\Users\\Source\repos\MysolutionName\GenerateFile\GenerateFile.csproj]

Update: The scripts in the bat file is:

@echo OFF 
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat"
echo "Starting Build for all Projects with proposed changes"
MSBuild.exe "C:\Users\MyName\Source\repos\MySolutionName\MySolutionName.sln"
pause
echo "All builds completed." 

Any suggestion? Thanks in advance.

回答1:

Execute bat file in the build event got the error “error MSB3073: The command ”call “MyTest.bat” exited with code 1."

According to your question, I created a test sample and got more than 30 errors without any other detailed info:

error MSB3073: The command "call "D:\TestSample.bat"" exited with code 1.

Further study found the reason for those errors, that because you build the solution in the bat file, then you set this bat file in the build event of one project in the solution, which called in the bat file.

So an infinite loop will generated, that is the reason why we got more than 30 errors for one command line in the build event.

To resolve this issue, we should not build the specify project when we build the solution with bat file, so you could open Configuration Manager, uncheck the build checkbox for generate project:

With this setting, this project will be ignore when you build the solution with that bat file.