Alexey Kamenskiy
Alexey Kamenskiy

Reputation: 2948

Maven compile multi-module project with cyclic dependencies

I am working on my little OSS project in which I am using Maven as build tool. I split the project into smaller sub-projects to simplify development. Thus I have following structure:

project
+-- main-module
|   |
|   +- pom.xml
|
+-- submodule1
|   |
|   +- pom.xml
|
+ pom.xml

My thought was that main-module should provide interfaces which each submodule should be implementing in order to be plugged into whole application. Therefore submodule1/pom.xml contains compile time dependency reference to main-module. In its turn I also need to be able to test whole application and thus main-module/pom.xml contains test scope dependency reference to submodule1. As the result maven refuses to compile projects saying that they contain cyclic references.

My thought was that maven could first compile classes of main-module as it does not require any compile time dependency on any of submodules, then it using compiled classes of main-module could compile classes of submodule1 and after that compile test classes of main-module (to be able run tests). But seems that maven compiler does not take in account the scope of dependency and I somehow need to work around that.

The only solution I can see is to move away tests from main-module, which doesn't really make sense for me as only that module provides main logic.

My question - is there any other way around this issue except for moving away tests? Or maybe something is wrong with my understanding of how maven-reactor-plugin should work?

Upvotes: 0

Views: 2124

Answers (2)

Pavel Horal
Pavel Horal

Reputation: 18194

I know this is more of a comment, but might provide you with (not so pretty) solution:

You can seet your submodule1 as a dependency of maven-surefire-plugin (so that reactor is forced to build it) and then play with its settings... i.e. childDelegation or additionalClasspathElements

Upvotes: 1

SpaceTrucker
SpaceTrucker

Reputation: 13556

Instead of moving your tests away, you could move all of your API into it's own module. Then your main module would contain the application and you can freely distribute your application's API to allow others to access it. If they want to develop new functionality they do not necessarily need the sources of your app.

I consider this a much better style, because sub modules with specific functionality can now clearly separate between what is your applications API and what is the code your application needs to startup/shutdown, etc.

This is in my mind the intended way of how maven projects should look like. It also sticks with the single responsibility principle. The main modules responsibility is to startup/shutdown, etc your application. The API modules responsibility is to show how other developers can access your application. And the other submodule provide specific functionality for your application.

Upvotes: 2

Related Questions