MSBuild custom (obfuscate) step between build and publish

1.6k Views Asked by At

I have a web application (WebAPI web service) that is building on my CI server. It builds and publishes fine using VS2013, a publish to file system publish profile (*.pubxml) and the below command from the CI server task.

MSBuild %ProjectPath%/MyApp.sln /t:Clean,Build /p:Configuration=Release;DeployOnBuild=True;PublishProfile=TAGBIN;VisualStudioVersion=12.0

However I also need to obfuscate the software, the obfuscator (Red Gate Smart Assembly), is invoked via the below xml placed inside the csproj file.

<UsingTask TaskName="SmartAssembly.MSBuild.Tasks.Build" AssemblyName="SmartAssembly.MSBuild.Tasks, Version=6.0.0.0,&#xD;&#xA;Culture=neutral, PublicKeyToken=7f465a1c156d4d57" />
<Target Name="PostBuildEvent" Condition=" '$(Configuration)' == 'Release' ">
    <SmartAssembly.MSBuild.Tasks.Build ProjectFile="MyApp.saproj" />
</Target>

This appears to build and publish fine, however upon inspecting the command line output I have noticed that the publish occurs before the obfuscation.

Obviously obfuscation needs to happen after the build, but before the publish, is this possible?

I have tried a few things, including editing the *.pub.xml file to call obfuscate via a 'BuildDependsOn' node, but this causes the obfuscation to run before the build.

<BuildDependsOn>        
    Obfuscate;
    $(BuildDependsOn);
</BuildDependsOn>

It just does not seem possible to decouple the build from the publish, the only option I have at the moment is to build it twice, where the second publish picks up the obfuscated files from the first build.

1

There are 1 best solutions below

0
seva titov On

In MSBuild 4.0 and above you can use BeforeTargets and AfterTargets to control target build order.

Example below introduces new build step that executes after build but before publishing:

<Target Name="MyNewBuildStep" AfterTargets="CoreBuild" BeforeTargets="Publish">
    <Message Text="Executing new build step" />
</Target>