Building C++ and Java code using CMake and Maven and bundle in a jar

北城余情 提交于 2020-05-24 21:36:28

问题


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.

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

cd /to/pkg/dir
cmake .
make

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?


回答1:


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.

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>


来源:https://stackoverflow.com/questions/44639122/building-c-and-java-code-using-cmake-and-maven-and-bundle-in-a-jar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!