Bozhidar Bozhanov

About Bozhidar Bozhanov

Senior Java developer, one of the top stackoverflow users, fluent with Java and Java technology stacks - Spring, JPA, JavaEE. Founder and creator of Computoser and Welshare. Worked on Ericsson projects, Bulgarian e-government projects and large-scale online recruitment platforms.

Embedding Maven

It is a very rare usecase, but sometimes you need it. How to embed Maven in your application, so that you can programatically run goals?

Short answer is: it’s tricky. I dabbled into the matter for my java webapp automatic syncing project, and at some point I decided not to embed it. Ultimately, I used a library that does what I needed, but anyway, here are the steps and tools that might be helpful.

What you usually need the embedded maven for, is to execute some goals on a maven project. There are two scenarios.

The first one is, if you are running inside the maven container, i.e. you are writing a mojo/plugin. Then it’s fairly easy, because you have everything managed by the already-initialized plexus container. In that case you can use the mojo-executor. Easy to use, but expects a “project”, “pluginManager” and “session”, which you can’t easily obtain.

The second scenario is completely embedded maven. There is a library that does what I needed it to do (thanks to MariuszS for pointing it out) – it’s Maven Embedder. Its usage is described in this SO question. Use both the first and the second answer.

Before finding that library, I tried two more libraries: the jenkins maven embedded and the Maven Invoker. The problem in both libraries is: they need a maven home. That is, the path to where a maven installation resides. Which is kind of contrary to the idea of “embedded” maven. If the Maven Embedder suits you, you can stop reading. However, there might be cases where the Maven Embedder might not be what you are looking for. In that case, you should use one of the two aforementioned libraries. So, how to find and set a maven home?

  • Ask the user to specify it. Not too much of a hassle, probably
  • Use M2_HOME. One of the libraries uses that by default, but the problem is it might not be set. I don’t usually set it, for example. If it is not, you can fallback to the previous approach
  • Scan the entire file system for a maven installation – sounds ok, and it can be done only once, and then stored in some entry. The problem is – there might not be a maven installation. Even if it’s a developer’s machine – IDEs (Eclipse, at least) have an “embedded” maven. And while it probably stores it somewhere internally in the same format a manual installation would, it may change it’s path or structure depending on the version. You can, of course, re-scan the file tree every once in a while to find such an installation
  • Download Maven programatically yourself. Then you can be sure where it is located and that it will always be located there in the same format. The problem here is version mismatch – the user might be using another version of maven. Making the version configurable is an option.

All of these work in some cases, and don’t work in others.

So, in order of preference:

  1. make sure you really need to embed maven
  2. use the Maven Embedder
  3. use another option with its implications.

 

Reference: Embedding Maven from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

2 Responses to "Embedding Maven"

  1. Bozho says:

    Many thanks, MariuszS – I updated the article in all possible locations (requested an update here as well)

Leave a Reply


8 × eight =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books