在构建Netbeans/Ant-java项目时,跨本地库(非jar)复制到dmg中



在我的NetBeans java 8项目中,有一些本机库必须复制到lib目录中。从许多搜索尝试中,我发现NetBeans没有通过GUI实现这一点的方法。所以我添加了以下内容:

对于正常的本地构建和运行:

<condition property="isMac">
<os family="mac" />
</condition>
<condition property="isWindows">
<os family="windows" />
</condition>
<target name="-copy-win" if="${isWindows}">
<echo message="copying windows libraries"/>
<copy todir="${basedir}/dist/lib"> 
<fileset dir="${basedir}/native" includes="**/*.dll"/>
</copy> 
</target>  
<target name="-copy-mac" if="${isMac}">
<echo message="copying mac libraries"/>
<copy todir="${basedir}/dist/lib"> 
<fileset dir="${basedir}/native" includes="**/*.dylib"/>
<fileset dir="${basedir}/native" includes="**/*.so"/>
</copy> 
</target>    
<target depends="-copy-win,-copy-mac" name="-pre-jfx-deploy"/>

对于"仅作为图像打包"选项:

<target name="-post-jfx-deploy-mac" if="${isMac}"> 
<copy todir="${basedir}/dist/bundles/MyApp.app/Contents/Java/lib"> 
<fileset dir="${basedir}/native" includes="**/*.dylib"/> 
<fileset dir="${basedir}/native" includes="**/*.so"/> 
</copy> 
</target>    
<target name="-post-jfx-deploy-win" if="${isWindows}"> 
<copy todir="${basedir}/dist/bundles/MyApp/app/lib"> 
<fileset dir="${basedir}/native" includes="**/*.dll"/> 
</copy> 
</target>    
<target depends="-post-jfx-deploy-mac, -post-jfx-deploy-win" name="-post-jfx-deploy"/>

但是对于打包为EXE/DMG,本机库不会被复制。我可以通过在build.xml中定义我自己的fx:deploy任务来解决这个问题,但这很麻烦,因为NetBeans已经在jfx-impl.xml中使用一些javascript创建了一个任务。

还有其他方法吗?

最后,我想我有一些工作要做。

  1. 在顶级项目目录中创建一个名为native的文件夹,并将您的dll和dylib放入其中

然后在build.xml:中

  1. 添加-copy-win和/或-copy-mac目标(如上所述(

  2. 添加两个目标:

    <target depends="-jfx-copylibs,-rebase-libs,jfx-deployment,-copy-win,-copy-mac" name="-post-jar"/>

    <target depends="-copy-win,-copy-mac" name="-pre-jfx-deploy"/>

第一个确保在netbeans中运行项目时将库复制到dist/lib,第二个确保在部署项目时将它们复制到dist/lib

  1. 添加一个目标-pre-init,在nbproject目录中从jfx-impl.xml经过整个宏<macrodef name="deploy-deploy">,并将<property name="pp_deploy_fs2" value="lib${file.separator}*.jar"/>中的值更改为value="lib${file.separator}*.*

整个文件都在这个要点中。

然后,您可以在以下代码中加载本机库:

String dir = Paths.get(MainController.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent().toString();
if (SystemUtils.IS_OS_WINDOWS) {
System.load(current + "/lib/native_library.dll");
}   
else if (SystemUtils.IS_OS_MAC_OSX) {
System.load(current + "/lib/native_library..dylib");
}

最新更新