Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#

Maven Build for .NET Application

Rate me:
Please Sign up or sign in to vote.
3.67/5 (4 votes)
18 Mar 2011CPOL2 min read 88.9K   719   16   7
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)


Written By
Technical Lead
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionsample code doesn't work....what's changed? Pin
Member 826232623-Sep-11 6:16
Member 826232623-Sep-11 6:16 
AnswerRe: sample code doesn't work....what's changed? Pin
Rajamohan.Rajendran25-Sep-11 17:18
Rajamohan.Rajendran25-Sep-11 17:18 
GeneralRe: sample code doesn't work....what's changed? Pin
Member 826232626-Sep-11 8:37
Member 826232626-Sep-11 8:37 
AnswerRe: sample code doesn't work....what's changed? Pin
Rajamohan.Rajendran26-Sep-11 17:36
Rajamohan.Rajendran26-Sep-11 17:36 
The MavenBuildTesting.exe file will be created and it live inside the Bin folder only.

The exe file createion time will be modified.

If want to move the out files as per your need and use the following pom files.

To pakcage the output files. you need to use package assembly pulgin

Add Build module

XML
<modules>
    <module>MavenBuildTesting</module>
    <module>Build</module>
  </modules>


Create Build folder parent folder. "C:\MavenBuildTesting\" the location will be differ.
the following item will present in the parent folder.
1. Build
2. MavenBuildTesting
3. pom.xml(parent which contain Modules tag

I have attached package pom and copy and paste file to Build folder. verify the location it may differ.

Create POm file under the Build folder and copy and paste.
XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>Build</groupId>
  <artifactId>clientBuild</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <name>Client Package</name>
  <description> Client Package pom</description>

  <build>
    <plugins>
      <!-- Package -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-5</version>
        <configuration>
          <outputDirectory>${basedir}/../dist</outputDirectory>
          <finalName>ClientBuild</finalName>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>PackageBuild.xml</descriptor>
          </descriptors>
        </configuration>

        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>
</project>


create packageBuild.xml file and copy the below the content and paste.

XML
<assembly>
  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>${basedir}/<big>../MavenBuildTesting/bin/Release</big></directory>
      <outputDirectory>/MavenBuildTesting</outputDirectory>
      <includes>
        <include>*.dll</include>
        <include>*.config</include>
        <include>*.chm</include>
        <include>*.xsd</include>
        <include>*.exe</include>
      </includes>
      <excludes>
        <exclude>*.pdb</exclude>
        <exclude>Test*</exclude>
        <exclude>test*</exclude>
        <exclude>*.vshost.exe</exclude>
        <exclude>*.vshost.exe*</exclude>
      </excludes>
    </fileSet>
  </fileSets>
</assembly>


let me know if you have any issue.
GeneralRe: sample code doesn't work....what's changed? Pin
Member 82623265-Oct-11 10:08
Member 82623265-Oct-11 10:08 
General[My vote of 1] good idea (possibly), bad execution Pin
Tieske811-Apr-11 11:13
professionalTieske811-Apr-11 11:13 
GeneralExciting! Pin
JimBlaney18-Mar-11 3:45
JimBlaney18-Mar-11 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.