9
votes

I'm trying to set up a MSBuild file which will invoke another MSBuild file and I'm wondering what's the best way to achieve this.

We're using it in the scenario of where a build server downloads a MSBuild file which then depending on the parameters it'll execute the appropriate 2nd file.

I know I can just use the <Exec Command="msbuild.exe ..." /> task, but that seems to be a bit of a hacky way to do it.

Is there an easier way to use MSBuild to execute another MSBuild file?

4

4 Answers

13
votes

You can use the MSBuild task to build a target in another MSBuild project.

You can also put the target in an external .targets file that is imported by both MSBuild projects and use the CallTarget task to build it.

Update: To execute an external command, use the Exec task.

7
votes

It's not just prettier to use the <MSBuild> task rather than <Exec Command="msbuild.exe .."/>. It means that the single MSBuild process knows about all the projects being built, and there's no collisions; a particular project can't be being built by two threads at once. That's usually vital if you are building with "/m". There's some other advantages too, such as there being one set of loggers for the whole build.

2
votes

Response to Adam Oren's comment: Here's an example to execute an external file - in this case, it launches nunit. The %22 is for escaping, it means " so you can have spaces in your executable path.

<Exec Command="%22$(NUnit_Install_Directory)bin\net-2.0\nunit-console.exe%22 
/noshadow @(TestableAssemblies, ' ') /xml 
 $(BuildFilesPath)\NCover-NUnit-Results.xml" /> 

You don't need to use variables like I did, but I find that it makes your scripts more portable. $(NUnit_Install_Directory) is defined as

<PropertyGroup> 
   <NUnit_Install_Directory>c:\Program Files\NUnit 2.5.3\</NUnit_Install_Directory> 

0
votes
<Target Name="TA">
    <CallTarget Targets="TB"/>
</Target>

<Target Name="TB">
    <Message Text="TB..." />
</Target>