jado
jado

Reputation: 954

Compile C to native library in Maven

I have a Java project that relies on C source files. I have the proper JNI header files, I just need to compile them with the C code to a dll (or Linux/OSX equivalent) in target/. My current pom.xml builds the Java code, but doesn't build the dll.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xyz.jadonfowler</groupId>
    <artifactId>o</artifactId>
    <packaging>jar</packaging>
    <version>2.0</version>
    <properties>
        <!-- Native compiler -->
        <compiler-name>clang</compiler-name>
        <linker-name>clang</linker-name>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.heroku.sdk</groupId>
            <artifactId>heroku-jdbc</artifactId>
            <version>0.1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        </dependency>
    </dependencies>
    <build>
        <defaultGoal>install</defaultGoal>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>src</testSourceDirectory>
        <resources>
            <resource>
                <directory>res</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>xyz.jadonfowler.o.WebIDE</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>xyz.jadonfowler.o.WebIDE</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.alexkasko.maven</groupId>
                <artifactId>jni-headers-maven-plugin</artifactId>
                <version>1.0.6</version>
                <executions>
                    <!-- generate header for native methods -->
                    <execution>
                        <id>javah</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>javah</goal>
                        </goals>
                        <configuration>
                            <javahClass>xyz.jadonfowler.o.OCBindings</javahClass>
                            <javahOutputFilePath>${project.basedir}/xyz_jadonfowler_o_OCBindings.h</javahOutputFilePath>
                        </configuration>
                    </execution>
                    <!-- generate header for java methods -->
                    <!-- Not need atm
                    <execution>
                        <id>javap</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>javap</goal>
                        </goals>
                        <configuration>
                            <javapClass>xyz.jadonfowler.o.OCBindings</javapClass>
                            <javapOutputFilePath>${project.basedir}/my_header_callbacks.h</javapOutputFilePath>
                        </configuration>
                    </execution> -->
                </executions>
            </plugin>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <version>3.2.3</version>
                <extensions>true</extensions>
                <configuration>
                    <c>
                        <name>${compiler-name}</name>
                        <debug>false</debug>
                        <includes>
                            <include>*.h</include>
                            <include>*.c</include>
                        </includes>
                        <!-- Options for compiler -->
                        <options combine.children="append">
                            <option>-I $JAVA_HOME</option>
                            <option>-I $JAVA_HOME/*</option>
                        </options>
                    </c>
                    <linker>
                        <name>${linker-name}</name>
                    </linker>
                    <libraries>
                        <library>
                        <type>jni</type>
                        <narSystemPackage>xyz.jadonfowler.o</narSystemPackage>
                        </library>
                    </libraries>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>windows-common</id>
            <activation>
                <os>
                    <family>windows</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.maven-nar</groupId>
                        <artifactId>nar-maven-plugin</artifactId>
                        <extensions>true</extensions>
                        <configuration>
                            <cpp>
                                <options combine.children="append">
                                    <option>-mthreads</option>
                                </options>
                            </cpp>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>linux-common</id>
            <activation>
                <os>
                    <family>linux</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.maven-nar</groupId>
                        <artifactId>nar-maven-plugin</artifactId>
                        <extensions>true</extensions>
                        <configuration>
                            <cpp>
                                <options combine.children="append">
                                    <option>-pthread</option>
                                </options>
                            </cpp>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

I'm using Java 8 with Clang. I have the jni-headers-maven-plugin, but I don't think I need it because I don't need to generate the headers on-the-fly. I also have the nar-maven-plugin, which I think is configured correctly, but it isn't showing up in the log anywhere.

Upvotes: 3

Views: 2189

Answers (0)

Related Questions