How do you tell Maven that one module depends on another?

Asked

Viewed 814 times

2

I have two modules and each depends on the other.

How to say this on Maven dependencies?

2 answers

3

Cyclical dependency? This is project failure, because how will you compile project A if it depends on project B and vice versa? The correct thing in this case is to separate the common features in a new C project so that A and B will depend only on C.

  • Maybe I can’t change the code, it’s a big, legacy project

  • Then you have a big problem, because in the compilation will always give error by lack of dependency...

  • managed to compile, but by an "alternative method", made a 3rd project and in the linksource of this points to the source of these two modules, so they were able to compile and run, now I’m trying to do a refectory and I came across this situation

2

Since you mentioned that you can’t alter the structure, I’m going to give you three ideas for circumvent the situation:

Including the sources

In one of the projects include the necessary sources of the other using the Maven Plugin Build Helper. Note that it will not have a direct dependency.

After compiling, you can delete compiled classes that are not part of this project on jar or in the War.

The other project can depend on the first without problems, provided it respects the build order. Or you can apply this solution to both projects.

Including the classes

The previous solution can be implemented by directly including the compiled classes of a project in the classpath of the other. This can be done, for example, with the Maven Antrun Plugin.

Note that in this solution and the previous one, it is considered that the two projects are available in the local file system. If projects are compiled on a server, you can still use another plugin to download the required files from your file versioner.

Creating a dependency fake

With the simplest design, manually create a jar with only the required classes and interfaces. Add this jar to your local repository, your company’s Artifactory or Nexus.

Then make the most complex project depend on this new jar in scope provided, that is, it will be used at compile time but not running.

Thus, the compilation of the most complex project will occur without problems, since at least the "bark" of the necessary classes exists in the classpath. And the simplest project can depend directly on the complex project, which will already be compiled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.