Integrating Maven NAR plugin with Google’s googletest unit test framework

Previously I wrote an article about building C/C++ applications with maven and maven NAR plugin. However that article left out the part of running the unit tests, therefore I’ll present in this article a way of integrating the build process with Google’s unit test framework. So basically will show you how to quickly integrate maven NAR plugin with Google’s unit test framework.

First, since google test is a third party library and it does not seem to be mavenized, we have to do this by ourselves. For this, the easiest way is to download the library sources and turn them into a nar maven dependency which will reference from the projects we want to test with. The latest version (at this time) can be downloaded from this link. Once downloaded, you’ll need to extract the header & the source files into the specific NAR project structure (follow this link to see the required structure of a NAR project). Once this is done, there are two more things to be taken care of. First would be to create a pom.xml file, to build and package the library into a maven artifact (which can be either installed locally, added as a module to your project, or put in your company’s artifact repository for later dependency resolving). For this here is a sample pom.xml file, which should be edited and changed according to your preferences and naming conventions:

<project xmlns="" xmlns:xsi=""




    <!-- parent POM will disable deployment to make sure no useless artifacts will be deployed
    however we reenable deployment here, to be able to deploy new builds of the engine in
    artifactory -->



    <id>yout snapshot repository id here</id>
    <name>yout snapshot repository name here</name>
    <url>yout snapshot repository url here</url>


You will notice is a fairly usual NAR pom.xml file, the only addition being adding the linker section to the plugin config. This is required since the unit test library depends on the pthread library.
The second remaining thing would be to fix the compile errors and build/install/deploy the artifact. You can remove the file since the build process will include the required files anyway. Then you will need to fix the include references so the project will build correctly.

This being completed, you now have the gtest library as a maven artifact. You can add it now as a dependency to other projects, and you can write tests with it. To adding it as a dependency, you will need to add it something like this:


once this done, you can start using it in tests, like this:

#include <gtest/gtest.h>

TEST(dummyTest, AllwaysTrueTest) {
    EXPECT_EQ(1, 1);

However in order to be able to actually run the tests, they should be added in the pom.xml file in the config section of the maven-nar-plugin (the example shows how to add the dummyTest):


Please note however that is sufficient to add only one test, as it seems like google test framework will run all the tests anyways.

A sample module based github repository featuring a library project using googletest and the googletest itself as a module can be found at this  link. The sample was built and run with netbeans.

Happy unit testing

Leave a Comment