Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Maven Build for .NET Application

3.67/5 (4 votes)
18 Mar 2011CPOL2 min read 90.8K   721  
How to build a .NET application using Maven

Introduction

This article describes how to perform the .NET application build in Maven tool. The intent of this article is to tell you how to go about doing a full command-line build of .NET application using Maven. You first need to install the prerequisite application, listed here.

Prerequisites

The following applications are used by the different goals, but This article describes only .NET application build and fxcop reports.

ApplicationUsageVersionDownload site
MSBuild.NET sdk install dir2.0, 3.5http://msdn.microsoft.com/en-us/netframework/aa569263.aspx
Fx Cop.NET code quality reports1.36 +http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505&displaylang=en
MavenTo build Run Maven POM2.2.1http://maven.apache.org/docs/2.2.1/release-notes.html

The following changes are required after the installation. After the SDK 7 is installed, we need to explicitly run Fxcop application. It will be available in the following location: C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\FXCop and run the fxcop application.

The following environment variables need to be set.

Variable NamePath
FXCOP_HOMEC:\Program Files\Microsoft FxCop 1.35
JAVA_HOMEC:\Program Files\Java\jdk1.6.0_21
MAVAN_HOMEC:\Bin\apache-maven-2.2.1\bin
PATH*Append : %JAVA_HOME%\bin;

Remember to restart the computer after changing the value of the SYSTEM PATH.

Configure Maven to Run

To configure Maven plugin to run, you need to edit the setting.xml. Open the file from c:\bin\apache-maven-2.2.1\conf\settings.xml and make the change (for your case, location may differ):

  1. Change Maven location repository, otherwise it will be downloaded in your document setting \ .m2... or <localRepository>C:\maven_repository</localRepository>.
  2. Change the Proxy setting according to your organization proxy config example: include the following code to proxy section and change the domain name and IP address:
    XML
    <proxy> 
      <active>true</active> 
      <protocol>http</protocol> 
      <host>http.proxy.domain.com</host> 
      <port>8000</port> 
      <nonProxyHosts>localhost|*.domain.com|0.0.0.1</nonProxyHosts> 
    </proxy>
  3. Include the .NET Profile into the profiles section example:
    XML
    <profile> 
      <id>dotnet</id> 
      <activation> 
        <activeByDefault>true</activeByDefault> 
      </activation> 
    </profile>
  4. Add the following code to Properties section:
    XML
    <properties> 
      <dotnet.2.0.sdk.directory>
        C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727
      </dotnet.2.0.sdk.directory> 
      <dotnet.3.5.sdk.directory>
        C:/WINDOWS/Microsoft.NET/Framework/v3.5
      </dotnet.3.5.sdk.directory> 
      <dotnet.4.0.sdk.directory>
        C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319
      </dotnet.4.0.sdk.directory> 
      <fxcop.directory>
        c:/Program Files/Microsoft FxCop 1.36
      </fxcop.directory> 
    </properties>

Build the .NET project in the command project with a few modifications to the pom files.

Project Parent Pom

XML
<?xml version="1.0" encoding="utf-8"?>
<project xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
<groupId>MavenBuildTesting</groupId>
<artifactId>MavenBuildTesting-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
    <visual.studio.solution>MavenBuildTesting.sln</visual.studio.solution>
    <visual.test.project.pattern>*.Tests</visual.test.project.pattern>
    <dotnet.tool.version>3.5</dotnet.tool.version>
    <sonar.language>cs</sonar.language>
</properties>

<build>
    <plugins>
    <plugin>
        <groupId>org.codehaus.sonar-plugins.dotnet</groupId>
        <artifactId>maven-dotnet-plugin</artifactId>
        <extensions>true</extensions>
    </plugin>

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <configuration>
            <language>cs</language>
        </configuration>
        </plugin>
    </plugins>
</build>
</project>

The above file needs to be created where the project solution is present and below the file, you need to create where the project file is present.

Project Child Pom

XML
<?xml version="1.0" encoding="utf-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://maven.apache.org/POM/4.0.0">
  <parent>
   <groupId>MavenBuildTesting</groupId>
    <artifactId>MavenBuildTesting-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>MavenBuildTesting</artifactId>
  <packaging>sln</packaging>
</project>

Please ensure that you build and install the parent first (from the parent directory).
Start -> Run -> cmd root to sample project:

C:\MavenBuildTesting\mvn dotnet:clean dotnet:compile install 

Then build and run the goals on the child:

C:\MavenBuildTesting\MavenBuildTesting>mvn dotnet:clean dotnet:compile dotnet:fxcop 

After the successful Maven task, you will be able to see the executable available in the obj directory or project bin release.

If you have any further issues building .NET with Maven, please let me know.

History

  • 18th March, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)