2

I am creating a .net web application and have some build tasks eg msbuild copy task. I add this by editing the project file for the application and adding the task.

While this works ok, is there any way I can use an external xml file to the project file and have my build tasks in this with the main project file referencing it? I would much prefer this as I wouldn't then have to edit the main project file and there is separation between the project file and the build tasks.

amateur
  • 43,371
  • 65
  • 192
  • 320
  • possible duplicate of [MSBuild task to execute an external MSBuild file](http://stackoverflow.com/questions/1501930/msbuild-task-to-execute-an-external-msbuild-file) – Dour High Arch Jan 10 '11 at 19:31

2 Answers2

1

You can call msbuild.exe, targetting your separate build file, in a pre-build or post-build event.

Chris Dickson
  • 11,964
  • 1
  • 39
  • 60
1

You can simply reference any external project or target file by adding an import to your main project file:

...
<!-- this is the default import for (c#) web project files -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />

<!-- import your custom project/target file here -->
<Import Project="MyCustom.targets" Condition="Exists('MyCustom.targets')" />
...

Adding the Condition will allow to build your main project even if your custom project/target file is missing because the build is run in a different environment.

I'm using this approach to run FxCop and StyleCop targets on my local machine but the same main project file can be built without any changes in my staging environment.


Update

Your comment suggests that you actually are looking rather for a solution that should work the other way round: You want to execute some steps before and after building your project without modifying the project configuration itself.

In that case the best way is to create your custom project to call the build of your web project. This could look something like this:

<Project DefaultTargets="MyTargetAfterBuild">
    <!-- some Project attributes omitted for readability -->

    <Target Name="MyTargetBeforeBuild" ContinueOnError="false">
        <Exec Command="svn export" />
    </Target>

    <Target Name="Build" DependsOnTargets="MyTargetBeforeBuild">
        <MSBuild Projects="MyWebProject.csproj" Targets="Build" Properties="Configuration=Release" >
        </MSBuild>
    </Target>

    <Target Name="MyTargetAfterBuild" DependsOnTargets="Build">
        <Exec Command="powershell.exe .\MyCustomScript.ps1" />
    </Target>

</Project>

You might be interested in this answer on a similar scenario with a more detailed example.

Community
  • 1
  • 1
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Thanks for your help with this. Can I create an external xml file with similar set up to the project file ie. etc. From here I can reference my build tasks, supply params etc. – amateur Jan 11 '11 at 23:49
  • @Niall: Yes you can create an external xml file with any `Target` and `Task` that MSBuild (or extensions) offer ... but it sounds like your're more after something which would work better with a different approach. See my updated answer for details. – Filburt Jan 12 '11 at 08:53