Maven 和 JOGL 库?

发布于 2024-08-16 06:15:50 字数 1323 浏览 8 评论 0原文

过去几天我一直在空闲时间研究 Maven,但似乎不知道如何组织项目以便使用 JOGL 库。我最好喜欢以下内容:

  1. 如有必要,自动从 这里(包含4个jar文件和一些本机库文件(.so/.dll));或者依赖于 Maven 项目,它是其中一个文件的包装器。
  2. 适当地解压缩该 zip 文件,以便:
    1. 将 jar 文件添加到类路径并根据需要进行部署,并且
    2. 本机库文件被添加到最终的 jar 文件中(这将使它们能够自动使用,还是我需要更多的参与?)

我认为我的部分问题是我不完全理解JOGL的使用,运行代码时在哪里放置本机库等。我需要回到基础知识并编写一个JOGL hello world,从命令编译它行并从命令行运行它,以准确了解它对本机库的目录放置的要求;事实上,我现在就可以去做。

对于第 1 项,我发现了一些特定于操作系统的功能; Maven 配置文件可以根据系统的属性(包括操作系统)来激活。这样我就可以激活一个 Windows 配置文件,它依赖于 Windows 特定的 JOGL 库,对于 Linux 来说也是如此,并且两者都有一个 64 位的另一个自我。 (激活官方文档 / 非官方文档。)

我尝试过基于 JOGL jar 文件创建 Maven 存储库,然后添加 JOGL jar 文件项目作为我的项目的依赖项;依赖项已下载,但未使用。我不知道 jar 文件去了哪里,也不知道如何使用它、解压它等。 这是我使用的命令。

因此,简而言之:JOGL 由四个 .jar 文件和一些本机库组成。我如何将这些文件集成到我的 Maven 项目中,以便我可以使用 Maven 处理我的构建过程来编写 JOGL 应用程序?此外,我如何根据操作系统使用不同的文件集,因为当然Windows、Linux 和 Mac 之间的本机库甚至 .jar 文件都不同。

I've been studying Maven in my free time over the last several days but can't seem to figure out how to organize a project so that the JOGL libraries are used. I would preferably like the following:

  1. Automatically download, if necessary, the OS-specific JOGL zip file from here (contains 4 jar files and some native library files (.so/.dll)); or depend on a Maven project which is a wrapper of one of the files.
  2. Unzip that zip file appropriately, so that:
    1. the jar files are added to the classpath and deployed as necessary, and
    2. the native library files are added to the final jar file (would this enable them to be automatically used, or would I need something more involved?)

I think part of my problem is that I don't fully understand the use of JOGL, where to place the native libraries when running the code, etc. I need to go back to the basics and write a JOGL hello world, compile it from the command line and run it from the command line to see exactly what it requires as far as directory placement of the native libraries; I may go do that right now, actually.

With item 1, I've found some OS-specific features; Maven profiles can be activated based on properties of the system, which include the operating system. So then I could activate a Windows profile which has a dependency of the Windows-specific JOGL library, same for the Linux, and both having a 64-bit alter ego. (Activation official docs / unofficial docs.)

I have tried creating a Maven repository based on a JOGL jar file, and then adding the JOGL jar file project as a dependency of my project; the dependency is downloaded, but not used. I have no idea where the jar file goes or how to use it, unpack it, etc. Here is the command I used.

So, in short: JOGL consists of four .jar files and some native libraries. How can I integrate those files into my Maven project so that I can write a JOGL application with Maven handling my build process? Furthermore, how can I use a different set of files depending on the operating system, because of course the native libraries and even the .jar files differ between Windows, Linux and Mac.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不一样的天空 2024-08-23 06:15:50

处理 JNI 和 Maven 时, Projects With JNI 是应该从参考开始的。它涵盖的内容比您当前的问题(“只是”使用依赖于 JNI 和本机库的库)要多得多,但是,谁能做得更多,谁就能做得更少。

如果您仔细阅读,您会发现使用 JNI 库的一种解决方案是将它们捆绑在特定于体系结构的 JAR 中,以便您可以从 Maven 的角度像依赖任何其他依赖项一样依赖它们。这实际上就是 JOGL 版本 1.1.1 的打包方式 http:// /download.java.net/maven/2/net/java/dev/jogl/,有一个包含 Java 类的 JAR 工件和多个包含本机库的特定于体系结构的 JAR 工件。

JNI 库存档在 jar 内

我最终使用的解决方案是
将编译好的jni库存储在
jar 与类文件一起。

这意味着交叉编译
所有可能的架构,或更多
简单地说,有一个不同的罐子
每个架构。后者适合
我们的设置非常好 - 其中
几乎我们所有的机器都是
Linux-i386,带有一点 win32
盒子。

遗憾的是System.load()无法应对
从 jar 中加载库,
所以我们需要一个定制的
加载器将库提取到
运行时的临时文件;这是
然而,显然是可以实现的。

然后,正如所解释的,我们的想法是使用自定义库加载器来加载本机库。好消息是这样的加载器是“提供”的,如下所述。

库加载器

我们现在已经有了 JNI 库
类路径,所以我们需要一种方法
加载它。我创建了一个单独的
将提取 JNI 的项目
来自类路径的库,然后
加载它们。找到它在
http://opensource.mxtelecom.com /maven/repo/com/wapmx/native/mx-native-loader/1.2/
这作为依赖项添加到
pom,显然。

要使用它,请致电
com.wapmx.nativeutils.jniloader.NativeLoader.loadLibrary(libname)
更多信息位于 javadoc 中:
NativeLoader

我一般更喜欢包装这些东西
在 try/catch 块中,如下所示:

公共类 Sqrt {
    静止的 {
        尝试 {
            NativeLoader.loadLibrary(“sqrt”);
        } catch (Throwable e) {
            e.printStackTrace();
            系统.退出(1);
        }
    }
    /* ... 类体 ... */
}

我们现在应该处于以下位置:
我们的 junit 测试在 Maven 中工作;一个MVN
测试应该有效!它也应该起作用
从 IDE 中很好。

现在,回答您的问题,如何:

如有必要,从此处自动下载特定于操作系统的 JOGL zip 文件(包含 4 个 jar 文件和一些本机库文件 (.so/.dll));或者依赖于 Maven 项目,该项目是其中一个文件的包装器。

遗憾的是,JOGL 2.0 jar 在 java.net 的 Maven 存储库中不可用,因此您必须处理这个问题,要么在私有存储库中提供它们,要么在每个开发人员的本地存储库中手动安装它们。为此,请使用 安装第 3 方 JAR 的指南(而不是像您那样使用 mvn deploy:deploy-file ,此目标用于将工件安装到远程存储库)。

就个人而言,我会从 URL 下载 JOGL 2.0 ZIP 您提供的,将其打包,就像使用 JOGL 1.1.1 一样(一个 Java JAR 和几个用于本机库的特定 JAR),并暂时将 JAR 安装在每个本地存储库中。然后,声明对 Java 工件的标准依赖关系,并且实际上使用 profiles 作为架构特定的依赖关系。像这样的事情:

<project>
  ...
  <dependencies> 
    <dependency>
      <groupId>net.java.dev.jogl</groupId>
      <artifactId>jogl</artifactId>
      <version>2.0-beta10</version>
    </dependency>
    ...
  </dependencies>
  ...
  <profiles>
    <profile>
      <id>linux-i586</id>
      <activation>
        <os>
          <arch>i386</arch>
          <family>unix</family>
          <name>linux</name>
        </os>
      </activation>
      <dependencies>
        <dependency>
          <groupId>net.java.dev.jogl.jogl-linux-i586</groupId>
          <artifactId>jogl-linux-i586</artifactId>
          <version>2.0-beta10</version>
        </dependency>
      </dependencies>
    </profile>
    ...
  </profiles>
  ...
</project>

不要忘记添加自定义库加载器和依赖项所需的存储库:

<project>
  <repositories>
    <repository>
      <id>opensource.mxtelecom.com</id>
      <url>http://opensource.mxtelecom.com/maven/repo</url>
    </repository>
    ...
  <repositories>
  ...
  <dependencies> 
    <dependency>
      <groupId>com.wapmx.native</groupId>
      <artifactId>mx-native-loader</artifactId>
      <version>1.2</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

关于问题的第二部分:

正确解压缩该 zip 文件,以便 (...)

正如我所解释的,您实际上不会依赖于 ZIP 文件,而是依赖于 JAR,并且在开发期间或分发项目时都不需要解压缩它们。对于发行版,您只需要创建一个包含依赖项的 jar。这可以使用 Maven-Assembly-Plugin 来完成。有关更多详细信息,请参阅此答案

When dealing with JNI and Maven, Projects With JNI is the reference one should start with. It covers a lot more than your current problem (which is "just" using a library that relies on JNI and native libraries) but, well, he who can do more can do less.

If you read it carefully, you'll see that one solution to use JNI libraries is to bundle them in architecture specific JARs so that you can depend on them like any other dependency from a Maven point of view. This is actually how JOGL version 1.1.1 is packaged in http://download.java.net/maven/2/net/java/dev/jogl/, there is one JAR artifact with the Java classes and several architecture specific JARs artifacts with the native libraries.

JNI library archived within the jar

The solution I ended up using was to
store the compiled jni library in the
jar alongside the class files.

This means either cross-compiling for
all possible architectures, or more
simply, having a different jar for
each architecture. This latter fits
quite well with our setup - where
almost all of our machines are
Linux-i386, with a smattering of win32
boxes.

Sadly System.load() can't cope with
loading libraries from within a jar,
so we'll therefore need a custom
loader which extracts the library to a
temporary file at runtime; this is
obviously achievable, however.

Then, as explained, the idea is to use a custom library loader to load the native library. The good news is that such a loader is "provided" as explained below.

Library loader

We now have our JNI library on the
class path, so we need a way of
loading it. I created a separate
project which would extract JNI
libraries from the class path, then
load them. Find it at
http://opensource.mxtelecom.com/maven/repo/com/wapmx/native/mx-native-loader/1.2/.
This is added as a dependency to the
pom, obviously.

To use it, call
com.wapmx.nativeutils.jniloader.NativeLoader.loadLibrary(libname).
More information is in the javadoc for
NativeLoader.

I generally prefer to wrap such things
in a try/catch block, as follows:

public class Sqrt {
    static {
        try {
            NativeLoader.loadLibrary("sqrt");
        } catch (Throwable e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    /* ... class body ... */
}

We should now be at the point where
our junit tests work from maven; a mvn
test should work! It should also work
fine from an IDE.

Now, to answer your questions, how to:

Automatically download, if necessary, the OS-specific JOGL zip file from here (contains 4 jar files and some native library files (.so/.dll)); or depend on a Maven project which is a wrapper of one of the files.

Sadly, JOGL 2.0 jars aren't available in the Maven repository of java.net so you'll have to deal with that and either make them available in a private repository or install them manually in the local repository of each developer. To do so, use mvn install:install-file as documented in the Guide to installing 3rd party JARs (and not mvn deploy:deploy-file as you did, this goal is used to install artifacts to a remote repository).

Personally, I would download JOGL 2.0 ZIPs from the URL you provided, package it as they did with JOGL 1.1.1 (one Java JAR and several specific JARs for native libraries) and install the JARs in each local repository for now. Then, declare a standard dependency on the Java artifact and, indeed, use profiles for the architecture specific dependency. Something like this:

<project>
  ...
  <dependencies> 
    <dependency>
      <groupId>net.java.dev.jogl</groupId>
      <artifactId>jogl</artifactId>
      <version>2.0-beta10</version>
    </dependency>
    ...
  </dependencies>
  ...
  <profiles>
    <profile>
      <id>linux-i586</id>
      <activation>
        <os>
          <arch>i386</arch>
          <family>unix</family>
          <name>linux</name>
        </os>
      </activation>
      <dependencies>
        <dependency>
          <groupId>net.java.dev.jogl.jogl-linux-i586</groupId>
          <artifactId>jogl-linux-i586</artifactId>
          <version>2.0-beta10</version>
        </dependency>
      </dependencies>
    </profile>
    ...
  </profiles>
  ...
</project>

Don't forget to add the repository required for the custom library loader and the dependency:

<project>
  <repositories>
    <repository>
      <id>opensource.mxtelecom.com</id>
      <url>http://opensource.mxtelecom.com/maven/repo</url>
    </repository>
    ...
  <repositories>
  ...
  <dependencies> 
    <dependency>
      <groupId>com.wapmx.native</groupId>
      <artifactId>mx-native-loader</artifactId>
      <version>1.2</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

Regarding the second part of your question:

Unzip that zip file appropriately, so that (...)

As I explained, you actually won't depend on ZIP files but on JARs and you won't need to uncompress them neither during development nor to distribute your project. For the distribution, you'll just need to create a jar including the dependencies. This can be done with the maven-assembly-plugin. See this answer for example for more details on this.

内心激荡 2024-08-23 06:15:50

Jogamp 现在包含对 Maven、jogl 组件的支持(jocl 和 joal 支持即将推出)。从 2.0-rc11 开始,包被推送到 Maven Central。

只需将其放入您的 pom 中即可:

 <dependencies>
   <dependency>
     <groupId>org.jogamp.gluegen</groupId>
     <artifactId>gluegen-rt-main</artifactId>
     <version>2.0-rc11</version>
   </dependency>
   <dependency>
     <groupId>org.jogamp.jogl</groupId>
     <artifactId>jogl-all-main</artifactId>
     <version>2.0-rc11</version>
   </dependency>
 </dependencies>

下次您尝试构建项目时,Maven 将提取所有依赖项。

在 wiki 上了解更多信息

Jogamp now contains support for Maven, for the jogl components (jocl and joal support is forthcoming). As of 2.0-rc11, packages are pushed to Maven Central.

Just put this to your pom:

 <dependencies>
   <dependency>
     <groupId>org.jogamp.gluegen</groupId>
     <artifactId>gluegen-rt-main</artifactId>
     <version>2.0-rc11</version>
   </dependency>
   <dependency>
     <groupId>org.jogamp.jogl</groupId>
     <artifactId>jogl-all-main</artifactId>
     <version>2.0-rc11</version>
   </dependency>
 </dependencies>

Maven will pull all of the dependencies the next time you attempt to build the project.

Read more here on the wiki

海拔太高太耀眼 2024-08-23 06:15:50

这里有一个 JOGL 2.0 的 Maven 存储库: http://jogamp.org/deployment/maven/

我使用 SBT 来构建我的项目。您需要添加到 build.sbt 的解析器:

resolvers += MavenRepository("jogamp", "http://jogamp.org/deployment/maven")

以及依赖项,例如,对于基本 jogl 库:

libraryDependencies += "org.jogamp.jogl" % "jogl-all" % "2.0-rc9"

在 maven xml 文件中,这将类似于(根据 this):

 <settings>
   <profiles>
     <profile>
       <id>jogamp</id>
       <activation>
         <activeByDefault>true</activeByDefault> 
       </activation>
       <repositories>
         <repository>
           <id>jogamp-remote</id>
           <name>jogamp test mirror</name>
           <url>http://www.jogamp.org/deployment/maven/</url>
           <layout>default</layout>
         </repository>
       </repositories>
     </profile>
   </profiles>
 </settings>

依赖项声明为:

<dependency>
  <groupId>org.jogamp.jogl</groupId>
  <artifactId>jogl-all</artifactId>
  <version>2.0-rc9</version>
</dependency>

要自动下载正确的本机 jar,请在sbt 我做了类似的事情:

sys.props("os.name") match {
  case "Linux" => "org.jogamp.jogl" % "jogl-all-natives-linux-i586" % "2.0-rc9"
  ... etc. ...

There is a maven repo for JOGL 2.0 here: http://jogamp.org/deployment/maven/

I use SBT for building my projects. The resolver you need to add to build.sbt:

resolvers += MavenRepository("jogamp", "http://jogamp.org/deployment/maven")

And the dependency, e.g., for base jogl library:

libraryDependencies += "org.jogamp.jogl" % "jogl-all" % "2.0-rc9"

In maven xml files this would be something like (according to this):

 <settings>
   <profiles>
     <profile>
       <id>jogamp</id>
       <activation>
         <activeByDefault>true</activeByDefault> 
       </activation>
       <repositories>
         <repository>
           <id>jogamp-remote</id>
           <name>jogamp test mirror</name>
           <url>http://www.jogamp.org/deployment/maven/</url>
           <layout>default</layout>
         </repository>
       </repositories>
     </profile>
   </profiles>
 </settings>

With dependency declared as:

<dependency>
  <groupId>org.jogamp.jogl</groupId>
  <artifactId>jogl-all</artifactId>
  <version>2.0-rc9</version>
</dependency>

To automatically download proper native jar, in sbt I do something like:

sys.props("os.name") match {
  case "Linux" => "org.jogamp.jogl" % "jogl-all-natives-linux-i586" % "2.0-rc9"
  ... etc. ...
猫九 2024-08-23 06:15:50

我不知道 JOGL 库,但我有使用 Java3d 的经验,它有相同的安装/构建问题。有两种方法可以实现此目的:

  • 告诉开发人员独立安装 JOGL,然后将 JOGL 库视为系统依赖项,就像我们对 Java3d 所做的那样

    <依赖>;
        javax.java3d;
        j3dcore;
        <版本>1.5.1
        <范围>系统
        ${java.home}/lib/ext/j3dcore.jar
    
    
  • 将所有 jar 和系统依赖库放在自己的存储库中,并为它们创建合适的 pom

如果您强烈希望通过 JOGL 的 Maven 安装实现自动化,您可以尝试使用 maven-antrun-plugin 或创建自己的 Maven 插件来处理安装(一个很好的例子是 Cargo 下载服务器并解压)。

我考虑使用第一个选项 - 告诉开发人员安装 JOGL。在我们的例子中,Java3d 应用程序是由 Java WebStart 分发的,因此对于他们来说,Java3d 的安装是由 WebStart 完全自动化的。

I don't know JOGL library but I have experience with Java3d, which has the same installation/build issues. There are two ways to achive this:

  • tell developers to install JOGL unassisted, then treat JOGL libraries as system dependencies like we do with Java3d

    <dependency>
        <groupId>javax.java3d</groupId>
        <artifactId>j3dcore</artifactId>
        <version>1.5.1</version>
        <scope>system</scope>
        <systemPath>${java.home}/lib/ext/j3dcore.jar</systemPath>
    </dependency>
    
  • place all jars and system dependent libraries in own repository and create suitable poms for them

If you strongly want to automate with Maven installation of JOGL you can try use maven-antrun-plugin or create own Maven plugin that handles installation (a good example is Cargo that downloads servers and unpack it).

I consider to use first option - tell developers to install JOGL. In our case Java3d application is distributed by Java WebStart so for them installation of Java3d is fully automated by WebStart.

只是在用心讲痛 2024-08-23 06:15:50

作为参考,这里是我的 Ant build.xml 文件的一部分,用于下载并解压 JOGL 库(2.0 beta 10)。

<target name="libraries" depends="libraries.jogl" />

<target name="libraries.jogl.check">
    <condition property="libraries.jogl.exists">
        <available file="lib/jogl" />
    </condition>
</target>

<target name="libraries.jogl" depends="libraries.jogl.check" unless="libraries.jogl.exists">
    <condition property="joglostype" value="windows-i586">
        <and>
            <os family="windows" />
            <or>
                <os arch="i386" />
                <os arch="x86" />
            </or>
        </and>
    </condition>
    <condition property="joglostype" value="windows-amd64">
        <and>
            <os family="windows" />
            <os arch="amd64" />
        </and>
    </condition>
    <condition property="joglostype" value="linux-i586">
        <and>
            <os name="Linux" />
            <or>
                <os arch="i386" />
                <os arch="x86" />
            </or>
        </and>
    </condition>
    <condition property="joglostype" value="linux-amd64">
        <and>
            <os name="Linux" />
            <or>
                <os arch="AMD64" />
                <os arch="x86_64" />
            </or>
        </and>
    </condition>
    <echo>Detected operating system: ${joglostype}</echo>
    <echo>(if invalid OS, update ant build file)</echo>

    <mkdir dir="lib" />
    <get src="http://download.java.net/media/jogl/builds/archive/jsr-231-2.0-beta10/jogl-2.0-${joglostype}.zip" dest="lib/jogl.zip" usetimestamp="true" />

    <mkdir dir="lib/jogl" />
    <unzip src="lib/jogl.zip" dest="lib/jogl">
        <patternset>
            <include name="**/gluegen-rt.jar" />
            <include name="**/jogl.all.jar" />
            <include name="**/nativewindow.all.jar" />
            <include name="**/newt.all.jar" />
            <include name="**/*.so" />
            <include name="**/*.dll" />
        </patternset>
        <mapper type="flatten" />
    </unzip>
</target>

Here, for reference, is the part of my Ant build.xml file that downloads and unzips the JOGL library (2.0 beta 10).

<target name="libraries" depends="libraries.jogl" />

<target name="libraries.jogl.check">
    <condition property="libraries.jogl.exists">
        <available file="lib/jogl" />
    </condition>
</target>

<target name="libraries.jogl" depends="libraries.jogl.check" unless="libraries.jogl.exists">
    <condition property="joglostype" value="windows-i586">
        <and>
            <os family="windows" />
            <or>
                <os arch="i386" />
                <os arch="x86" />
            </or>
        </and>
    </condition>
    <condition property="joglostype" value="windows-amd64">
        <and>
            <os family="windows" />
            <os arch="amd64" />
        </and>
    </condition>
    <condition property="joglostype" value="linux-i586">
        <and>
            <os name="Linux" />
            <or>
                <os arch="i386" />
                <os arch="x86" />
            </or>
        </and>
    </condition>
    <condition property="joglostype" value="linux-amd64">
        <and>
            <os name="Linux" />
            <or>
                <os arch="AMD64" />
                <os arch="x86_64" />
            </or>
        </and>
    </condition>
    <echo>Detected operating system: ${joglostype}</echo>
    <echo>(if invalid OS, update ant build file)</echo>

    <mkdir dir="lib" />
    <get src="http://download.java.net/media/jogl/builds/archive/jsr-231-2.0-beta10/jogl-2.0-${joglostype}.zip" dest="lib/jogl.zip" usetimestamp="true" />

    <mkdir dir="lib/jogl" />
    <unzip src="lib/jogl.zip" dest="lib/jogl">
        <patternset>
            <include name="**/gluegen-rt.jar" />
            <include name="**/jogl.all.jar" />
            <include name="**/nativewindow.all.jar" />
            <include name="**/newt.all.jar" />
            <include name="**/*.so" />
            <include name="**/*.dll" />
        </patternset>
        <mapper type="flatten" />
    </unzip>
</target>
情痴 2024-08-23 06:15:50

该脚本将从 URL 下载版本并将其安装到本地(指定)存储库。
https://gist.github.com/1624599

示例用法:./install_jogl_maven http:// /jogamp.org/deployment/v2.0-rc5 path_to_local_repo 2.0-rc5

this script will download a release from a URL and install it to a local (named) repository.
https://gist.github.com/1624599

example usage: ./install_jogl_maven http://jogamp.org/deployment/v2.0-rc5 path_to_local_repo 2.0-rc5

淡写薰衣草的香 2024-08-23 06:15:50

没有简单的方法可以实现这一点。尝试是否可以配置 maven-assemble-plugin 来构建可执行文件jar 并将正确的文件与您的代码打包在一起。您无法使用 Maven 依赖项管理来实现此目的,因为您需要 ZIP 内容而不是 ZIP 本身。您可以尝试 maven-ant-plugin

There is no easy way to accomplish this with. Try if you can configure the maven-assembly-plugin to build a executable jar and package the correct files with your code. You cannot use the maven dependency management to achieve this because you need the ZIP contents not the ZIP itself. You could try the maven-ant-plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文