Skip to content

Instantly share code, notes, and snippets.

@ogerardin
Last active October 12, 2018 13:19
Show Gist options
  • Save ogerardin/d259a9600bb2d5ce3bea1ac104d0707f to your computer and use it in GitHub Desktop.
Save ogerardin/d259a9600bb2d5ce3bea1ac104d0707f to your computer and use it in GitHub Desktop.
Retrieve Maven version in .Net projet
Add a custom attibute "MavenVersion" to the assembly
----------------------------------------------
- Add the file MavenVersion.cs to the project
- Add a line [assembly: MavenVersion("")] in the appropriate AssemblyInfo.cs files
Update attribute during build
-----------------------------
- Using NuGet package manager, add the following dependency to the project: "MSBuildTasks" by LoreSoft
- Add the following line to the project's .csproj file under <Projet> element (edit path as appropriate):
<UsingTask TaskName="FileUpdate" AssemblyFile="$(SolutionDir)\packages\MSBuildTasks.1.5.0.235\tools\MSBuild.Community.Tasks.dll" />
- Add the following line under the <PropertyGroup> element in the same file:
<MavenVersion Condition=" '$(MavenVersion)' == '' ">N/A</MavenVersion>
- Add the following lines to the same file (customize AssemblyInfo.cs location as appropriate):
<!-- Custom pre-build task to update the "MavenVersion" attribute of AssemblyInfo.cs -->
<Target Name="BeforeBuild">
<ItemGroup>
<!-- find all the AssemblyInfo.cs files in the solution-->
<AssemblyInfoFiles Include="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs" />
</ItemGroup>
<Message Text="MavenVersion: $(MavenVersion)" />
<FileUpdate Files="@(AssemblyInfoFiles)" Regex="MavenVersion\(.*\)" ReplacementText="MavenVersion(&quot;$(MavenVersion)&quot;)" />
</Target>
- Pass the value for MavenVersion to the MSBuild command, e.g. if using exec-maven-plugin in your POM file:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>msbuild-build</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<arguments>
<argument>/m</argument>
<argument>"/p:Configuration=Release"</argument>
<argument>"/p:Platform=Any CPU"</argument>
<argument>"/p:MavenVersion=${project.version}"</argument>
<argument>"/t:Build"</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<executable>msbuild</executable>
</configuration>
</plugin>
Access the MavenVersion property at runtime
-------------------------------------------
- Add this utility method somewhere:
public static string MavenVersion(Assembly assembly) {
object[] attributes = assembly.GetCustomAttributes(typeof(MavenVersionAttribute), false);
if (attributes.Length == 0)
return "";
return ((MavenVersionAttribute) attributes[0]).Version;
}
- Usage:
string mavenVersion = MavenVersion(Assembly.GetExecutingAssembly());
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Oge.Utilitaires {
public sealed class MavenVersionAttribute : Attribute {
// Methods
public MavenVersionAttribute(string mavenVersion)
{
this.m_mavenVersion = mavenVersion;
}
// Properties
public string Version
{
get
{
return this.m_mavenVersion;
}
}
// Fields
private string m_mavenVersion;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment