在 JAR 中包含本机二进制文件带有 maven-shade-plugin 的问题

问题描述

我正在构建一个需要 SO 文件的库。

我的目标:

  • 自动从工件存储库中提取 SO 文件
  • 将其作为资源包含在已安装的 JAR 中,这样我就不需要手动添加它,因此也不必将其放入 GitHub 存储库中。

我在我的库 POM 中像这样引用它:

    <dependency>
        <groupId>com.myco.mygrp</groupId>
        <artifactId>native</artifactId>
        <type>so</type>
        <scope>runtime</scope>
        <version>0.0.1</version>
    </dependency>

这确实将它添加到我的本地 Maven 存储库中,但是当我尝试从作为该项目客户端的第二个项目构建 uber JAR 并因此导入它并在其 POM 中引用它时,我得到

无法执行目标 org.apache.maven.plugins:maven-shade-plugin:3.2.4:shade (default) on project i-am-the-client: Error creation shaded jar: error in opening zip 文件 /Users/me/.m2/repository/com/myco/mygrp/0.0.1/native-0.0.1.so -> [帮助 1]

虽然我希望在我的客户端 uber JAR 中使用该 SO 文件,但我不需要解压缩或打开它。 如何让 maven-shade-plugin 将其包含在 JAR 中,但又不将 SO 文件视为要解压的 JAR?这里是 maven-shade-plugin 供参考:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.myco.somegrp.Client</mainClass>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>scala/tools/nsc/doc/html/resource/lib/jquery*</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

我可以通过手动将本机 SO 添加到资源目录中来规避这一点,但这将违背目标之一,并且在代码审查期间也会引起注意,因为根据目标,二进制文件已经在 Nexus 中,不一定是在 GitHub 中也需要。

作为次要方法,我使用我的 maven-dependency-plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.1.2</version>
        <executions>
            <execution>
                <id>copy</id>
                <phase>package</phase>
                <goals>
                    <goal>copy</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>com.myco.mygrp</groupId>
                            <artifactId>native</artifactId>
                            <type>so</type>
                            <overWrite>false</overWrite>
                            <destFileName>my_native.so</destFileName>
                        </artifactItem>
                    </artifactItems>
                    <overWriteReleases>false</overWriteReleases>
                    <overWriteSnapshots>true</overWriteSnapshots>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>

当我 mvn install 时,我在目标/lib 中得到了预期的“my_native.so文件,但在安装的 JAR 中却没有。 我希望将此文件包含在已安装的 JAR 中,以便 JNI 代码可以加载它。我打算 System.load 将它String之前将其写入文件系统中的临时文件,然后加载该临时文件
我已经为此工作了几天,包括在我确定此策略之前搜索互联网,但如果这是错误方法,我愿意接受建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)