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:#includeAt next step you set up the maven project, that has the following directory structure:main() { printf("Hello World \n"); }
- 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: Maven Native Plugin C/C++

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