Executing javah from Maven

by 8/26/2013 04:33:00 PM 0 comments
Need to generate some JNI headers for files in a Maven project? Below is the simplest snippet I could put together that executes javah. This does nothing fancy, it simply generates headers from a list of files you provide and dumps them into a PROJECT/target/jni directory.

<build>
    <!-- ... -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
            <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <exportAntProperties>true</exportAntProperties>
                <target>
                   <property name="runtime_classpath" refid="maven.compile.classpath"/>
                   <exec executable="javah">
                       <arg value="-cp"/>
                       <arg value="${runtime_classpath}"/>
                       <arg value="-d"/>
                       <arg value="${project.build.directory}/jni"/>
                       <arg value="org.mbari.terrainnav.jni.TerrainNavNative"/>
                   </exec>
                </target>
            </configuration>
           </execution>
        </executions>
    </plugin>
    <!-- ... -->
</build>

To add your files with native methods just add space separated values to the line that contains org.mbari.terrainnav.jni.TerrainNavNative. For example, if you have classes foo.Bar and foo.Baz then that line would become:

<arg value="foo.Bar foo.Baz"/>


Why did I do it this way? Well, mainly the maven-antrun-plugin provides simple access to Maven's compile classpath. That classpath is required in order to execute javah correctly. I should note that ant has a javah task, but I was unable to get it to work with Maven. On the other hand, getting ant to execute javah directly was trivial.

Brian Schlining

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

0 comments: