我有一个使用CMake构建的遗留C ++代码。它会生成一个.so文件。我需要用Java包装这个代码并构建一个包含Java代码和C ++的jar用于部署。
使用CMake构建C ++代码的步骤很简单:
cd /to/pkg/dir
cmake .
make
.so文件在build /目录下生成。如果我将整个项目转换为maven,我将不得不修改目录结构(这是一个博客,解释了如何工作http://blog.bigpixel.ro/2012/07/building-cc-applications-with-maven/)。但是,我不想这样做。可以选择从maven调用上面显示的前两行来构建.so文件,然后将它包含在最终的jar中吗? 提前谢谢。
答案 0 :(得分:8)
在Apache Hadoop中,构建执行与您描述的类似的操作。
我们在编译阶段使用Apache Maven AntRun Plugin来对cmake
进行外部调用,然后在CMake生成的构建输出上调用make
来编译和链接代码库的C部分。然后,此输出将输入到我们的最终构建工件中。在我们的例子中,那些构建工件是tarball而不是直接捆绑到jar文件中,但是你可以通过控制Apache Maven JAR Plugin的配置来完成它。具体而言,您可能需要覆盖内容include/exclude settings。
如果您想将它作为起点,可以在此处看到Hadoop构建的相关部分:
<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>