java Maven 和 JOGL 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1962718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:43:59  来源:igfitidea点击:

Maven and the JOGL library?

javamavenmaven-2build-processjogl

提问by Ricket

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:

过去几天我一直在空闲时间学习 Maven,但似乎无法弄清楚如何组织项目以便使用 JOGL 库。我更喜欢以下内容:

  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?)
  1. 如有必要,从此处自动下载特定于操作系统的 JOGL zip 文件(包含 4 个 jar 文件和一些本机库文件 (.so/.dll));或依赖于一个 Maven 项目,它是其中一个文件的包装器。
  2. 适当地解压缩该 zip 文件,以便:
    1. jar 文件被添加到类路径并根据需要进行部署,以及
    2. 本地库文件被添加到最终的 jar 文件中(这会让它们被自动使用,还是我需要更多的东西?)

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.

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

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.)

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

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.

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

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.

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

采纳答案by Martin Seeler

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.

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

Just put this to your pom:

把它放到你的 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.

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

Read more here on the wiki

在 wiki 上阅读更多信息

回答by Pascal Thivent

When dealing with JNI and Maven, Projects With JNIis the referenceone 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.

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

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 库的一种解决方案是将它们捆绑在特定于体系结构的 JAR 中,这样您就可以像依赖 Maven 的任何其他依赖项一样依赖它们。这实际上是 JOGL 版本 1.1.1 在http://download.java.net/maven/2/net/java/dev/jogl/ 中的打包方式 ,有一个带有 Java 类的 JAR 工件和几个特定于体系结构的 JAR 工件与本地库。

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.

JNI 库归档在 jar 中

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

这意味着要么对所有可能的架构进行交叉编译,要么更简单地说,为每个架构使用不同的 jar。后者非常适合我们的设置——我们几乎所有的机器都是 Linux-i386,还有少量的 win32 机器。

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

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.

库加载器

我们现在在类路径上有我们的 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 for NativeLoader.

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

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

我们现在应该是我们的 junit 测试在 maven 中工作的时候了;一个 mvn 测试应该可以工作!它也应该可以在 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.

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

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-fileas documented in the Guide to installing 3rd party JARs(and not mvn deploy:deploy-fileas you did, this goal is used to install artifacts to a remote repository).

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

Personally, I would download JOGL 2.0 ZIPs from the URLyou 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 profilesfor the architecture specific dependency. Something like this:

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

<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 (...)

适当地解压缩该 zip 文件,以便 (...)

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 answerfor example for more details on this.

正如我所解释的,您实际上不会依赖于 ZIP 文件,而是依赖于 JAR,并且在开发过程中或分发项目时都不需要解压缩它们。对于发行版,您只需要创建一个包含依赖项的 jar。这可以通过 maven-assembly-plugin 来完成。例如,有关此问题的更多详细信息,请参见此答案

回答by axel22

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

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

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

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

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

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

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

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

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

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

 <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:

要自动下载适当的本地 jar,在 sbt 中我执行以下操作:

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

回答by cetnar

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:

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

  • 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

  • 告诉开发人员独立安装 JOGL,然后将 JOGL 库视为系统依赖项,就像我们对 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>
    
  • 将所有 jars 和系统相关库放在自己的存储库中并为它们创建合适的 poms

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

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

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.

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

回答by Ricket

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

作为参考,这里是我的 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>

回答by Tyson

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

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

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

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

回答by Eduard Wirch

There is no easy way to accomplish this with. Try if you can configure the maven-assembly-pluginto 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.

没有简单的方法来实现这一点。试试你是否可以配置maven-assembly-plugin来构建一个可执行的 jar 并用你的代码打包正确的文件。您不能使用 maven 依赖管理来实现这一点,因为您需要 ZIP 内容而不是 ZIP 本身。你可以试试maven-ant-plugin