Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions docs/usage/msbuild-task.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# MSBuild Task

The MSBuild Task for GitVersion — **GitVersionTask** — a simple solution if you
want to version your assemblies without writing any command line scripts or
modifying your build process.
The MSBuild Task for GitVersion — **GitVersionTask** — is a simple solution if
you want to version your assemblies without writing any command line scripts or
modifying your build process.

## TL;DR

### Install

It works simply by installing the [GitVersionTask NuGet
Package](https://www.nuget.org/packages/GitVersionTask/) into the project you
want to have versioned by GitVersion:
want to have versioned by GitVersion:

Install-Package GitVersionTask
```shell
Install-Package GitVersionTask
```

### Remove attributes

The next thing you need to do, is remove the `Assembly*Version` attributes from
your `Properties\AssemblyInfo.cs` files, so GitVersionTask can be in charge of
versioning your assemblies.
versioning your assemblies.

### Done!

Expand All @@ -32,7 +34,7 @@ described below.

## How does it work?

### Inject version metadata into the assembly
### Inject version metadata into the assembly

The sub-task named `GitVersionTask.UpdateAssemblyInfo` will inject version
metadata into the assembly which GitVersionTask is added to. For each assembly
Expand All @@ -45,7 +47,7 @@ At build time a temporary `AssemblyInfo.cs` will be created that contains the
appropriate SemVer information. This will be included in the build pipeline.
Sample default:

```c#
```csharp
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+Branch.master.Sha.722aad3217bd49a6576b6f82f60884e612f9ba58")]
Expand All @@ -56,32 +58,32 @@ Now when you build:
* `AssemblyVersion` will be set to the `AssemblySemVer` variable.
* `AssemblyFileVersion` will be set to the `MajorMinorPatch` variable with a
* appended `.0`. `AssemblyInformationalVersion` will be set to the
* `InformationalVersion` variable.
* `InformationalVersion` variable.

#### Other injected Variables

All other [variables](../more-info/variables.md) will be injected into an
internal static class:
internal static class:

```c#
```csharp
namespace AssemblyName
{
[CompilerGenerated]
internal static class GitVersionInformation
{
public static string Major = "1";
public static string Minor = "1";
public static string Patch = "0";
...All other variables
}
[CompilerGenerated]
internal static class GitVersionInformation
{
public static string Major = "1";
public static string Minor = "1";
public static string Patch = "0";
...All other variables
}
}
```

#### Accessing injected Variables

##### All variables

```c#
```csharp
var assemblyName = assembly.GetName().Name;
var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation");
var fields = gitVersionInformationType.GetFields();
Expand All @@ -94,7 +96,7 @@ foreach (var field in fields)

##### Specific variable

```c#
```csharp
var assemblyName = assembly.GetName().Name;
var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation");
var versionField = gitVersionInformationType.GetField("Major");
Expand All @@ -118,7 +120,7 @@ After `GitVersionTask.GetVersion` has executed, the MSBuild properties can be
used in the standard way. For example:

```xml
<Message Text="GitVersion_InformationalVersion: $(GitVersion_InformationalVersion)"/>
<Message Text="GitVersion_InformationalVersion: $(GitVersion_InformationalVersion)"/>
```

### Communicate variables to current Build Server
Expand All @@ -129,7 +131,7 @@ the version information to the current Build Server log.
If, at build time, it is detected that the build is occurring inside a Build
Server then the [variables](../more-info/variables.md) will be written to the
Build Server log in a format that the current Build Server can consume. See
[Build Server Support](../build-server-support/build-server-support.md).
[Build Server Support](../build-server-support/build-server-support.md).

## Conditional control tasks

Expand All @@ -147,7 +149,7 @@ this:
...
</PropertyGroup>
```

## My Git repository requires authentication. What do I do?

Set the environmental variables `GITVERSION_REMOTE_USERNAME` and
Expand Down