rust-maven-plugin:在Java Maven项目中编译和捆绑Rust JNI库


如果你想在Java中玩JNI和Rust,Rust Maven插件让这一过程变得简单。 可以说,它比JNI和C更简单。


插件功能

  • 该插件将构建委托给cargo并支持大部分cargo build功能。
  • 主要用例是简化 Java Maven项目中[url=https://crates.io/crates/jni]Rust JNI 库[/url]的构建过程。
  • 此外,该插件还可以编译二进制文件。
  • 该插件可以将编译的二进制文件复制到自定义位置,因此可以将它们捆绑在.jar文件内。
  • 支持cargo test期间调用mvn test。

编辑您的pom.xml以添加插件:

<project ...>
    ...

    <!-- Note: Don't add rust-maven-plugin to <dependencies>. -->

    <build>
        <plugins>
            <plugin>
                <groupId>org.questdb</groupId>
                <artifactId>rust-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <id>rust-build-id</id>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <path>src/main/rust/your-rust-crate</path>
                            <copyTo>${project.build.directory}/classes/io/questdb/example/rust/libs</copyTo>
                            <copyWithPlatformDir>true</copyWithPlatformDir>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
    </build>

</project>


这里,<path>...</path>是要构建的Rust crate的路径,它是相对于pom.xml文件本身的。该插件将调用crate上的cargo build。

<id>是一个任意的字符串,你可以用它来识别执行。它不需要与cockate的名字相匹配。

如果你需要构建多个货箱,你可以添加多个执行。