Tag Archives: tips

How to Create Two JARs from One Project (…and why you shouldn't)


January 26, 2010 By Tim O'Brien

This is a repost of an answer on Stackoverflow.   Someone asked a question about creating two build targets from a single project.  Here is an excerpt from the question:

…the ant build has 2 build targets – one that builds the entire app, and one that builds a jar from some of those files (only a few). In ant, it’s easy to have multiple build targets to handle this, but I’m trying to determine the best way to handle this in maven. I could split the subset of files into a second project and it will have its own pom. Then the first project could depend on this one. However, since the subset of files is so small (less than 10), it seems like it might be overkill to have an entirely new project for that.

I answered by detailing two solutions.   Using two separate profiles to build two classified JAR artifacts, and configuring two extra executions of the JAR plugin.   I want to be very clear about this answer.   We strongly recommend that you not configure a Maven project to create two separate JARs from a single project.  It violates one of the most important core concepts of Maven: modularity. Once you go down this path, you will likely be tempted to start creating more and more “uber” projects that create multiple JARs.   If you start designing complex POM profiles to generate these JARs you are going to have to do a lot of heavy-lifting managing dependencies (classified dependencies), and fixing hard to diagnose bugs in different projects.

In doing this, you break a core convention of Maven, and when you do this, you are on your own.  Tools that have been designed to work with Maven won’t help you.   The various forums and mailing lists set up to provide you with help will be of no use.  I wrote this answer to counter the argument that Maven isn’t “flexible” and that it “forces” you to work within its limitations.   This isn’t true.  You can do just about anything you want to do in Maven.  You can customize the lifecycle to do just about anything you need it to do.   Maven, like Ant, is infinitely flexible.  Assuming that you’ve read the books and are familiar with plugin configuration syntax, you could configure Maven to do anything.    The real question isn’t “can I do this with Maven?” it is “should I do this with Maven?”.   If you are violating a core convention, the answer is usually no.

The following is my answer from Stackoverflow: Continue reading