February 21, 2010

Native Maven Plugin - Build native C/C++ projects with Maven

« Spring Hibernate - LazyInitializationException | Main | Unit testing with Hibernate and Spring »

Recently I looked for an alternative to build C/C++ based source code with Maven. I found the Native Maven Plugin at codehaus.org. This plugin provides all necessary native packaging types how dll, exe, lib, a, o, so, sl, dylib, jnilib and uexe (executables running on unix based systems). The skeleton plugin description looks like follow snippet:

...
<!-- possible packaging types - dll, exe, lib, a, o, so, sl, dylib, jnilib and uexe -->
<packaging>uexe</packaging>
...
<build>   
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>native-maven-plugin</artifactId>
      <extensions>true</extensions>
      <configuration>
        ...
      </configuration>
    </plugin>
  </plugins>
</build>

The following example exercise defines a hello world c program, which will be compiled with the native-maven-plugin.

First you implement your helloWorld.c file:
#include 
main()
{
 printf("Hello World \n");
}
At next step you set up the maven project, that has the following directory structure:
- maven-native-example
  - src
    - main
      - native

The necessary plugin description is:
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>native-maven-plugin</artifactId>
  <extensions>true</extensions>
  <configuration>
      <compilerProvider>generic-classic</compilerProvider>
      <compilerExecutable>g++</compilerExecutable>
      <linkerExecutable>g++</linkerExecutable>*
      <sources>
           <source>
              <directory>${native.source.dir}</directory>
              <fileNames>
                  <fileName>helloWorld.c</fileName>
              </fileNames>
          </source>
          <source>
              <directory>${native.source.dir}/include</directory>
          </source>
      </sources>
      <!-- for libs as so files -->
      <!--linkerStartOptions>
          <linkerStartOption>-shared</linkerStartOption>
      </linkerStartOptions-->
  </configuration>
</plugin>

The "compilerExecutable"- and the "linkeExecutable"-element contains the used compiler. Because I work on a linux system i choose g++ compiler. The source and header files will be defined in sources element.

A "mvn clean install"-command has the following steps:

[INFO] Building maven-native-example.uexe
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting file set: /home/rafsob/projects/maven-native-example/target (included: [**], excluded: [])
[INFO] [native:initialize {execution: default-initialize}]
[INFO] [native:unzipinc {execution: default-unzipinc}]
[INFO] [native:compile {execution: default-compile}]
[INFO] /bin/sh -c cd /home/rafsob/projects/maven-native-example && 
g++ -I/home/rafsob/projects/maven-native-example/src/main/native 
-I/home/rafsob/projects/maven-native-example/src/main/native/include 
-o/home/rafsob/projects/maven-native-example/target/objs/helloWorld.o -c 
/home/rafsob/projects/maven-native-example/src/main/native/helloWorld.c
[INFO] [native:link {execution: default-link}]
[INFO] /bin/sh -c cd /home/rafsob/OpenSourceProjekte/JNINativeExample && 
g++ -o/home/rafsob/projects/maven-native-example/target/maven-native-example.uexe 
target/objs/helloWorld.o
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rafsob/projects/maven-native-example/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [install:install {execution: default-install}]
[INFO] Installing 
/home/rafsob/projects/maven-native-example/target/maven-native-example.uexe 
to /home/rafsob/.m2/repository/org/developers/blog/maven-native-example/1.0-SNAPSHOT/maven-native-example-1.0-SNAPSHOT.uexe
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

After that you can execute your hello world program as follow:

rafsob@rafsob-desktop:~/projects/maven-native-plugin/target$ 
./maven-native-example.uexe
Hello World

Full example can be downloaded here .

Regards
Rafael

Technorati Tags:

Posted by rafael.sobek at 11:53 AM in Maven

 

[Trackback URL for this entry]

Comment: Jason van Zyl at So, 21 Feb 4:09 PM

The NAR[1] plugin is more advanced and is still very active. It's what Sonatype plans to work on and support if that means anything.

Jason van Zyl

[1]: http://github.com/sonatype/maven-nar-plugin

Pingback: BlogTek &raquo; Blog Archive &raquo; Native Maven Plugin &#8211; Build native C/C++ projects with Maven at Mo, 22 Feb 6:01 PM

Native Maven Plugin - Build native C/C++ projects with Maven
Blog &#8211; Programming Languages, Technologies and Visions. Topics: Visions, Architecture Issues, Programming Languages, &#8230; Visit link: Native Maven

Comment: Deepak at Mi, 16 Jun 9:41 AM

Hi,
I am using maven-native plugin for the first time for building my c++ project but i am facing below problem, can you please help me out here?

----------------------------------------
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error executing command line. Exit code:1

[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: Error executing command
line. Exit code:1
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa
ultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLi
fecycle(DefaultLifecycleExecutor.java:556)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(Defau
ltLifecycleExecutor.java:535)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHan
dleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmen
ts(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLi
fecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:6
0)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)

at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error executing comma
nd line. Exit code:1
at org.codehaus.mojo.natives.plugin.NativeCompileMojo.execute(NativeComp
ileMojo.java:171)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPlugi
nManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa
ultLifecycleExecutor.java:694)
... 17 more
Caused by: org.codehaus.mojo.natives.NativeBuildException: Error executing comma
nd line. Exit code:1
at org.codehaus.mojo.natives.util.CommandLineUtil.execute(CommandLineUti
l.java:34)
at org.codehaus.mojo.natives.compiler.AbstractCompiler.compile(AbstractC
ompiler.java:98)
at org.codehaus.mojo.natives.plugin.NativeCompileMojo.execute(NativeComp
ileMojo.java:167)
... 19 more
----------------------------------------

Thanks and i am waiting for your reply..
-Deepak

Comment: Vaibhav at Fr, 20 Apr 6:47 AM

Any sample/ref for building c/c++ programs on windows xp platform?

Comment: Yashaswini at Di, 24 Apr 6:24 AM

Hi,
Please provide a solution for the above mentioned issue. I'm facing the same error too

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
 
test