Replacing TFSBuild.proj files with TFS 2013 - c#

I'm trying to upgrade our TFS server to 2013. We're currently using 2012, but we've also been clinging on to the upgrade template for dear life. With 2013, we'd like to go to the default template and modify it as little as possible.
The problem comes in when you consider that the default template asks you to add each individual .csproj or .sln file that you would like to build. The nice thing about the tfsbuild.proj files is that not only can you build on the server, but you can check out the entire branch and build everything locally, on the command line, by just passing the tfsbuild.proj file to msbuild.exe. Also, developers can own the tfsbuild.proj file without having write access to change the build definition.
What is the replacement for the TFSBuild.proj file in TFS 2013?
My requirements are:
Clean build configuration.
Can easily build everything locally.
What is the solution to this problem in TFS 2013?

Create a wrapper MSBuild .proj that your TFS build definition uses. We use this technique for NuGet package restore, but it can equally be used to chain together multiple solution files.
For local builds you can use msbuild with that .proj wrapper as the target (I just build the .sln file directly as there is only 1).
.proj file (not suggesting that you should use this exact .proj file, just an example)
<PropertyGroup>
<OutDir Condition=" '$(OutDir)'=='' ">$(MSBuildThisFileDirectory)bin\</OutDir>
<Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
<SourceHome Condition=" '$(SourceHome)'=='' ">$(MSBuildThisFileDirectory)</SourceHome>
<ToolsHome Condition=" '$(ToolsHome)'=='' ">$(MSBuildThisFileDirectory)tools\</ToolsHome>
</PropertyGroup>
<ItemGroup>
<Solution Include="$(SourceHome)*.sln">
<AdditionalProperties>OutDir=$(OutDir);Configuration=$(Configuration)</AdditionalProperties>
</Solution>
</ItemGroup>
<Target Name="RestorePackages">
<Exec Command=""$(ToolsHome)NuGet\NuGet.exe" restore "%(Solution.Identity)"" />
</Target>
<Target Name="Clean">
<MSBuild Targets="Clean"
Projects="#(Solution)" />
</Target>
<Target Name="Build" DependsOnTargets="RestorePackages">
<MSBuild Targets="Build"
Projects="#(Solution)" />
</Target>
<Target Name="Rebuild" DependsOnTargets="RestorePackages">
<MSBuild Targets="Rebuild"
Projects="#(Solution)" />
</Target>

Related

C# - Unable to change ApplicationVersion from MSBuild script

I'm a newbie trying to modify on publish the ApplicationVersion value by AssemblyVersion one on a MS Office AddIn.
Here is my csproj :
<Target Name="SetAssemblyVersionToPublish" AfterTargets="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
When I publish the app from Visual Studio 2015, logs show the good version :
app Version : 4.4.9.0
But in fact it is the ApplicationVersion defined in VS that is used :
<ApplicationVersion>1.0.0.0</ApplicationVersion>
I already tried :
reset all VS settings
disable any vs third party extensions
use safemode
if I publish a WPF app, it works well
Any one would know why I get this change ?
Update
After some test, I found that MSBUILD use a different target file for VSTO : MSBuild\Microsoft\VisualStudio\v14.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets.
It seems to use PublishVersion which is a copy of ApplicationVersion. I don't know why but if I set ApplicationVersion in my target, it does not update the PublishVersion... If I set the publishVersion, I get the good version in my bin folder, but publishing failed (don't copy to publish folder because it still want to get the folder with 1_0_0_0 in bin).
It may be a problem with a bad step which launch my target, but I don't figure when it must be done.
Any help ?
UPDATE 2
It seems to be a bug with VS2015 publishing tool. If I try with msbuild.exe command line, it works well:
On VS I get an error asking for the below folder :
Whereas with the following msbuild command, it works :
MSBuild xxx\ExcelAddIn1\ExcelAddIn1\ExcelAddIn1.csproj /t:clean;publish
Anyone know what can be done as a workaround ?
First of all, I have a question, when I use your code in a winform project and execute publish, I got an error like:
After I test, I found that
ApplicationVersion which you got is illegal three digits instead of four digits, so there is a problem with your method of obtaining AssemblyVersion.
In general, the publish version is under Project Properties(right-click on your project)-->Publish
You can note that it is four digits.
Which proves that you have miss a node on Pattern property.
Suggestion
You should use
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
The whole code are:
<Target Name="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
</Target>
It will reads the AssemblyVersion from the AssemblyInfo.cs file.
In my side, it is
Actually, your method already works and it overwrite the publish version for the app.
I think you have read the first part of the build output log like this:
It is only the default system initial value at the beginning of the build, and it is just displayed there. And then through your custom msbuild script actually has overwritten its value.
Before executing publish, you should delete the publish folder, bin and obj folder, then execute the publish, you can enter the folder to check:
Update 1
I think your csproj file has imported some targets or props file which has other targets overwrite the AfterCompile target. So your method failed.
That is, your method may be overwritten elsewhere.
So I think you should not use AfterCompile as the target name and should name it as another to distinguish between them.
Use this:
<Target Name="SetAssemblyVersionToPublish" AfterTargets="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
</Target>
Or, You can add a file called Directory.Build.targets file on your project folder
and then add my code on it:
<Project>
<Target Name="SetAssemblyVersionToPublish" AfterTargets="AfterCompile">
<ReadLinesFromFile File="Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
</Target>
</Project>
Then, delete bin, obj, publish folder and then republish again to check it.
If the issue still persists, you should try the following suggestions to troubleshoot the issue:
1) Try to reset vs settings by Tools-->Import and Export settings-->Reset all vs settings
2) disable any vs third party extensions under Tools-->Extensions and Updates -->Installed, after that, close VS ,restart your project.
3) you could try devenv /safemode on the Developer Command Prompt for VS2015 to start a pure, initial vs to test your solution.
===============================================
Update 2
The MS Office Excel AddIn project is quite different from the traditional projects. And it is impossible by using this way.
As a workaround, I think you should use msbuild script to publish the project.
1) create a file called PublishExcel.proj
2) add these content on that file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="SetAssemblyVersionToPublish">
<!--you must specify the full path of the AssemblyInfo.cs from your project -->
<ReadLinesFromFile File="C:\Users\xxx\Documents\Visual Studio 2015\Projects\ExcelAddIn1\ExcelAddIn1\Properties\AssemblyInfo.cs">
<Output TaskParameter="Lines" ItemName="AssemblyVersion" />
</ReadLinesFromFile>
<PropertyGroup>
<In>#(AssemblyVersion)</In>
<Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
<Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
<ApplicationVersion>$(Out.Remove(0, 28))</ApplicationVersion>
</PropertyGroup>
<Message Text="app Version : $(ApplicationVersion)" Importance="High" />
<MSBuild Projects="C:\Users\Administrator\Documents\Visual Studio 2015\Projects\ExcelAddIn1\ExcelAddIn1\ExcelAddIn1.csproj" Properties="ApplicationVersion=$(ApplicationVersion)" Targets="Publish">
</MSBuild>
</Target>
</Project>
3) then open Developer Command prompt for VS2015 and then type:
msbuild xxx\xxx\PublishExcel.proj -t:SetAssemblyVersionToPublish
And the publish folder will be under the bin\Debug or Release\app.publish folder.
This function will work for MS Office Excel AddIn project.

How to get nuget restore in TFS build

I can't make it work TFS build. It is nuget restore issue. Nuget is not restoring reference dll files.
Here is belwo my build configuration. Please advise me how I can make this works.
As per this blog post on Nuget's website you can use the command line you mentioned, but it has to be part of a custom target using a Build.proj file.
You need to add a Build.proj and put this as the contents:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutDir Condition=" '$(OutDir)'=='' ">$(MSBuildThisFileDirectory)bin\</OutDir>
<Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
<SourceHome Condition=" '$(SourceHome)'=='' ">$(MSBuildThisFileDirectory)src\</SourceHome>
<ToolsHome Condition=" '$(ToolsHome)'=='' ">$(MSBuildThisFileDirectory)tools\</ToolsHome>
</PropertyGroup>
<ItemGroup>
<Solution Include="$(SourceHome)*.sln">
<AdditionalProperties>OutDir=$(OutDir);Configuration=$(Configuration)</AdditionalProperties>
</Solution>
</ItemGroup>
<Target Name="RestorePackages">
<Exec Command=""$(ToolsHome)NuGet\NuGet.exe" restore "%(Solution.Identity)"" />
</Target>
<Target Name="Clean">
<MSBuild Targets="Clean"
Projects="#(Solution)" />
</Target>
<Target Name="Build" DependsOnTargets="RestorePackages">
<MSBuild Targets="Build"
Projects="#(Solution)" />
</Target>
<Target Name="Rebuild" DependsOnTargets="RestorePackages">
<MSBuild Targets="Rebuild"
Projects="#(Solution)" />
</Target>
</Project>
Alternatively, you could call it from a custom Pre-Build Script.
Or, customise the XAML template and add a Foreach loop to invoke:
nuget.exe restore path\to\solution.sln
on each solution in the build definition.
Here are some steps (described here which was mentioned in Dave's post) you need to follow in order to have these NuGet packages restored during the VSO (TFS) build process.
Add following items to the solution. (Content of the nuget.config and .tfignore file can be found here)
Add one build.proj file under the root path of the solution folder. (Content of the build.proj file can be found here)
Create one folder named tools under the root path of the solution folder. Create NuGet sub-folder under tools folder, download and save nuget.exe under tools\NuGet path.
Check in nuget.config, .tfignore, build.proj and tools\NuGet\nuget.exe into TFS version control.
Modify the build definition to choose to build the build.proj file.
Then you will have NuGet packages restored successfully during the TFS build process.

How do i have a .csproj build a .proj file before building itself?

I have a .csproj file and a .proj file. As part of my .proj file I am generating a file to include in the .csproj, so the .proj needs to run first.
How can this be done. I originally tried to add a project reference as follows:
<ProjectReference Include="..\FileGenerator\FileGenerator.proj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
This however gives me the error:
error MSB4057: The target "GetNativeManifest" does not exist in the project
I then noticed there is a BeforeBuild target in my csproj file.
Can I use this to have the other file be built?
Use MSBuild task to invoke other projects. Example:
<Target Name="BeforeBuild">
<MSBuild Projects="..\FileGenerator\FileGenerator.proj" Targets="Build" />
</Target>
If you need any cleanup done as part of the common Clean target, you can plug in custom cleanup target like this:
<Target Name="FileGeneratorClean" BeforeTargets="Clean">
<MSBuild Projects="..\FileGenerator\FileGenerator.proj" Targets="Clean" />
</Target>

Customizations on MSBuild (like version) for a C# solution

I'm thinking that the final result is going to be "it can't be that easily done", but just seems like it should be. I have a personal project I am working on. I'd hate to have to manually (or even in script) change versions, company, copyright, and all that on ALL the assembly.cs files and would like all that to be either in a script or in a file I can change (so the script stays the same mostly) when I want to update the version. But it seems like MSBuild is mostly a "build as is specified in Visual Studio". I'd just hate to have all that history of these files where I change just the version and possibly even make a mistake as this project will continue to get bigger and bigger. I'd like to just be able to add a new project to Visual studio and have whatever command line in my powershell script just say "compile this, but give it this company name and this file version instead of whatever is listed in the code file".
Google has NOT proven fruitful in this. I've even found it difficult to build my files to a specific folder. I've had to so far make sure all my projects are 2 folders deep and was able to say to build them at ....\, but I would like to be able to change that randomly if I like and have them built elsewhere if I so desire.
Is MSBuild perhaps not the way to go? Is there someway else to build visual studio that would be better from command line? Eventually I also want to auto build the install with wix and be able to match its version with the binary versions.
thank you
Since csproj is xml, you can use XmlUpdate "helpers" to modify the values inside the csproj file before you do your build.
For other files, you can use some other Tasks to do the job.
Here is one helpful target:
http://msbuildtasks.tigris.org/ and/or https://github.com/loresoft/msbuildtasks has the ( FileUpdate (and SvnVersion task if that is your Source-Control) ) tasks.
<Target Name="BeforeBuild_VersionTagIt_Target">
<ItemGroup>
<AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
</ItemGroup>
<!--
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
<Output TaskParameter="Revision" PropertyName="MyRevision" />
</SvnVersion>
-->
<PropertyGroup>
<MyRevision>9999</MyRevision>
</PropertyGroup>
<FileUpdate Files="#(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="AssemblyFileVersion("$1.$2.$3.$(MyRevision)" />
</Target>
Below is an example of manipulating the csproj(xml).
How to add a linked file to a csproj file with MSBuild. (3.5 Framework)
But basically, when you build, you can put all the repetative stuff in a msbuild definition file (usually with the extension .proj or .msbuild)...and call msbuild.exe MyFile.proj.
Inside the .proj file, you will reference your .sln file.
For example:
$(WorkingCheckout) would be a variable (not defined here)...that has the directory where you got a copy of hte code from your source-control.
<Target Name="BuildIt" >
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"></Output>
</MSBuild>
<Message Text="BuildItUp completed" />
</Target>
So below is the more complete example.
You would save this as "MyBuild.proj" and then call
"msbuild.exe" "MyBuild.proj".
Start .proj code. (Note, I did not import the libraries for the FileUpdate Task)
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">
<PropertyGroup>
<!-- Always declare some kind of "base directory" and then work off of that in the majority of cases -->
<WorkingCheckout>.</WorkingCheckout>
</PropertyGroup>
<Target Name="AllTargetsWrapped">
<CallTarget Targets="BeforeBuild_VersionTagIt_Target" />
<CallTarget Targets="BuildItUp" />
</Target>
<Target Name="BuildItUp" >
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"></Output>
</MSBuild>
<Message Text="BuildItUp completed" />
</Target>
<Target Name="BeforeBuild_VersionTagIt_Target">
<ItemGroup>
<AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
</ItemGroup>
<!--
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
<Output TaskParameter="Revision" PropertyName="MyRevision" />
</SvnVersion>
-->
<PropertyGroup>
<MyRevision>9999</MyRevision>
</PropertyGroup>
<FileUpdate Files="#(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="AssemblyFileVersion("$1.$2.$3.$(MyRevision)" />
</Target>
</Project>
To enhance the above, you would create a new target that would run before "BeforeBuild_VersionTagIt_Target", that would pull your code from source-control and put it in the $(WorkingCheckout) folder.
The basic steps would then be: 1. Checkout code from Source-Control. 2. Run the targets that alter the AssemblyVersion (and whatever else you want to manipulate) and 3. Build the .sln file.
That's the basics of a .proj file. You can do much more. Usually by using helper libraries that already exists.

XSLTC.EXE MSBuild Task

I have several XSLTs used in my ASP.NET web application.
I want these files to be compiled to dll whenever I build the project.
Currently, I'm compiling the xslts manually by invoking xsltc.exe from vs2010 tools command prompt.
How can I add msbuild task for xsltc.exe so that it will generate assembly whenevr i build my project?
I'm using .NET 4.0.
That works but doesn't really wrap the tool in a MSBuild friendly way.
I came up with this (which was good enough to get by).
<!-- The Transform File Names... -->
<ItemGroup>
<XsltcTransform Include="Transform1.xslt">
<!-- And the generated .Net Class name. -->
<Class>Transform1Class</Class>
</XsltcTransform>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Sadly using $(OutDir) MUST come after the Import of CSharp.targets -->
<PropertyGroup>
<XSLTCOutputDll>$(OutDir)xslts.dll</XSLTCOutputDll>
</PropertyGroup>
<Target Name="FindXSLTC">
<PropertyGroup>
<XSLTC>"$(TargetFrameworkSDKToolsDirectory)xsltc.exe"</XSLTC>
</PropertyGroup>
</Target>
<Target Name="XSLTC" Inputs="#(XsltcTransform)" Outputs="$(XSLTCOutputDll)" DependsOnTargets="FindXSLTC">
<Exec Command="$(XSLTC) /out:"$(XSLTCOutputDll)" #(XsltcTransform -> ' /class:%(Class) %(FullPath) ')" />
</Target>
<Target Name="BeforeResolveReferences" DependsOnTargets="XSLTC">
</Target>
These targets will let you compile multiple transforms into one DLL.
Running XSLTC before "BeforeResolveRefereneces" is necessary so that you can have an assembly reference to the generated DLL.
<PropertyGroup>
<WinSDK>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin</WinSDK>
</PropertyGroup>
<Target Name="Build">
<Exec Command="%22$(WinSDK)\xsltc.exe%22 /out:$(OutputPath)\_PublishedWebsites\xyzapp\bin\Xslts.dll /class:ABC %22$(MSBuildProjectDirectory)\xyzapp\a.xslt%22 /class:DEF %22$(MSBuildProjectDirectory)\xyzapp\b.xslt%22 /class:GHI %22$(MSBuildProjectDirectory)\xyzapp\c.xslt%22"/>
</Target>

Categories