Fork me on GitHub

Build a MSVC DLL with JNI headers generated by javah

The example uses javah to generate native header files which are then be part of the native build to create a Windows DLL

Note:

  • User is responsible to provide all neccessary compiler and linker options
  • envFactoryName is used to set up compiler and linker specific environments. This feature is extremely useful when you need to use various compilers in your build system and it is impossible to set up single global environment to accomodate all compilers.
  • Checkout real examples in svn
<project>
   ...
   <packaging>dll</packaging>
   
   <dependencies>
     <dependency>
       your jar which has jni interface.
     </dependency>
     <dependency>
       other native library ( .lib, .so, .a, .o, etc)
       to be linked in.
     </dependency>
     ...
   </dependencies>
   
   ...

   <build>   
     <plugins>
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>native-maven-plugin</artifactId>
         <extensions>true</extensions>
         <configuration>

           <!-- trigger javah execution -->         
           <javahClassNames>
             <javahClassName>class1</javahClassName>
             <javahClassName>class2</javahClassName>              
             <javahClassName>...</javahClassName>
           </javahClassNames>
           <!-- enable additional javah interface in dependencies list -->
           <javahSearchJNIFromDependencies>true</javahSearchJNIFromDependencies>
           <!-- 
            | Add jdk include directories to system include path
            | Override ${jkdIncludePath} If your jdk does not conform to Sun JDK layout
            -->
           <javahOS>win32</javahOS>

           <!-- setup compiler and linker environment according to msvc 6 vcvars32.bat -->
           <!-- without this setting, you will need to setup the environment outside   -->
           <!-- of Maven                                                               -->
           <envFactoryName>org.codehaus.mojo.natives.msvc.MSVC6EnvFactory</envFactoryName>
         
           <compilerProvider>msvc</compilerProvider>
           
           <compilerStartOptions>
             <compilerStartOption> /MD /W4 -O</compilerStartOption>
             <compilerStartOption>-D_WIN32_WINNT=0x0500 </compilerStartOption>
           </compilerStartOptions>
          
           <sources>
             <source>
               <-- relative to your project directory -->
               <directory>../src/main/native</directory>
               <fileNames>
                 <fileName>file1.c</fileName>
                 <fileName>file2.c</fileName>
                 <fileName>...</fileName>
               </fileNames>
             </source>
             <source>
               <directory>src/main/native</directory> 
               <fileNames>
                 <fileName>file3.c</fileName>
                 <fileName>file4.c</fileName>
                 <fileName>...</fileName>
               </fileNames>
             </source>          
            
             <!-- additional include path -->
             <source>
               <directory>...</directory>
             </source>          

             <!-- additional system include path -->
             <source>
               <directory>...</directory>
               <dependencyAnalysisParticipation>false</dependencyAnalysisParticipation>
             </source>          
           </sources>

           <!-- deploy the accompany .lib file as well -->          
           <linkerSecondaryOutputExtensions>lib</linkerSecondaryOutputExtensions >
          
           <linkerStartOptions>
             <linkerStartOption> /INCREMENTAL:NO /DLL user32.lib advapi32.lib oldnames.lib kernel32.lib </linkerStartOption>
           </linkerStartOptions>
         </configuration>
        
    </build>
    
    
</project>