Tip: MSBuild support the easy way

.NET obfuscator and automated error reporting

Tip: MSBuild support the easy way

Postby Uniwares_AS » Thu Jun 24, 2010 4:52 pm

There is a nice description on how to use SA with MSBuild on the red-gate site (http://www.red-gate.com/supportcenter/Content.aspx?p=SmartAssembly&c=SmartAssembly/help/5.0/SA_UsingSmartAssemblyWithMSBuild.htm&toc=SmartAssembly/help/5.0/toc1032253.htm) but there is an much easier way which comes handy when you have multiple projects or simply no mood to add all the stuff to your project file.

Create a file called "SmartAssembly.targets" in the location of your preference (I'd suggest the SA installation folder or a common path to your projects). In this file you paste the following:
Code: Select all
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <UsingTask TaskName="SmartAssembly.MSBuild.Tasks.Build" AssemblyName="SmartAssembly.MSBuild.Tasks, Version=, Culture=neutral, PublicKeyToken=7f465a1c156d4d57" />
   <Target Name="BeforeBuild" Condition=" '$(Configuration)' == 'Release' ">
      <CreateProperty Value="true">
         <Output TaskParameter="Value" PropertyName="RunSmartAssembly" />
   <Target Name="AfterCompile" Condition=" '$(RunSmartAssembly)' != '' ">
      <Copy SourceFiles=".\\obj\\Release\\$(TargetFileName)" DestinationFiles=".\\obj\\Release\\$(TargetName).temp$(TargetExt)" />
      <SmartAssembly.MSBuild.Tasks.Build ProjectFile="$(SolutionDir)Build\\$(TargetName).{sa}proj" OverwriteAssembly="True" />

In your project file you add just one single line:
Code: Select all
<Import Project="$(YourPathToSA)\\SmartAssembly.targets" />

instead of adding all the code mentioned in the link above. Take care to store your SA project files in the $(SolutionDir)\\Build\\ folder with the exact name of the target assembly. If your assembly is "MyTestAssembly.dll" then the sa project should be named "MyTestAssembly.{sa}proj"

And thats it already. With that single line in all your project files, all your release builds will be protected by SA.

One note: I added a <copy> step to make your life easier if you ever need to change the sa-project settings in the sa GUI again. It will make a copy of the unprotected assembly into the same obj\\release folder. So you can simply rename it, run the GUI and make your changes and then rebuild. If you dont need this, just remove the <copy> step.

One could now improve this to include MarkAsReleased depending on the $(Configuration), which would allow you to make an additional "Release" config for you projects for the "real" release which will be shipped. No need to mark all release builds with MarkAsReleased, right?

Posts: 132
Joined: Thu Oct 11, 2007 2:29 pm

Postby Alex.Davies » Fri Jun 25, 2010 3:01 pm

Nice tip, I know that the SQL Source Control team in red gate do something very similar. Stickied.
Red Gate .NET Tools
Posts: 335
Joined: Tue Dec 02, 2008 11:23 am
Location: Cambridge, uk

Postby rcd.guerra » Wed Sep 15, 2010 9:39 am

Very nice post!

With this approach i still need to have a .saproj file for each project or dependency right ?

Thanks a lot indeed!

Posts: 10
Joined: Mon Sep 13, 2010 4:43 pm

Postby Linda Hawksworth » Wed Sep 15, 2010 1:54 pm

Yes, you will still have to have a project for each project/dependency. There is currently no way to automate this.
Linda Hawksworth
Posts: 192
Joined: Mon Jun 25, 2007 10:32 am

Postby Uniwares_AS » Wed Sep 15, 2010 2:00 pm

Well Linda, depending on your needs there actually IS a way to make it easier.

For example, I've got a set of assemblies which I pass through SA, around 15 dll's, some depending on others, some standalone. But I use the same project file for all of them. How? Hand-editing the project file including ALL dependencies into the SA project. SA chooses which of the dependencies are needed and ignores the rest. Less than optimal, but it works and makes things easier.
Posts: 132
Joined: Thu Oct 11, 2007 2:29 pm

Return to SmartAssembly 5

Who is online

Users browsing this forum: No registered users and 0 guests