将C编译为Maven中的本机库

时间:2015-11-02 04:41:49

标签: java c maven java-native-interface maven-nar-plugin

我有一个依赖于C源文件的Java项目。我有正确的JNI头文件,我只需要使用C代码将它们编译为 target / 中的dll(或Linux / OSX等效文件)。我当前的 pom.xml 构建了Java代码,但没有构建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>
&#13;
&#13;
&#13;

我使用Java 8和Clang。我有 jni-headers-maven-plugin ,但我认为我不需要它,因为我不需要动态生成标题。我还有 nar-maven-plugin ,我认为配置正确,但它并没有在任何地方显示在日志中。

0 个答案:

没有答案