jaffa
jaffa

Reputation: 27350

Use different pre-build events for different build configurations in Visual Studio

Is it possible to use different pre-build events for different build configurations in Visual Studio? For example, I'd like both a release configuration for a beta & live system and have the relevant app.[type].config get copied to app.config before it is compiled.

At the moment the configuration settings are baked into the .settings file, using the settings from the default app.config file.

Upvotes: 6

Views: 4988

Answers (3)

Ahmed Kamal
Ahmed Kamal

Reputation: 85

To use different build events for different configuration in visual studio, open the cs proj file of the project. in the pre build section

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Condition="'$(Configuration)'=='Release'" Command="echo Release" />
    <Exec Condition="'$(Configuration)'=='Debug'" Command="echo Debug" />
  </Target>

The command in "Command" parameter will only execute if this condition is met.

Upvotes: 2

cheerless bog
cheerless bog

Reputation: 934

Or just put the Condition on your target ... eg.,

Condition="'$(Configuration)' == 'Debug'" 

.. or on your task.

If you're using Visual Studio VB/C# simple post build events, you can hand-edit the project file to put such conditions on the PreBuildEvent/PostBuildEvent property tags; and repeat the tags for Release.

Dan (msbuild dev)

Upvotes: 2

Michael Edenfield
Michael Edenfield

Reputation: 28338

You can do this in a couple of ways, depending on your exact situation:

Option 1: Check the $(ConfigurationName) variable in your pre-build script, like so:

IF EXISTS $(ProjectDir)app.$(ConfigurationName).config 
    COPY $(ProjectDir)app.$(ConfigurationName).config $(ProjectDir)app.config

Option 2: Add a "BeforeCompile" MSBuild target to your project file:

<Target Name="BeforeBuild">
    <!-- MSBuild Script here -->
</Target>

Option 3: Use configuration file transformations; this VSIX plug-in adds the web.config transform features to non-web projects. These are XSLT files that let you rewrite your config files on build (unlike web projects, where it happens on publish.)

Upvotes: 1

Related Questions