使用CMake和Maven构建C ++和Java代码并将其捆绑在jar中 [英] Building C++ and Java code using CMake and Maven and bundle in a jar

查看:172
本文介绍了使用CMake和Maven构建C ++和Java代码并将其捆绑在jar中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用CMake构建的旧版C ++代码。它生成一个.so文件。我需要将此代码包装在Java中,并构建一个包含Java代码以及用于部署的C ++的jar。

I have a legacy C++ code that is built using CMake. It generates a .so file. I need to wrap this code in Java and build a jar that includes Java code as well as C++ for deployment.

使用CMake构建C ++代码的步骤很简单:

Steps for building C++ code with CMake is simple :

cd /to/pkg/dir
cmake .
make

.so文件在build /目录下生成。如果我将整个项目转换为Maven,则必须修改目录结构(这是一个博客,解释了该方法如何工作 http://blog.bigpixel.ro/2012/07/building-cc-applications-with-maven/ )。但是,我不想这样做。是否可以选择从maven调用上面显示的前两行来构建.so文件,然后将其包括在最终的jar中?

The .so file is generated under a build/ directory. If I convert the whole project into maven, I will have to modify the directory structure (here is a blog that explains how that could work http://blog.bigpixel.ro/2012/07/building-cc-applications-with-maven/). However, I don't want to do that. Could there be an option to call the first two lines shown above from maven to build .so file and then include it with the final jar?

推荐答案

在Apache Hadoop中,该构建与您所描述的类似。
我们在编译阶段使用 Apache Maven AntRun插件外部调用 cmake ,然后在CMake生成的生成输出上调用 make 来编译和链接C部分我们的代码库。然后,此输出将输入我们的最终构建工件中。在我们的例子中,这些构建工件是压缩文件,而不是直接捆绑到jar文件中,但是您可以通过控制 Apache Maven JAR插件。具体来说,您可能需要覆盖内容包含/排除设置

In Apache Hadoop, the build does something similar to what you described. We use the Apache Maven AntRun Plugin during the compile phase to make an external call to cmake and then call make on the build output generated by CMake to compile and link the C portion of our codebase. This output then feeds into our final build artifacts. In our case, those build artifacts are tarballs rather than bundled straight into a jar file, but you could accomplish it by controlling configuration of the Apache Maven JAR Plugin. Specifically, you may need to override content include/exclude settings.

如果您希望将其用作起点,则可以在此处看到Hadoop构建的相关部分:

If you'd like to use it as a starting point, the relevant part of the Hadoop build is visible here:

https://github.com/apache/hadoop/blob/release-2.7.3-RC2/hadoop-common-project/hadoop-common/pom.xml#L598-L615

<execution>
  <id>make</id>
  <phase>compile</phase>
  <goals><goal>run</goal></goals>
  <configuration>
    <target>
      <exec executable="cmake" dir="${project.build.directory}/native" failonerror="true">
        <arg line="${basedir}/src/ -DGENERATED_JAVAH=${project.build.directory}/native/javah -DJVM_ARCH_DATA_MODEL=${sun.arch.data.model} -DREQUIRE_BZIP2=${require.bzip2} -DREQUIRE_SNAPPY=${require.snappy} -DCUSTOM_SNAPPY_PREFIX=${snappy.prefix} -DCUSTOM_SNAPPY_LIB=${snappy.lib} -DCUSTOM_SNAPPY_INCLUDE=${snappy.include} -DREQUIRE_OPENSSL=${require.openssl} -DCUSTOM_OPENSSL_PREFIX=${openssl.prefix} -DCUSTOM_OPENSSL_LIB=${openssl.lib} -DCUSTOM_OPENSSL_INCLUDE=${openssl.include} -DEXTRA_LIBHADOOP_RPATH=${extra.libhadoop.rpath}"/>
      </exec>
      <exec executable="make" dir="${project.build.directory}/native" failonerror="true">
        <arg line="VERBOSE=1"/>
      </exec>
      <!-- The second make is a workaround for HADOOP-9215.  It can
           be removed when version 2.6 of cmake is no longer supported . -->
      <exec executable="make" dir="${project.build.directory}/native" failonerror="true"></exec>
    </target>
  </configuration>
</execution>

这篇关于使用CMake和Maven构建C ++和Java代码并将其捆绑在jar中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆