Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a common post build event used by multiple csproj projects

I have several Visual Studio 2010 C# projects. I'd like to have them all have the same post build event. Basically I want them to copy their output somewhere. I think I can do this using an <import> statement in each csproj file, but I can't seem to figure out the properties/targets and such that I need in the imported file. Do you have any suggestions?

EDIT: I've tried the following but can't get it to work. This is what the Imports.props file looks like:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="AfterBuild">
        <Message Text="Here I come to save the day!" />
    </Target>
</Project>

I then Include it in the csproj files:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="Imports.props" />
    ...

I've also tried this as Imports.props:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <PostBuildEvent>echo Here I come to save the day!</PostBuildEvent>
    </PropertyGroup>
</Project>
like image 738
Charles Avatar asked Mar 01 '11 21:03

Charles


People also ask

How do I create a post build event in Visual Studio?

In the Post-build event command line box, specify the syntax of the build event. Add a call statement before all post-build commands that run . bat files. For example, call C:\MyFile.

What are post build events?

Pre/Post build events are useful when we wish to perform some operation before/after a project is built. These operations are nothing but the Shell commands being used from the command line. A build event can be formed using a single, multiple, or conditional commands.

How do I create a build property in Microsoft?

MSBuild lets you set properties on the command line by using the -property (or -p) switch. These global property values override property values that are set in the project file. This includes environment properties, but does not include reserved properties, which cannot be changed.

Which of the following build events will occur if the error occurs before the linking phase?

If an error occurs in the build, the post-build event does not occur; if the error occurs before the linking phase, neither the pre-link nor the post-build event occurs. Additionally, if no files need to be linked, the pre-link event does not occur.


2 Answers

Make sure that you import your targets file after the Microsoft.Common.targets import, as that file defines an empty AfterBuild target, which would override the definition in your targets file.

like image 146
radical Avatar answered Sep 28 '22 05:09

radical


<import> in each projectfile and a <AfterBuild> target in your common build file.

CommonTargets http://msdn.microsoft.com/en-us/library/ms171464.aspx

TargetOrder http://msdn.microsoft.com/en-us/library/ms171462.aspx

like image 21
rene Avatar answered Sep 28 '22 04:09

rene