Tip: MSBuild support the easy way

Uniwares_ASUniwares_AS Posts: 168
edited September 15, 2010 10:00AM in SmartAssembly 5
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:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
	<UsingTask TaskName="SmartAssembly.MSBuild.Tasks.Build" AssemblyName="SmartAssembly.MSBuild.Tasks, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57" />
	<Target Name="BeforeBuild" Condition=" '$(Configuration)' == 'Release' ">
		<CreateProperty Value="true">
			<Output TaskParameter="Value" PropertyName="RunSmartAssembly" />
		</CreateProperty>
	</Target>
	<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" />
	</Target>
</Project>

In your project file you add just one single line:
<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?

cheers

Comments

  • Nice tip, I know that the SQL Source Control team in red gate do something very similar. Stickied.
    Alex
    Developer,
    Red Gate .NET Tools
  • Very nice post!

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

    Thanks a lot indeed!

    Ricardo
  • Yes, you will still have to have a project for each project/dependency. There is currently no way to automate this.
  • 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.
Sign In or Register to comment.