Delphi XE custom build target is always disabled

Solution 1:

Delphi generates the entire dproj content itself and this custom import will always be deleted.

You could write your own msbuild xml files but the dproj belongs to Delphi.

Unless you have source code or are willing to monkey patch the ide you cant do that.

If you really want a flexible xml way to build delphi projects and create targets galore try want or want vnext (my fork on bitbucket)

Solution 2:

I would include the targets file manually and build externally using MSBuild rather than from the IDE, because when compiling from the IDE its a bit messy to know which configuration and target have you applied (is the one clicked on the project? or the one from enabled target? you dont get any visual hint that a custom target is enabled).

I usually do it before the Import Project="$(BDS)\Bin\CodeGear.Delphi.Targets" so they will not show on the IDE (they exist, but are hidden to developers).

For example my Delphi XE4 projects end with:

    <Import Project="..\BuildServer.Targets"/>
    <Import Project="$(BDS)\Bin\CodeGear.Delphi.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')"/>
    <Import Project="$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj" Condition="Exists('$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj')"/>
</Project>

My .targets file defines a custom "PropertyGroup" and "Target" with a condition, so they will only apply when called from MSBuild:

  <PropertyGroup  Condition="'$(Config)'=='CustomConfig'">
    <DCC_Define>RELEASE;$(DCC_Define)</DCC_Define>
    ...
  </PropertyGroup>
  <Target Name="DisplayProjectInfo">
    <Message Text="Project File Name = $(MSBuildProjectFile)"/>
    <Message Text="Version = $(VerInfo_Keys)"/>
    <Message Text="OutputDir = $(DCC_ExeOutput)"/>
  </Target>
  <Target Name="CustomTarget" Condition="'$(Config)'=='CustomConfig'">
  <MSBuild Projects="$(MSBuildProjectFile)" Targets="Clean" />
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="Build" />
    <CallTarget Targets="DisplayProjectInfo"/>
  </Target>

Then compile it with:

msbuild /t:CustomTarget /p:config=CustomConfig poject.dproj

Using this approach lets you customize build targets to make sure every application gets the correct settings without being affected by changes made by anyone.