Add JNI Library Compilation with maven or ant tasks

6

I'm creating a Java project with native methods ( JNI ) without having to create three separate projects for this. My goal is to compile everything I need in one go.

I use only one DLL / OS in this project generated by a single C file.

How to add this build step and move the library to the dist folder in pom do maven or ant tasks ?

My indifference to maven or ant tasks is that I plan to use netbeans to compile this, so what's easiest to adapt will be what I'm looking for.

    
asked by anonymous 08.10.2014 / 19:07

2 answers

5

Maven has the Native Maven Plugin and NAR Plugin for Maven . Both have goals for working with C / C ++ compilers, generating headers with javah , etc.

If you need a do-it-yourself solution, this article (a little old) has an interesting cake recipe. Classes are compiled by calling the make command through AntRun and it shows you how to load and package correctly the native libraries in Maven (contemplating the scenario in which you want to build build for multiple platforms).

    
09.10.2014 / 14:08
2

I use nar-maven-plugin to work with JNI, C, and Java in a single maven project. This plugin was left for a time without maintenance but now there is a group that has resumed the development of the project.

This plugin automatically creates the JNI headers from Java methods with the reserved native keyword, prior to compiling the Java code. It also automatically creates a Java class to load the generated DLL by adjusting the DLL name according to the version (following the Maven pattern). The plugin generates two artifacts for a JNI project: the JAR file and the DLL; so it's easier to keep married code C and Java.

See these two articles in Portuguese:

1 - JNI: Java and C ++ in a Single Maven Project

2 - C ++ and Maven: nar-maven-plugin

I think the first article is exactly what you are looking for. Both have source code for download.

    
17.12.2014 / 13:15