Launch MongoDB with Maven

Spread the love

Question Description

I am using the MongoDB Java driver to perform some persistence in my application. The build for my application is managed via Maven and I’m looking for the best way to integrate a series of MongoDB-related unit tests into my Maven build process. I cannot assume that the user building the application has installed the MongoDB dameon as a service and therefore need to launch the daemon prior to the execution of the related unit tests.

My first thought was to store the binaries in the resources folder of the test directory (e.g., src/test/resources) and launch the daemon using Runtime.exec(). Is there an approach that’s cleaner? I feel like Runtime.exec() is a quick and dirty way to get something working but not the most ideal…I need this to work on both linux and windows.

Practice As Follows

I’ve created a Maven plugin that wraps the ’embedded mongo’ API:


It provides a start goal that you can use to start any version of MongoDB you want (e.g. during pre-integration-test), and a stop goal that will stop MongoDB (e.g. during post-integration-test).

MongoDB binaries are downloaded and stored in ~/.embedmongo for future builds.

You may be interested in these books.


As an Amazon Associate I earn from qualifying purchases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.